1
class A2 {
    int numt=111; 
  void hello() {

    System.out.println("Hello from A2");
  }
}

class B2 extends A2 {
  void hello() {
    System.out.println("Hello from B2");
  }
}

class C2 extends B2 {
  int numt=666;  
  void hello() {
    System.out.println("Hello from C2");
  }


}

class MethodOverriding2 {
  public static void main(String args[]) {
    A2 obj = new C2();
    obj.hello();
    System.out.println("Num is : " + obj.numt);
  }
}

So basically the output here is

Hello from C2
Num is : 111

Why does it run hello() from C2 but numt from A2?

As I understand, I'm inheriting A2 into B2, and C2 into B2. Then in my main I'm creating an object of class A2 which refers to C2 (which it can do since A2 is a superclass of C2). Then in compile time there is no error as the code is satisfied. At runtime, the program looks for 'hello()' where 'obj' is referring to and not where it is defined. But it does the opposite for 'numt'. Why is this so? And is my above understanding correct? Please correct if it isn't.

Thanks a lot! I know it's a novice question, I'm very new to OOP.

4 Answers 4

4

When you call method, the method in the instance will be called, not on reference type due to polymorphism. Here your object is of C2 even though reference is of type A2, so method in C2 will be invoked.

When you access variables it is on reference type than object type because polymorphism doesn't applicable for fields. That is why it is always good design to make your variables private so that other classes can't directly access these variables.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 I will just add that polymorphism works only for methods that are not final, static or private and it doesn't work for fields (that is why OP gets 111 not 666).
Okay, so ` A2 obj = new C2();` means obj refers to A2? I thought it refers to C2.
Yes, obj refers to C2, so obj is reference OF TYPE A2. But, the real object is of type C2().
@user1265125 Yes, obj refers to A2. This is what A2 obj means. However, because A2 can be extended, the concrete class to which obj refers can be an object of any of A2's subclasses.
0

In first case you overriden method hello(); in C2 so it prints: Hello from C2

In second case you didn't assign any new value to instance variable int numt so it prints:111. To assign new value to var. numt do this:

obj.numt=666;

Comments

0

Actually In java, member variables are not polymorphic. So if you use obj.numt, there will be no compiler error. But if your obj is a reference of B2, then you will get compiler error.

In case of methods, those are polymorphic. So if it is object of A2, A2.hello() will be called. If it is object of B2, B2.hello() will be called. Same in case of C2.

class MethodOverriding2 {
  public static void main(String args[]) {
    // class member variables are not polymorphic(i.e. they are not inherited)
    A2 obj = new C2(); // obj is of type A2, so obj.numt would print 111
    B2 obj1 = new C2(); // obj1 is of type B2, so obj1.numt would give compiler error.
    C2 obj2 = new C2(); // obj2 is of type C2, so obj2.numt would print 666
    obj.hello(); // would call C2.hello() as obj points to object of C2

    A2 obj3 = new B2(); // obj3 is of type A2 which points to object of B2
    obj.hello(); // this would call B2.hello()
  }
}

Comments

0

You can't override a field the way you override methods. They are not polymorphic, so you always will get the value specified for the type you are accessing the field through. You can compare it with "final" methods for example, they behave in similar way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.