• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
babu thannasi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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.
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks Pawel & Roel , it makes sense to me .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic