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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does inheritance works?" Watch "How does inheritance works?" New topic
Author

How does inheritance works?

goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
I am confused with how inheritance works? Does the code of superclass gets copied into subclass or what else is the logic behind inheritance?
Lets take an example :-


In the above example does the code of class A gets copied into Class B or what else is the reason that we are able to access public members of class A into class B also.
What if we declare a instance variable of same name in subclass also??



Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I believe that the JVM is responsible for loading the superclass' code and creating an object with all members of the class hierarchy. I may be wrong about that. If you declare a variable in your subclass with the same name as an attribute from the superclass, the subclass variable will hide the definition of the superclass attribute, this is not a good thing to do.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
randy orton
Greenhorn

Joined: May 20, 2011
Posts: 15
astinct ast wrote:I am confused with how inheritance works? Does the code of superclass gets copied into subclass or what else is the logic behind inheritance?
Lets take an example :-


In the above example does the code of class A gets copied into Class B or what else is the reason that we are able to access public members of class A into class B also.
What if we declare a instance variable of same name in subclass also??


Hello.......
Superclass code along with subclass code is loaded into class loader.
Declaring the instance variable with the same name in subclass overrides the instance variable of superclass.
To refer to super class members use the keyword super.
Stan Giggs
Greenhorn

Joined: May 20, 2011
Posts: 2

"When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself."

http://download.oracle.com/javase/tutorial/java/IandI/subclasses.html
goel Ashish
Greenhorn

Joined: May 14, 2011
Posts: 21
randy orton wrote:
astinct ast wrote:I am confused with how inheritance works? Does the code of superclass gets copied into subclass or what else is the logic behind inheritance?
Lets take an example :-


In the above example does the code of class A gets copied into Class B or what else is the reason that we are able to access public members of class A into class B also.
What if we declare a instance variable of same name in subclass also??


Hello.......
Superclass code along with subclass code is loaded into class loader.
Declaring the instance variable with the same name in subclass overrides the instance variable of superclass.
To refer to super class members use the keyword super.

Suppose there is a variable 'i' declared into both the super class and subclass. then to which 'i' the inherited code of the superclass will refer to ??
I read somewhere that we private members are of the super Class are not inherited. So what does it mean? Are they not inherited or they are inherited but are hidden into the subclass.
randy orton
Greenhorn

Joined: May 20, 2011
Posts: 15
astinct ast wrote:
randy orton wrote:
astinct ast wrote:I am confused with how inheritance works? Does the code of superclass gets copied into subclass or what else is the logic behind inheritance?
Lets take an example :-


In the above example does the code of class A gets copied into Class B or what else is the reason that we are able to access public members of class A into class B also.
What if we declare a instance variable of same name in subclass also??



private members are not accessible outside its class and this applies to superclass too.
When you will run the above code.you will find the output to be 20 and not 50 because java runtime system will first check the instance of i in subclass.If it is there i will be printed otherwise it will refer to superclass for i.
Now remove i from subclass and run the code ,you will get an error because i is declared to be private in superclass and private members are not accessible outside its class.
abhilash pallerlamudi
Greenhorn

Joined: Jan 06, 2008
Posts: 4
astinct ast wrote:
randy orton wrote:
astinct ast wrote:I am confused with how inheritance works? Does the code of superclass gets copied into subclass or what else is the logic behind inheritance?
Lets take an example :-


In the above example does the code of class A gets copied into Class B or what else is the reason that we are able to access public members of class A into class B also.
What if we declare a instance variable of same name in subclass also??


Hello.......
Superclass code along with subclass code is loaded into class loader.
Declaring the instance variable with the same name in subclass overrides the instance variable of superclass.
To refer to super class members use the keyword super.

Suppose there is a variable 'i' declared into both the super class and subclass. then to which 'i' the inherited code of the superclass will refer to ??
I read somewhere that we private members are of the super Class are not inherited. So what does it mean? Are they not inherited or they are inherited but are hidden into the subclass.


Declaring the instance variable with the same name in subclass DOES NOT overrides the instance variables of superclass. Instance variables are specific to Class. Though, same name is used for variables in subclass separate memory is allocated for them in their own namespace on heap. private instance variables of super class can be accessed in the subclass using getters() (java bean specification). When accessing private members of the super class through getters(), we need the super class reference. subclass members can be accessed using subclass reference eventhough same name is used.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Welcome to the Ranch
Bill foster
Greenhorn

Joined: May 17, 2011
Posts: 13

Hi there follow greenhorn,

The inheritance works so that you can reuse code in the case the methods in the parent class.
This is a great attribute to O.O. programming you can have subclasses (child) call methods of a parent or Super class. Instead of writing the same code for each class, use extends keyword to do this (like you did in your program).

you might want to check out a Book called Head First Java they also take about encapsulation which just means that the data will be accessed from the methods only.
This provides security of your data. Also remember that the Class of a program is like a blueprint where it describes the methods that can interact with objects. Don't forget IS-A and HAS-A relationships as well (you need to know this to design classes that interact with each other).

These are important topics if you are going to pass the SCJP test to get certified. They are confusing but when you get more involved in designing your programs you will master this. You might also study U.M.L. as well this well also teach you in a visual way how classes relate to each other.

Any book authored by Cay Horstmann is also a great book, he is a great author.

Your friend,

Bill Foster
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does inheritance works?