• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance and private methods  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m not sure i understand why the output of this code is the way it is aka Person Person .The first is clear , new Person().printPerson(); displays Person but for the second : new Student().printPerson(); it accesses the Student constructor that points to the Person class => object. It builds the Person instance then goes back to the Student constuctor .Both methods are private and to my knowledge invisible one to the other , except that you cant run the the Person one because it's private so the only one in the Student class is the Student one . Guess it 's incorrect , but why ? (is because private methods cant be overriden and somehow the super class one always has priority ? , even if it's private?)

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both objects access the public printPerson() method. What's not clear?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getInfo method in Person is private. The getInfo method in Student does not override the method in person, they are completely separate. That is why only the Person version of the method is being called.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only inherit non-private, non-static members. But ever if Person getInfo() were public, you'd still get Student's getInfo(). Try it.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You can only inherit non-private, non-static members. But ever if Person getInfo() were public, you'd still get Student's getInfo(). Try it.


Almost, the rule has to do with accessibility. Since the subclass can't access the super class private method, no inheritance will occur.

This means that if the Student class getInfo() method had been package private, meaning default access, then inheritance will occur with subclasses within the same package -- but not with subclasses in different packages.

Henry
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Both objects access the public printPerson() method. What's not clear?


I think i understand , the method print printPerson() (in Person class) is executed and it can only run the local private method ... Aka if the printPerson method was in the Student class it would display Student ?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. An object is always going to execute its "local" (in the same class) method even if inheritance were happening.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for clearing it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!