Java Inheritance - Calling Superclass Method

Java Inheritance - calling superclass method

You can do:

super.alphaMethod1();

Note, that super is a reference to the parent class, but super() is its constructor.

Java Inheritance: Calling a subclass method in a superclass

I'm very new to java and would like to know whether calling a subclass
method in a superclass is possible.

A superclass doesn't know anything about their subclasses, therefore, you cannot call a subclass instance method in a super class.

where is the proper place to set public static void main.

I wouldn't recommend putting the main method in the Admin class nor the User class for many factors. Rather create a separate class to encapsulate the main method.

Example:

public class Main{
public static void main(String []args) {
User user1 = new Admin("Bill", 18, 2);

System.out.println("Hello "+user1.getName());
user1.getLevel();
}
}

How to call the superclass implementation of an overridden method from inside the superclass in Java?

You could move said method body to a private method and let the default method (the one which may be overridden by the subclass) delegate to the former. See this example

public abstract class SuperClass {

private Object field1;

protected SuperClass(Object obj){
// call the safe implementation
setField1Safe(obj);
}

public void setField1(Object obj){
// just delegates
setField1Safe(obj);
}

private void setField1Safe(Object obj){
// perform some check on obj and
// then set field1 such that field1==obj
}
}

public class SubClass extends SuperClass{
public SubClass(Object obj){
super(obj);
}

@Override
public void setField1(Object obj){
super.setField1(obj);
// do some work that is necessary only when
// field1 is set through SubClass.setField1()
}
}

That way the sub class can still override setField1 but if you really depend on the implementation then you can call the private setField1Safe method.

Calling super super class method

You can't even use reflection. Something like

Class superSuperClass = this.getClass().getSuperclass().getSuperclass();
superSuperClass.getMethod("foo").invoke(this);

would lead to an InvocationTargetException, because even if you call the foo-Method on the superSuperClass, it will still use C.foo() when you specify "this" in invoke. This is a consequence from the fact that all Java methods are virtual methods.

It seems you need help from the B class (e.g. by defining a superFoo(){ super.foo(); } method).

That said, it looks like a design problem if you try something like this, so it would be helpful to give us some background: Why you need to do this?

Calling a superclass method from a subclass

The problem is with

return;

You should replace it by

return withdrawal;

    public double makeWithdrawal(double withdrawal) {
double tempbalance = getBalance();
if (withdrawal > getBalance()) {
withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
return withdrawal;
}
else {
return super.makeWithdrawal(withdrawal);
}
}

calling method in super class using object of sub class how it work?

a.print() is an example of method overriding which is runtime polymorphism. That is, at runtime it decides whether to call the subclass method or the super class method (here your SubClass method is called as it is overridden and matches the parameter signature).

In case of someMethod() call, you have not defined it in your SubClass. That means, your SubClass automatically got a someMethod() when you inherited it from Superclass as it is public. Putting it in simple words, SubClass also has the definition of someMethod(). Thats how JVM got to know which method to call.

Also in SuperClass a, the variable 'a' will be stored in stack whereas the reference will be stored in heap. You have constructed the reference using SubClass's constructor. Meaning the variable 'a' in stack refers to a 'SubClass' object in heap. That is why 'a' is able to call someMethod

Calling a subclass method from superclass

When you declare a variable as having the type of the superclass, you can only access (public) methods and member variables of the superclass through that variable.

Pet cat = new Cat("Feline",12,"Orange"); 
cat.getName(); // this is OK
cat.getColor(); // this is not OK, getColor() is not in Pet

To access the methods in the concrete class (Cat in this case), you need to either declare the variable as the derived class

Cat cat = new Cat("Feline",12,"Orange"); 
cat.getName(); // OK, getName() is part of Cat (and the superclass)
cat.getColor(); // OK, getColor() is part of Cat

Or cast it to a type you know/suspect is the concrete type

Pet cat = new Cat("Feline",12,"Orange"); 
((Cat)cat).getName(); // OK (same as above)
((Cat)cat).getColor(); // now we are looking at cat through the glass of Cat

You can even combine the two methods:

Pet pet = new Cat("Feline",12,"Orange"); 
Cat cat = (Cat)pet;
cat.getName(); // OK
cat.getColor(); // OK


Related Topics



Leave a reply



Submit