Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Method calling using polymorphism

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

======================================================


the above code works properly. I have one query, according to the law of polymorphism, the method calling should be:
when a parent class reference holds instance of child class, the child class method will be called.
But here the child class do not have the method "display", as it is declared private in parent class.
If this is working, then what is the rational behind this.?
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Zanwar wrote:
the above code works properly....
If this is working, then what is the rational behind this.?



The code, as you have shown, should *not* compile.

Henry
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!  

Rahul Zanwar wrote:the above code works properly

Are you sure? because void of display method of Parent is missing.

according to the law of polymorphism, the method calling should be:
when a parent class reference holds instance of child class, the child class method will be called.

Only if the parent class's method is overridden in the child class.

  • Here in your code, Child class doesn't extend Parent class

  • If this is working, then what is the rational behind this.?

    This will not work because, below code give compile time error Type mismatch: cannot convert from Child to Parent, because Child is not sub class of Parent. and although you add ; it gives compile time error which says The method display() from the type Parent is not visible, because it has private access modifier which means that method can only be accessed within Parent class but here you are trying to access it in Test class which is invalid. So this will not work.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic