• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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

 
Ranch Hand
Posts: 75
AngularJS Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Saloon Keeper
Posts: 1183
74
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
AngularJS Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 39054
714
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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: 39054
714
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
That new kid is a freak. Show him this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!