5

Can a superclass variable access an overridden method of a subclass. For ex:

class A {
    void callMe() {
        System.out.println("Inside A");
    }
}

class B extends A {
    void callMe() {
        System.out.println("Inside B");
    }
}

class Dispatch {
    public static void main(String args[]) {
        A a = new A();
        B b = new B(); // Object of type B
        A r; // Obtain a reference of type A

        r = a; // Refers to A object
        r.callMe(); // Calls A's version of callMe()

        r = b; // Refers to B object
        r.callMe(); // calls B's version of callMe() and my question is on this
    }
}

I learned earlier that a superclass variable that is referencing a subclass object can access only those parts of an object that are defined by the superclass. Then how can the second r.callMe() call B's version of callMe()? It should only call A's version of callMe() again.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • No it shouldn't. Since the object is `B` (regardless of what the reference is), the object's method will be called. – Kayaman Mar 28 '17 at 06:46
  • Initially `r` has the reference of `A` so it is same as `A` then `r` has a reference of `B` so it is calling the `callme()` of `B`. – Haris Qurashi Mar 28 '17 at 06:49
  • Maybe this post about [dynamic and static types](http://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages) will help – Stefan Warminski Mar 28 '17 at 06:52
  • 1
    callme() **is** defined in A. But it' overridden in B, and since r is of type B, the overridden method in B is called. That's what polymorphism is all about. If you had a foo() method defined only in B, then the compiler wouldn't let you call r.foo(), because there is no foo() method defined in A, and the declared type of r is A. – JB Nizet Mar 28 '17 at 06:56

5 Answers5

2

...a superclass variable that is referencing a subclass object can access only those parts of object that are defined by superclass

That's not entirely correct. In the end, the runtime calls the actual type of the object, regardless of the reference type. So r.callme() will actually call callme() defined in B, because r is a B object.

new B();       // <-- The object in memory is of type B and its type never
               //         changes.
A a = new B(); // <-- The object in memory is of type B; the reference type
               //         is A. But that effectively does only matter at
               //         compile-time, I believe.

In the example above, B is called the object type, and A is called the reference type.

See Java Language Specification § 15.12.4.4:

Let X be the compile-time type of the target reference of the method invocation.

[...]

If the invocation mode is virtual, and the declaration in S overrides X.m (§8.4.8.1), then the method declared in S is the method to be invoked, and the procedure terminates.


Let me make a rough guess at what they mean by "access only those parts [...] defined by superclass":

class A {
    void doSomething() { }
}
class B extends A {
    void doAnotherThing() { }
}
A a = new B();
a.doAnotherThing(); // Not valid, because doAnotherthing()
                    // is defined in class B.

In order to call doAnotherThing(), a type cast must be used:

((B) a).doAnotherThing(); // Valid
Community
  • 1
  • 1
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
1

In your question

r=b;

now r catch "new B()" object.When u call r.callme() then run callme method in B class. Because r has B object.

Any program will throw a compile time error since reference type of super class doesn't have a method by the name of sub class.

As the example

class Animal {
  public void move() {
     System.out.println("Animals can move");
  }
}

class Dog extends Animal {
  public void move() {
     System.out.println("Dogs can walk and run");
  }

  public void bark() {
     System.out.println("Dogs can bark");
  }
 }

 public class TestDog {

  public static void main(String args[]) {
    Animal a = new Animal();   // Animal reference and object
    Animal b = new Dog();   // Animal reference but Dog object

    a.move();   // runs the method in Animal class
    b.move();   // runs the method in Dog class
    b.bark();
 }
}

Output

TestDog.java:26: error: cannot find symbol
  b.bark();
   ^
 symbol:   method bark()
 location: variable b of type Animal
 1 error
Priyantha
  • 4,839
  • 6
  • 26
  • 46
  • So over ridden methods (of subclass) can be called by object of superclass, but not the one which is not over ridden.....right? Also if we already know which function is going to be called then why do we say that this decision is made at runtime ....? – Akash Singh Apr 04 '17 at 06:48
0

Java methods are virtual, so the run-time (actual) type of the object determines which method is called, not the static (declared) type of the variable.

However, the static (declared) type of the variable determines which methods (and also fields) are visible, i.e. which methods you may call.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
0

I think you might be getting a little confused between Objects and variables. A variable can be thought of as a 'Pointer'. All it does is pointing to an Object.
E.g.

A var = newA(); // var -> Object A

Even though var1 is defined as type A, it can point to subclasses of that type.
E.g.

A var = new B(); // var -> Object B

Now, when you call a method on a variable, it calls that method on whatever object it's pointing to (even if the object is a subclass) E.g.

A var = new B(); // var -> Object B
var.someMethod(): // calls B.someMethod()

Even though var is of type A calling a method on it still invokes B.someMethod() because var points to Object B.

All the type of the Variable does is define what objects that variable can point to, so in are case all var being of type A means it can only point to Objects that are of type A or extend A.

I hope this helps! :)

Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43
0

Because, this is how method lookup work in Java.

  • First, the variable is accessed. in your case r.
  • Then, the object stored in the variable r is found which is b.
  • The class of the object is found which is B.
  • The class is searched for a method match. Now, B contain callMe() method. So, this method is executed instead of superclass callMe() method. If callMe() was not overidden, then A's callMe() method would have executed.

Also, this is required for polymorphism. For example: Let say, you have a super class Fruit with subclass Apple and Grape.

public class Fruit{
    public String getName() {
        return "Fruit";
    }
}
public class Apple extends Fruit{
    private String name;
    public Apple(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}
public class Grape extends Fruit{
    private String name;
    public Grape(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}

Now, you want to store instances of both Apple and Grape in same list.

Fruit apple = new Apple("apple");
Fruit grape = new Grape("grape");

ArrayList<Fruit> fruits = new ArrayList<Fruit>();
fruits.add(apple);
fruits.add(grape);

Now, you want to iterate through each item of fruit and get their name.

fruits.forEach(item -> {
    System.out.println(item.getName());
});

In this case, the type of item is Fruit and calling getName: you want to have name from respective child class Apple/Grape instead of "Fruit" value from Superclass Fruit.

Ra Ka
  • 2,995
  • 3
  • 23
  • 31