joy
joy

Reputation: 741

what determine the call of the override method

public class Superclass{

    void method1(){
        method0();
    }

    void method0(){
        System.out.println("superclass"); 
    }

}



public class Subclass extends Superclass{

    void method0(){
        System.out.println("subclass");
    }

}


public class Runclass{

    public static void main(String[] args){
        new Subclass().method1();
    }

}

This code print out - subclass.

Let say it is

new Superclass().method1();

then, it will print out - superclass

Is it the method called depend on the instance that call the method?

Upvotes: 0

Views: 1588

Answers (6)

Gayan Weerakutti
Gayan Weerakutti

Reputation: 13783

public class SubClass extends SuperClass {
    @Override
    public void print() {
        System.out.println("SubClass");
    }

    public static void main(String[] args) {
        SuperClass obj = new SubClass();
        obj.print();
    }
}

class SuperClass {

    SuperClass() {
        print();
    }

    public void print() {
        System.out.println("SuperClass");
    }
}

The above code will print:

SubClass
SubClass
SubClass

What is important to remember is that the method to be executed is determined by the JVM at runtime, based on the type of the actual object and not by the type of its reference variable.. Here, when an object of class SubClass is created, first SubClass's default constructor is called, which invokes SuperClass's constructor. SuperClass's constructor in turn calls print(). Here, since the class of the actual object is SubClass, SubClass's print is selected instead of SuperClass's print.

Upvotes: 0

gifpif
gifpif

Reputation: 4917

Its a example of Dynamic Method Dispatch (Run time polymorphism).
In Dynamic method dispatch call of an overridden method is resolve at the run time.
When u call

new Superclass().method1();  

the output is - superclass
Because Superclass does not have any information about subclass.
& when you call

new Subclass().method1();  

the output is - subclass
Because when overridden method is called, java determines which version of method to be execute based upon the type of the object being referred at the time of call occurs.

Upvotes: 2

Sushim Mukul Dutta
Sushim Mukul Dutta

Reputation: 777

Trying to explain it in a better way, when a function, say your method()is overridden in the subclass and you are calling method() with an instance of the superclass , the superclass object have no idea about the method() being overridden and it will run its own version of method(). In fact any changes made to the subclass is invisible to the superclass.Hence any instance of the superclass won't be able to access the subclass's overridden method().

Upvotes: 1

Azad
Azad

Reputation: 5055

Sub Class must extend Super Class :

public class Subclass extends SuperClass{

    void method0(){
        System.out.println("subclass");
    }

}

When calling SubClass().method() the priority of calling will be to subClass's method, the output is

subclass

But in this case :

public class Subclass extends SuperClass{

    }

The output will be :

superclass

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122008

Polymorphism.

May be you forgot this

public class Subclass extends ParentClass{

Untill unless you ovveride the method in child class it calls only parent method.

new Superclass().method1();

here I super class instance is there so super class method1 calls

And if you call

 new Subclass().method1();

subclasses ovveridden method1 calls.

And I would like to suggest this tutorial for clear picture.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

It depends on what object method is called. If the method is called on parent object, parents method will be called, but if the method is called on child object then child class method will be called. If method is not overridden then always parent class method will be called.

Upvotes: 1

Related Questions