aspose file tools*
The moose likes Beginning Java and the fly likes Understanding inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding inheritance" Watch "Understanding inheritance" New topic
Author

Understanding inheritance

Bhaskar Mukhejee
Greenhorn

Joined: Sep 16, 2008
Posts: 14
Hi All,

I have started java recently. Got a snippet of a code. can anybody please explain this inheritance?
I have got 2 classes Test1 and Test2. Test1 inherits Test2.



The output is :
20
Executing method of Test1

but when I am replacing Test1 with Test2 The output is :
40
Executing method of Test1

It is showing accessing the instance variable of Test2 class but method of Test1 class why?

Please help. Thank you.

Regards,
Bhaskar
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Bhaskar Mukhejee wrote:

but when I am replacing Test1 with Test2 The output is :
40
Executing method of Test1
Is this what you have done.

Regards,
Bhaskar


So here t refers to Test2 But at runtime the instance is created of Test1 so it overrides the math method of Test1.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Bhaskar Mukhejee wrote:It is showing accessing the instance variable of Test2 class but method of Test1 class why?


It's because variables aren't overridden, only methods are.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
This FAQ might help you.
Bhaskar Mukhejee
Greenhorn

Joined: Sep 16, 2008
Posts: 14
Thanks to all of you for this help.

Regards,
Bhaskar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding inheritance