Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Super keyword in Java, interesting behavior, please explain

Tags:

Lets say that we have the following code:

class A {      public void doLogic() {         System.out.println("doLogic from A");     } }  class B extends A {      @Override     public void doLogic() {         System.out.println("doLogic from B");     }      public void doDifferentLogic() {         System.out.println("doDifferentLogic from B");         super.doLogic();     } }  class C extends B {      @Override     public void doLogic() {         System.out.println("doLogic from C");     } }  public class Test {      public static void main(String[] args) {         C c = new C();         c.doDifferentLogic();     } } 

When we execute this code the expected behavior is the following: Since c holds a reference to object of class C, when you invoke the c.doDifferentLogic() method the JVM searches for the method in the C class and since it is not found it starts looking at the inheritance tree. As expected the doDifferentLogic() method is found in the super class and executed. However the construct super.doLogic() is expected to look from the current reference "Point of View", which is of type C. So the super of C should B, but instead the method from the top class A is invoked.

If you remove the super keyword, or replace it with the this keyword (which is the same as "this" is implicit), you get the expected polymorphic behavior and the doLogic() from C class is invoked.

So my question is: Should call to super.doLogic() be this.super.doLogic()(2), instead of static.super.doLogic()(1) ?

Both are invalid constructs, they are here just to try to explain myself better.

(1)or in other words - from the reference to the current object c , get the superclass of the current object and invoke the doLogic() method instead of (2)from this class get the superclass and invoke its doLogic() method ?

like image 601
Антон Антонов Avatar asked Feb 26 '14 17:02

Антон Антонов


People also ask

What is the purpose of the super keyword in Java?

The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.

Why do we use super keyword explain advantage?

We could access maxSpeed of base class in subclass using super keyword. 2. Use of super with methods: This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword.

What are the 3 uses of super keyword?

Uses of super keyword To call methods of the superclass that is overridden in the subclass. To access attributes (fields) of the superclass if both superclass and subclass have attributes with the same name. To explicitly call superclass no-arg (default) or parameterized constructor from the subclass constructor.

What are the use of keyword this and super in Java explain with example?

this keyword mainly represents the current instance of a class. On other hand super keyword represents the current instance of a parent class. this keyword used to call default constructor of the same class.


1 Answers

In Java, the super keyword always refers to the superclass of the type in which the keyword is used, not the superclass of the dynamic type of the object on which the method is invoked. In other words, super is resolved statically, not dynamically. This means that in the context of class B, the super keyword always refers to class A, regardless of whether the B method is executed using a C object as the receiver. To the best of my knowledge, there isn't a way to dynamically determine the superclass type and use its methods without using reflection.

Hope this helps!

like image 169
templatetypedef Avatar answered Oct 29 '22 17:10

templatetypedef