aspose file tools*
The moose likes Beginning Java and the fly likes inheritance and polymorphism across packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritance and polymorphism across packages" Watch "inheritance and polymorphism across packages" New topic
Author

inheritance and polymorphism across packages

ajithkumar punnakula
Greenhorn

Joined: Dec 27, 2008
Posts: 8
I wrote the following four classes while learning inheritance and polymorphism across different packages





as I read in the book the sub class has the inherited super class method..
so in this case C3 class has the C2 super class's method met1();

so i expected according to polymorphism the o1.met1() in class R to give the in C2 a is 2 output but it gives in C1 a is 1

could you please explain the above output.

Thanks in advance
Ajith
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

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.
ajithkumar punnakula
Greenhorn

Joined: Dec 27, 2008
Posts: 8
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.

Thanks
Ajith
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ajithkumar punnakula
Greenhorn

Joined: Dec 27, 2008
Posts: 8
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

case1-





case 2-





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

case1 -
We have a public met1() in C3 and the output is
in C3 a is 3 and in C3 a is 3

case2 -
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..

Thanks in advance ..
ajith
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

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.
ajithkumar punnakula
Greenhorn

Joined: Dec 27, 2008
Posts: 8
Thanks Rob Prime

I understood the concept based on what you had replied.

Thanks a lot for your time
Ajith
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

You're welcome.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Please avoid coloured text; it is difficult to read.
ajithkumar punnakula
Greenhorn

Joined: Dec 27, 2008
Posts: 8
My idea was to make the identifiers and output clearer from the rest of the Question, but I can see it is making it difficult to read.
Will not use that in future, thanks for pointing it out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance and polymorphism across packages