-5
class Y{
    void process(){
        System.out.println("In Y process()");
    }
}

class I extends Y{
    void process(){
        System.out.println("In I process()");
    }

    public static void main(String[] args){
        Y y = new I();
        y.process();
    }
}

this code is based on overriding in java .but I have a doubt regarding this problem.when we call the process method using y reference variable then is the method call resolved at runtime or is it resolved by the compiler?please help me out with this problem that whether the JVM reolves which process method is to be called or is it the compiler?give reason for your answer.

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • 4
    Please take the time to format your code when posting. It's very hard to read without any indentation. Additionally, this topic has been covered multiple times - and at least *some* of it should be answerable just by the output of your test app. – Jon Skeet Jul 14 '17 at 16:00
  • Possible duplicate of [How to call the overridden method of a superclass](https://stackoverflow.com/questions/15668032/how-to-call-the-overridden-method-of-a-superclass) – Blasanka Jul 14 '17 at 16:01
  • Have you tried running the code? – Andrew Li Jul 14 '17 at 16:02
  • yes .I have tried running it and it doesn't show any error. – simren sen Jul 14 '17 at 16:04
  • Who mentioned anything about errors? If you ran your code, you should have the answer to your question. – shmosel Jul 14 '17 at 18:30

3 Answers3

0

The method is resolved at runtime, you are extending a class and this mean that this class can have the same methods as the parent class but the reference is resolved at runtime.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Ivan Lynch
  • 563
  • 5
  • 23
0

This is called runtime polymorphism.Here method of class I will be executed .Always reference type of object is checked at run time.Suppose process method is not present in parent class then that is overriding and it will generate error.

Ankit
  • 49
  • 1
  • 12
0

This call will resolve at runtime. And the process() method of I class will gets exucuted. The Superclass reference is refering to the child class object. So when you call the method using superclass reference who is refereing to the child class object, always child class method will gets executed.

sForSujit
  • 987
  • 1
  • 10
  • 24