Calling Methods From Top Of Inheritance Chain?
Jul 14, 2014
So I'm just a little unclear about this, but how would I call methods from the 'top' of an inheritance chain? I say 'top' because Object is the top... E.g.:
public class AClass {
public void myMethod() { ... }
}
public class BClass extends AClass {
public void myMethod() { ... }
}
public class CClass extends AClass {
public void myMethod() { ... }
}
Assuming that BClass.myMethod() completely overrides AClass.myMethod() (so that there is no call to super.myMethod() in BClass.myMethod()) How can I call AClass.myMethod() from CClass.myMethod()?
View Replies
ADVERTISEMENT
Jul 6, 2014
The first is clear , new Person().printPerson(); displays Person but for the second : new Student().printPerson(); it accesses the Student constructor that points to the Person class => object. It builds the Person instance then goes back to the Student constuctor .Both methods are private and to my knowledge invisible one to the other , except that you cant run the the Person one because it's private so the only one in the Student class is the Student one . Guess it 's incorrect , but why ? (is because private methods cant be overriden and somehow the super class one always has priority ? , even if it's private?)
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
[code]....
View Replies
View Related
Mar 6, 2014
I've an interface with generic methods in it. I would like to have specialized methods in the sub types. While doing that I'm seeing the following warnings in eclipse.
class Sorter {
<E> void sort(E[] elements);
};
class StringSorter {
// This gives me a warning 'hiding' to 'sort'
<String> void sort(String[] elements) {
}
// Gives me an error "The method someCrap(String[]) in the type StringSorter is not applicable for the arguments (String[])"
void someCrap(String[] elements) {
}
};
I would like to understand why eclipse gives the above warnings and errors.
View Replies
View Related
Apr 23, 2014
What I have done wrong
public class BankAccount
{
String name;
int accountID;
double balance;
public void setAccount( String username, int ID, Boolean isJoint)
[Code] ....
View Replies
View Related
Jul 26, 2014
I'm working with Libgdx but I have a basic java question. I'm trying to access the overridden methods from and interface in another class via a call but I'm not sure how. This is what I've got so far :
Java Code:
public interface Controller {
public void show ();
}
public class MainActivity extends AndroidApplication implements Controller {
@Override
public void showAd(boolean show) {
System.out.println("TEST");
[Code] ....
Right now this code returns a null pointer at the call.
View Replies
View Related
Dec 14, 2014
I have two classes, MonsterGame (shown below) and Monster. Monster has a public accessor method to get it's private character variable, which represents the first character of the name of the Monster in question.
I'm just a bit confused as to why am I unable to cycle through the array of Monsters I have in the MonsterGame class and call
m.getCharacter(); (pretty much the last line of code)
public class MonsterGame{
private static final char EMPTY_SQUARE = ' ';
private char[][] gameBoard = new char[10][10];
private Monster[] monsters = new Monster[4];
private int maxXBoardSize = gameBoard.length -1;
private int maxYBoardSize = gameBoard[0].length -1;
[Code] ....
I understand that there need to be instances of objects to call methods, but is that not the case here? the Monster objects are have already been created, no? Do I need to create an index for the array? is the for loop not enough?
View Replies
View Related
May 7, 2015
I can call a child method from a main method. If I have a parent called "Assessment", a child called "Quiz", and another child called "test". Can I instantiate an object like Assessment a = new test(); and call a method in test.
I know the method can be called if I instantiate the test t = new test() but that defeats the purpose of inheritance...
View Replies
View Related
Apr 21, 2015
Basically I'm trying to code this program but I keep getting error can't be applied to given types. I know it has to do with my method trying to be called by an array, but I'm just kinda lost.
Write a program that prompts the user to input cash amounts and use a method to calculate the average amount for a week given 7 inputs of daily cash intake amounts for a cash register. Use an array to store the values. Recall that average is the sum of the values divided by the number of values. The method should take argument(s) of type double and return average weekly cash amount as a double.
Make sure you test your program using the test data given below and calls the averageCash() method defined above passing in the values. Verify that the input values are greater than or equal to 0. Negative numbers are not allowed. Output the values used and the averageCash value.
import java.util.*;
public class ArrayHandout {
public static void main(String args[]) {
int[] a=new int[6];
Scanner sc=new Scanner(System.in);
[Code] ....
View Replies
View Related
Sep 30, 2014
This current one is to calculate a planes holding pattern. I have to write a method to prompt user to enter speed in knots, then it converts it to km/hr. Another method to calc. pattern width using the speed, another method to calc. pattern length, than a main method which would call and print out the speed in knots, speed in km, pattern width and length.My current problem is on the second method. It works in that I can enter the values and it gives me the correct answers, however it's asking me to enter the speed twice, instead of just once. Anything I try just results in errors and won't compile.
import java.util.Scanner ;
//main method
public class TitleRemoved {
public static void main(String[] args) {
double airSpeedKm = airSpeedOts () ;
System.out.println("That speed is " + airSpeedKm + " km/hr.") ;
[code].....
I want my code to not only work, but be organized and easily readable as well, so I want to avoid bad habits.
View Replies
View Related
Mar 1, 2015
Does child class gets a copy of the methods and variables of parent class?
public class test1 {
public static void main(String a[]) {
Child c = new Child();
c.print();
[Code] ....
why is the output 1?
View Replies
View Related
Feb 3, 2014
eg: "Sourdough".toUpperCase();
I guess this is actually a 2-part question:
- How does this work? (nothing seems to be passed through the parameter, so how is it that the value or variable is manipulated in this fashion?)
- How would I create a method that does something similar? (ie. adjusting a variable without having it passed through the parameters)
View Replies
View Related
Apr 15, 2015
In this project each individual will create a data analysis program that will at a minimum,
1) read data in from a text file,
2) sort data in some way,
3) search the data in some way,
4) perform at least three mathematical manipulations of the data,
5) display results of the data analysis in numeric/textual form, and
6) display graphs of the data. In addition,
7) your program should handle invalid input appropriately and
8) your program should use some "new" feature that you have not been taught explicitly in class.
(Note: this is to give you practice learning new material on your own - a critical skill of today's programmer.) If you do not have a specific plan in mind for your project, below is a specific project that meets all of the qualifications as long as 7) and 8) are addressed in the implementation.
Everything is done except I need to call my methods in my GradeTester.
GradeBook:
/**
*This class creates an array called scores.
*This class determines the length of the array scores and determines the last grade in the array scores.
*This class sorts the array using a bubble sort, and searches the array.
*This class calculates the mean, standard deviation, and the median of the grades in the array scores.
*Once the grades in the array is sorted, the class then calculates the highest and lowest grades in the array.
*/
public class GradeBook {
public final int MAXARRAY_SZ = 20;
double [] scores = new double [MAXARRAY_SZ];
int lastGrade = 0;
double mean = 0;
[Code] ....
View Replies
View Related
Oct 21, 2014
package Experimentation;
import javax.swing.*;
import java.awt.event.*;
public class SimpleGUI1B implements ActionListener {
JButton button;
public static void main(String[] args) {
SimpleGUI1B gui = new SimpleGUI1B();
gui.go();
[code]...
This is a program from Head First Java! since main is static it shouldn't be able to call non-static methods because statics do not use any instance variable values but in the above program we're call a non-static method go() how is it possible?
View Replies
View Related
Jul 11, 2014
I'm learning about inheritance and part of my problem is to create an Order with methods, then an UpdateOrder where the total price is changed by adding four dollars to it, and then a main method displaying a few orders. I've copied all three below in order. My question is when I run the program it will display the totalprice() first for the second order followed by name, number, etc.what you override always displayed first regardless of the order you put them in? (The issue is at line 31 on the third code.)
import javax.swing.JOptionPane;
public class Order { //superclass
private String customerName;
private int customerNumber;
protected int quantityOrdered;
protected double unitPrice;
protected double totalPrice;
[code]....
View Replies
View Related
Dec 16, 2014
I am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...
View Replies
View Related
Jun 10, 2014
how inheritence and exception work together ?? what are the rules ??
View Replies
View Related
Apr 17, 2014
If I define a class which contains a few static fields, and then have a few classes who inherit this class, then all these classes would have the static field as well. Now my question is the following: would all those sub classes (and the base class itself) share the same object, or would each class have one object for all it's instances?
View Replies
View Related
Feb 24, 2014
I've tried to write a package and two classes this way:
<path>/pack/Aclass.java
Java Code: package pack;
public class Aclass<T> {
private T t;
public void set(T t) {
this.t = t;
[code]....
View Replies
View Related
Apr 24, 2014
I am in an intro programming class and we got assigned a problem for creating a super class with about a dozen sub classes for generating a random word(via WordGetter class) and then comparing that word to a variety of different patterns(like: does the word contain "re"). We were given the super class which looks like this...
public class Pattern {
public boolean matches(String text) {
return true;
}
public String toString() {
return "(TRUE)";
[code]...
and from this class, we have to write subclasses that override those three methods. I am struggling to understand inheritance and I am not really sure where to even start. Here is the instructions for the first sub class we need to write...
"CONTAINS" SUBCLASS
Constructor: The constructor accepts a String named ‘letters’.
Matches: This pattern matches any text that contains at least one occurrence of each ‘letter’.
toString: produces the text “(CONTAINS <LETTERS>)” where <LETTERS> is the ‘letters’ string.
getLetters(): this method must return letters.
equals(Object): careful on this one. Two Contains are equal if they have the same letters (order is not relevant).
(Example):
Pattern p = new Contains(“re”);
boolean f1 = p.matches(“renew”); // f1 is true
boolean f2 = p.matches(“zoo”); // f2 is false
String s = p.toString(); // s is “(CONTAINS re)”
boolean f3 = p.equals(new Contains(“er”)); // f3 is true.. really..
View Replies
View Related
Jun 25, 2014
If i have 2 classes, Top and ClassB which extends Top
public class Top {
String variable;
public Top(){
setVariable();
}
void setVariable(){
variable = "variable is initialized in Main Class";
[code]....
So what is happening when ClassB inherits from Top?I know that the B constructor is calling super, so does that mean its calling setVariable (in Top?) but as its overridden in ClassB, then that is whats being called and setting the String variable?
View Replies
View Related
Sep 26, 2014
Here is my abstract Boat class.
public abstract class Boat{
private int height;
private int length;
private int width;
private double boatValue;
private double chargeRate;
private Owner owner;
public Owner getOwner() {
return owner;
[Code] ....
View Replies
View Related
Apr 25, 2014
i was leaning inheritance and tried to implement it in Java.This is my base class vehicl.java
public class vehicle{
private int topSpeed;
private int cubicCap;
private String modelName;
private boolean hasAlloy;
[code]...
I also have a derived class called car.java.What i wanted to do in the derived class was that i wanted to override the constructor as well as the getInfo() method from the base class.I wanted to add an additional attribute to the car "numberSeats" and display tat too when the object to car class calls the getInfo() method .It showed an error "identifier required" when i tried to compile car.java program.
import java.util.Scanner;
public class car extends vehicle{
//int numberSeats;
//System.out.println("Enter the number of Seats");
Scanner numberSeats=new Scanner(System.in);
numberSeats=numberSeats.nextInt();
//System.out.println(numberSeats.nextInt());
[code]....
explain the errors that i get when i tried to compile car.java without using super keyword or without defining the constructor from the Car class ?
View Replies
View Related
Apr 30, 2014
Design Patterns are one form of reuse. so is inheritance. what are the similarities and difference between them?
View Replies
View Related
Nov 19, 2014
I am writing small pieces of code to make sure I understand Java basics and I have the following.
package teams1;
public abstract class Team1{
private String sport = new String();
public abstract String getSport();
public abstract void setSport();
}
import teams1.*;
[Code] .....
It doesn't compile because sport is private in the super class, but I thought FootballTeam1 would inherit it's own copy of sport because it is extending Team1.
View Replies
View Related
Jan 4, 2015
I have a working program, except that it does not calculate the credit hours and the financial aid. When I enter an input, it works, until it should show the student name, credit hours and financial aid. the error i get from the command is "Hours invalid for false student". Here is the program i think i might have the problem.
import java.text.DecimalFormat;
public abstract class Student
{
//initialise variables
String name;
int creditHrs;
[Code] .....
View Replies
View Related
Apr 6, 2014
I am trying to figure out how I can most easily make it easier to make new types of units in my game. I have buildings and ships, and would like to know how I could make it easy to add new units. I have been recently told about interfaces, and have worked with inheritance a little bit.
What I would like to able to do is have it so that all of the variables and methods common to all ships could be stored in a superclass or interface, and same with the buildings. I would also like to be able to assign behaviours to the buildings and ships, maybe as interfaces, which could contain all of the methods and variables required for the functions of that ship or building.
For example, creating a new type of building that can shoot, build ships, and can regenerate nearby ships. So it would possible inherit all of the variables and methods common to all buildings, such as health, image, x, y, getX(), getY() etc. But it would then also gain the variables and methods essential for its functionality, such as shootRange, shoot(), regenRate, etc.
How could this best be achieved?
View Replies
View Related