• Post Reply Bookmark Topic Watch Topic
  • New Topic

inheritance in java  RSS feed

 
Lakshmi Sarvani Videla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does child class gets a copy of the methods and variables of parent class?
public class test1
{
public static void main(String a[])
{
Child c = new Child();
c.print();
}
}
class Parent
{
int id =1;
void print()
{
System.out.println(id);
}
}
class Child extends Parent
{
int id=2;
}

why is the output 1?
 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output is 1, because print method is not override in child class. So if method is not found in child class, call goes to parent class.
 
Lakshmi Sarvani Videla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as the child class inherits the method of parent class, it must also have a print method and it must consider the attribute id (overrided)=2
please clarify.
does 2 copies of id variable exists or one?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lakshmi Sarvani Videla wrote:as the child class inherits the method of parent class, it must also have a print method and it must consider the attribute id (overrided)=2
please clarify.


Interesting argument, but obviously, not true. Instance variables are not polymorphic, so the class type (at compile time) is used to determine which instance variable to use. And the parent's print() method, has a "this" reference variable, that is of type Parent (at compile time).

Henry
 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The child can inherit all the methods and fields from its parent, but can then do its own thing. But you are not providing any definition for print method in child. Parent don't know about the child, so when call goes to print method of parent, it print id of parent.
Yes there are two instances of id variable exist. If you want to print child class id value, you can do like this :
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lakshmi Sarvani Videla wrote:
does 2 copies of id variable exists or one?


Sorry. Didn't answer the question. Yes, the child class instance has two instance variables with the name of id -- and your example accesses the one defined in the parent class.

Henry
 
Lakshmi Sarvani Videla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Mr. Henry Wong and Pratik Jain
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and welcome to the Ranch
 
Lakshmi Sarvani Videla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If possible please suggest me online material or a book to study and understand java compilation and running not just its syntax
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with our FAQ.
 
Jain Pratik
Greenhorn
Posts: 14
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to learn from basics i suggest you Head First Java by Kathy Sierra. Its a good book to start , may be other can suggest some more good materials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!