• Post Reply Bookmark Topic Watch Topic
  • New Topic

Private method overriding  RSS feed

 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the logic behind this output ?



Output : SubSuper

How?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private methods cannot be overridden. Class b merely has a method that happens to have the same return type, name and parameters. Class b is unaware of the printit method of class a and vice versa. Therefore, when p calls printit in class a it does not call the printit method from class b (even though it's a subclass) but it calls the printit method from class a instead.
 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when p calls printit(),the object calling it is of class B.
Therefore it calls this.printit() where this is an object of class B.Hence it is like B.printit() and not A.printit() as there is no relationship between printit() of A and B.Please explain???
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that class names should start with an uppercase letter. I found it your code hard to read as it doesn't follow this convention.

Also don't do Do:
 
Bernhard Waidacher
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohnish Khiani wrote:when p calls printit(),the object calling it is of class B.
Therefore it calls this.printit() where this is an object of class B.Hence it is like B.printit() and not A.printit() as there is no relationship between printit() of A and B.Please explain???


There is no relationship becuase both printit are different methods.
Printit in class A is private, so the printit in B is a new method, and it does not override printit from A.
Both printit has just the same names. If you would give them different names, you would get the same result.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!