Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Saumyaraj Zala
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??
 
Henry Wong
author
Marshal
Pie
Posts: 22108
88
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
 
Sushma Gurram
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
Marshal
Pie
Posts: 22108
88
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
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic