Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does inheritance works?

 
goel Ashish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
randy orton
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Bill foster
Ranch Hand
Posts: 63
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic