• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Chapter 5 Review Question 20 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


As fly is private in Bird, Pelican defines fly() as a new method.
When an object of Pelican is created why would fly method of Bird be executed as fly is not static? Aint it straight away??
 
author
Posts: 23928
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

Saumyaraj Zala wrote:
As fly is private in Bird, Pelican defines fly() as a new method.
When an object of Pelican is created why would fly method of Bird be executed as fly is not static? Aint it straight away??



The compiler chooses the version of the method to call based on the reference type. Since the reference is of Bird type, it will try to call the method of the Bird class. And as you mentioned, since the Pelican fly() method does not override (it hides) the Bird fly() method, it does not get called.

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

Now, this is printing "Pelican is flying". Can anyone explain what's happening with removal of private before fly method in abstract class?
 
Sushma Gurram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since fly is default now and default method's access is package, is the sub class fly() method overriding the abstract class fly() method?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sushma Gurram wrote:Since fly is default now and default method's access is package, is the sub class fly() method overriding the abstract class fly() method?



Correct. Since the method is now accessible from the subclass, the method is being overridden.

Henry
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sushma,
Welcome to CodeRanch!

As you are new so I added code tags to your code and formatted code with proper indentation . Always post code with proper indentation and put code tags around your code.
Click here ---> UseCodeTags to know how to use code tags, see now your code looks good and more readable
 
Sushma Gurram
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:Sushma,
Welcome to CodeRanch!

As you are new so I added code tags to your code and formatted code with proper indentation . Always post code with proper indentation and put code tags around your code.
Click here ---> UseCodeTags to know how to use code tags, see now your code looks good and more readable




Thank you  After posting, I realized that I can't edit the post.
 
Saumyaraj Zala
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Saumyaraj Zala wrote:
As fly is private in Bird, Pelican defines fly() as a new method.
When an object of Pelican is created why would fly method of Bird be executed as fly is not static? Aint it straight away??



The compiler chooses the version of the method to call based on the reference type. Since the reference is of Bird type, it will try to call the method of the Bird class. And as you mentioned, since the Pelican fly() method does not override (it hides) the Bird fly() method, it does not get called.

Henry



But the object in memory has a method which prints "Pelican". What understanding I have till now is that the reference type is only a kind of pointer, the method that is being defined in the object is what actually gets executed.
 
author & internet detective
Posts: 41415
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saumyaraj Zala wrote:But the object in memory has a method which prints "Pelican". What understanding I have till now is that the reference type is only a kind of pointer, the method that is being defined in the object is what actually gets executed.


Correct.And if the fly() method in Bird were protected, that's how it work. However, it is private. Which means the method isn't being overridden.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I don't quite understand this question. Compiler first check fly() exist in Bird, and JVM invoke the method base on which object that reference points to. Therefore, it invoke the fly() method in Pelican class. Private method here is only serve as a hidden method and not inherited by Pelican class. I don't understand why A is the correct answer.
 
Jeanne Boyarsky
author & internet detective
Posts: 41415
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Micheal,
Try thinking of it another way. Suppose the Pelican class didn't have a fly method. Then the code would clearly compile and print "Bird is flying", right?

Now pretend that the Bird class is a piece of paper. And to override a method, you have to put another piece of paper on top of the fly method in Bird. Well, we have a problem. The fly method in Bird is private. So Pelican can't see it to put the piece of paper there. Uh oh.

What this means in java, is that a method can't override another method if it can't see it. That's why Option A is correct here.
 
I am Arthur, King of the Britons. And this is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic