• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Private method overriding

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the logic behind this output ?



Output : SubSuper

How?
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic