Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the super class method called?

Tags:

java

class One { 
  public void doThing(One o) {System.out.println("One");}
}

class Two extends One{
  public void doThing(Two t) {System.out.println("Two");}
}

public class Ugly {
  public static void main(String[] args) {
    Two t = new Two();
    One o = t;
    o.doThing(new Two());
  }
}

Result : One

class One { 
  public void doThing(One o) {System.out.println("One");}
}

class Two extends One{
  public void doThing(Two t) {System.out.println("Two");}
}

public class Ugly {
  public static void main(String[] args) {
    Two t = new Two();
    One o = t;
    t.doThing(new Two());
  }
}

Result : Two

I know that at runtime, even though the object reference is of the super class type, the actual object type will be known and the actual object's method will be called. But if that is the case, then on runtime the doThing(Two t) method should be called but instead the super class method doThing(One o) is called. I would be glad if somebody explained it

In the second piece of code it prints "Two".

Question : when calling from the super class reference it is calling the doThing(One o) when calling from the sub class reference it is calling the doThing(Two o)

NOTE: I know that i am over loading and not over riding. i have edited my question for better clarity.

like image 565
Thirumalai Parthasarathi Avatar asked Sep 19 '13 12:09

Thirumalai Parthasarathi


People also ask

Why do we call super 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.

When super () is called?

3. Use of super() to access superclass constructor. As we know, when an object of a class is created, its default constructor is automatically called. To explicitly call the superclass constructor from the subclass constructor, we use super() .

Why super is called constructor?

We use super keyword to call the members of the Superclass. As a subclass inherits all the members (fields, methods, nested classes) from its parent and since Constructors are NOT members (They don't belong to objects. They are responsible for creating objects), they are NOT inherited by subclasses.

What is the super () method used for?

Definition and Usage The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.


2 Answers

The method doThing() have different method signature in One and Two.

One.doThing(One one)
Two.doThing(Two two)

Since, the signature isn't matched, Two.doThing(Two) doesn't Override One.doThing(One) and since o is of type One, One.doThing() is called.

Also to be noted that One.doThing(One) can take instance of Two as an argument for One.doThing(One) as Two extends One.

Basically, "@nachokk - You are Overloading, not Overriding"

In first scenario, when you did

Two t = new Two();
One o = t;
o.doThing(new Two());

So, o is an instance of One and Two.doThing(Two) isn't available to o thus calls One.doThing(One)

In second scenario,

Two t = new Two();
One o = t;
t.doThing(new Two());

t is an instance of Two and thus Two.doThing(Two) is called.

like image 179
TheKojuEffect Avatar answered Sep 19 '22 08:09

TheKojuEffect


The excelent book SCJP for Java 6 states:

If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you’re calling the supertype version of the method.

So basically with using supertype for reference you're telling compiler to use supertype method.

like image 42
Matej Avatar answered Sep 18 '22 08:09

Matej