Win a copy of Modern frontends with htmx this week in the Spring 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B7 Chapter 1 Self-test Question 6 (CH1Q6)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , Following, code is taken from K&B 7 chapter 1 Self test Question 6.


the answers is Compilation Succeeds.

Here, what i am trying to understand is ,
1. why line 7 is not failing, i mean compile time . the reason i am thinking this should fail is, because the class Phone3 is not declared abstract.
2.why line 5 is not failing , again compile time, i thought while over ridding even method signature and return type has to be identical to super class method, in this case, the subclass is allowing a parameter x.

i think its an errata in K&B7 , the answers should be at least E ( line 7 fails to compile). please correct me if i am wrong.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those code samples from mocks/books etc are poorly formatted, aren't they.

Below is the same code in more readable form:Why do you think class Phone3 should fail do compile? Why do you think it should be declared abstract?
Note that doStuff method has a body. It is empty but that is not a problem.

In Phone2 class method doIt is not overriden. It is overloaded. You can know that the method is overloaded if it uses different parameters.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

babu thannasi wrote:i think its an errata in K&B7 , the answers should be at least E ( line 7 fails to compile). please correct me if i am wrong.


No, you are both times wrong! The book is spot-on! The above code compiles successfully (no compiler errrors). Read on to know why

babu thannasi wrote:1. why line 7 is not failing, i mean compile time . the reason i am thinking this should fail is, because the class Phone3 is not declared abstract.


Why do you think class Phone3 has to be abstract? It implements the Device interface which defines the doIt() method. So in order to be a valid concrete class, Phone3 has to provide at least an implementation for this method. It can implement this method either directly (implementation defined in the class itself) or indirectly (implementation defined in a class it extends from). Have a look at the code snippet again and I'm sure you'll why class Phone3 has already an implementation for the doIt() method.

babu thannasi wrote:2. why line 5 is not failing , again compile time, i thought while over ridding even method signature and return type has to be identical to super class method, in this case, the subclass is allowing a parameter x.


If you want to override a method, you indeed have to use the same parameter list (and the same or a covariant return type). But this is not an override, it's an overload. And overloaded methods must have a different parameter list, so method doIt(int x) is a valid overload of method doIt(). So class Phone2 has 2 doIt methods: one with no parameters and another one with an int parameter.

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Those code samples from mocks/books etc are poorly formatted, aren't they.


True! They have to prepare you for the actual exam where you can't press a format button to have nicely formatted and indented code
 
babu thannasi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Pawel & Roel , it makes sense to me .
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic