Your expectation is correct. The methods that's called depends on the runtime type of the object, not the type of the reference. It might be you missed compiling something, or had some basic problem like that.
Joined: Dec 27, 2008
Hi Greg Charles,
I compiled all the classes one by one again in the order of hierarchy and also tried in eclipse.. but still getting the same output as I had mentioned in C1 a is 1 instead of in C2 a is 2
Could you please try running it and give some pointer in figuring out where I'm getting it wrong.
Try adding the @Override annotation to C2.met1. You will see it will fail.
The reason is simple. C1.met1 has default (package) access. C2 is in a different package. That means that C2 does not have access to C1.met1, and it doesn't override the method but instead hides / shadows it. It's odd indeed, but you can compare it to "overriding" a private method.
Thanks Rob Prime and Greg Charles for spending your valuable time in replying to my query. I again worked on the code with the help of your replies. But still got some doubts. I'll put my doubts more detailedly.
I have written two cases to understand the inheritance and polymorphism concepts-
case1- public met1() is in C3 class and not in C2 class
case2 - public met1() is in C2 and not in C3
in both the cases met1() with default access is in C1 class
Q1- The method met1() with default modifer in C1 class is not visble in C2 or C3 . So in both the cases
above the public met1() in either C2 or C3 is not an override of met1() in C1 as met1() is not visble. I hope my understanding is correct here?
Now coming to the two cases output
We have a public met1() in C3 and the output is
in C3 a is 3 and in C3 a is 3
we have a public met1() in C2 then output is
in C1 a is 1 and in C2 a is 2
I'm getting confused with the concepts of inheritance and polymorphism analyzing these outputs.
Q2 - As I read in the book the sub class has the inherited super class method. so that is why in case 2 for
o33.met1() the output is in c2 a is 2. I hope my understanding is correct?
Q3 -but why is the output in case 2 for o13.met1() is in C1 a is 2.?
if it is as pointed out by Rob Prime - since met1() here is not an override then in case-1 also public met1() is not an over ride and there also output for o13.met1() should be in C1 a is 1 but there i'm getting it as in C3 a is 3.
I know i'm getting it some where wrong either the concept itself or my understanding of it..
I'm jammed with this from couple of days...could you please help me out..
A class can only override a method it can see. That's also why you can never override a private method, only declare a method with the same name and parameters.
In case 1, C1 and C3 are in the same package. That means that class C3 can see met1 and it therefore overrides it.
In case 2, C1 and C2 are in different packages. That means that C2 does not see met1 and does not override it but declares a new method that happens to have the same name and parameters.
Joined: Dec 27, 2008
Thanks Rob Prime
I understood the concept based on what you had replied.