Subclass Reference by Superclass variable?

1.8k views Asked by At

When a class extends a class, we can use Super-class reference while assigning memory to the subclass object. I have understood so far is that it is ok to do so, because a subclass inherits the data of its parent class, but it cannot access the members of the subclass because it is the just the reference, and hence does not know of what additions are done by the child class.

My question is when I included method hiding to the above concept, the superclass reference variable started to refer to the child's class function. Why is that ? Why it didnt call it's own method as it is supposed to ?

class A{
 void show(){ System.out.print("CLass A. \n"); }
}

class B extends A{
 void show(){System.out.print("Class B. \n");  }
}

class Main{
 public static void main(String[] args){
  A a= new A();
  B b= new B();
  a.show();   // prints Class A
  b.show();   // prints Class B

  A a1= new B();
  a1.show();   // print Class B. why is this ? it should be Class A as per theory? 
 }
}
3

There are 3 answers

0
Suresh Atta On BEST ANSWER

variables and methods are two different things. Variables stick to their types where as methods get executed run time based on the implementation type provided.

Polymorphism. Methods bind dynamically and choosen at run time. If you ovveride them implementation class, they get executed otherwise the implementation from type class gets execute.

When you write

 A a1= new B();

Means that please call the implementations from the class B(which is on right side) which are from type A

0
Aditya W On

You have to know about overriding concept in java.

From oracle documentation page regarding overriding:

Overriding and Hiding Methods

Instance Methods

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed.

But overriding is different from hiding.

Static Methods

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

The distinction between hiding a static method and overriding an instance method has important implications:

  1. The version of the overridden instance method that gets invoked is the one in the subclass.
  2. The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.

Example to understand:

public class Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Animal");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Animal");
    }
}

public class Cat extends Animal {
    public static void testClassMethod() {
        System.out.println("The static method in Cat");
    }
    public void testInstanceMethod() {
        System.out.println("The instance method in Cat");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        Animal myAnimal = myCat;
        Animal.testClassMethod();
        myAnimal.testInstanceMethod();
    }
}

output:

The static method in Animal
The instance method in Cat
0
Homilzio Trovoada Santos On

It allways calls the method from the most specific class.