0

I'm trying to understand the object reference created in SuperClass and SubClass. I read a lot online but still not able to figure out which reference is called. Here is my code.

public class X {
    public void xCall() {
        System.out.println("X method called");
    }

    public void move(){
      System.out.println("I'm in parent class");
   }
}

public class Y extends X{

    public void yCall() {
        System.out.println("Y method called");
    }

    public void move(){
      System.out.println("I'm in child class");
   }
}

I tried to making following objects and got the result as shown.

    X a = new X();
    X b = new Y();

    a.move();// runs the method in X class

    b.move();//Runs the method in Y class

Output :

I'm in parent class
I'm in child class

My question is why

 b.move();

is giving output

`I'm in child class`

instead of

`I'm in parent class`

My question is different from the one marked as duplicate of. As I understand that it will refer the superclass not the subclass. My question is then why is it giving me the result of method in the subclass?

Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46
  • 1
    Don't mix compile-time behavior with runtime behavior. – M A Dec 17 '15 at 16:53
  • @manouti I'm clear about compile time error and runtime error. I just mentioned it there to tell when I'm writing `b.yCall()` its not accepting it. Its giving me error there itself. – Harshita Sethi Dec 17 '15 at 17:07
  • b.move() execute before b.yCall(), so it displays the result in runtime. Of course you can not compile this code because not found method – nofomopls Dec 17 '15 at 17:15
  • This is an exact duplicate. Methods are resolved at compile time based on the reference expression used to invoke them. `b` is of type `X`. `X` does not declare a method named `yCall`. The rest is polymorphism and late binding and is unrelated. – Sotirios Delimanolis Dec 17 '15 at 17:16
  • The first line of the accepted answer: _When you declare a variable as having the type of the superclass, you can only access (public) methods and member variables of the superclass through that variable._ – Sotirios Delimanolis Dec 17 '15 at 17:17
  • That's just a way to explain what I'm trying to ask. Please don't misinterpret. I know It will not find the method `yCall` as it is a reference to class `X` but my problem is why is it reading `move()` from class `Y` instead of class `X`? – Harshita Sethi Dec 17 '15 at 17:18
  • 1
    That's polymorphism. That's runtime late-binding. That's the whole point of Object Oriented languages. – Sotirios Delimanolis Dec 17 '15 at 17:19
  • And you also state _This is overriding. I understood._ So I'm not misinterpreting anything. You seem to understand what's happening for that case. – Sotirios Delimanolis Dec 17 '15 at 17:19
  • @SotiriosDelimanolis the content may be the same. But my question is different. What solution I'm asking for is not given in that question. – Harshita Sethi Dec 17 '15 at 17:20
  • The answer is there, right in the accepted answer. Cast your expression to the appropriate subtype to have access to its methods. `((Y)b).yCall()`. – Sotirios Delimanolis Dec 17 '15 at 17:20
  • @SotiriosDelimanolis Again I repeat why `b.move()` reads `Y's move()` method rather than `X's move()` method. – Harshita Sethi Dec 17 '15 at 17:25
  • Yah you earlier said you understand why. `b` is referencing an object of type `Y`. So which one is it. Your question is very confusing. You've changed it twice now. – Sotirios Delimanolis Dec 17 '15 at 17:29
  • @SotiriosDelimanolis I understand why `yCall` cannot be refered through `b`..As b is reference of `X` and not `Y`. I just dont understand why `b.move()` is going in Y's move() not X's move(). – Harshita Sethi Dec 17 '15 at 17:36
  • What are you referring to when you say _This is overriding. I understood._? – Sotirios Delimanolis Dec 17 '15 at 17:39
  • Also `b.yCall();` doesn't give you any output as it won't compile. Your question currently makes no sense. Please clarify it. – Sotirios Delimanolis Dec 17 '15 at 18:00
  • @SotiriosDelimanolis I modified my question to make to it clear what i want to ask. – Harshita Sethi Dec 18 '15 at 05:55
  • Ok. Now your question is a duplicate of [this](http://stackoverflow.com/questions/19840080/how-does-inheritance-and-polymorphism-work-in-this-situation), or [this](http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading), or [this](http://stackoverflow.com/questions/22391915/late-binding-in-java), or [this](http://stackoverflow.com/questions/28809274/java-polymorphism-late-binding-rules), or any of hundreds of similar questions on Stack Overflow. The concept behind all this is the basis of all Java and would be covered in any tutorial or book. – Sotirios Delimanolis Dec 18 '15 at 06:05

1 Answers1

0

Your move method is part of the X class, while yCall is not.

You're telling in your code that you have a b Object of type X, so there is no way to call a method on it, which is not a method from X or from one of its parent classes.

You would have to cast it to an Y object .

Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • That's what I'm confused about. When I'm not aloud to call method `yCall` using `b` then how come I'm getting result of method `move` from class `Y` rather than class `X`? – Harshita Sethi Dec 17 '15 at 17:04