File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 38020
    
  22
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
 
Similar Threads
Code within a curly braces
Constructor
Class Reference
instanceof question?
Super class constructor calls derived class overridden method?