aspose file tools
The moose likes Beginning Java and the fly likes Private method overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Private method overriding" Watch "Private method overriding" New topic
Author

Private method overriding

Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
Whats the logic behind this output ?



Output : SubSuper

How?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Bernhard Waidacher
Greenhorn

Joined: Aug 06, 2011
Posts: 3
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Private method overriding
 
Similar Threads
interface rule
interface
Object orientation
A question about runtime polymorphism.
Question regarding the flow of execution with static blocks