• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Sierra/Bates SCJP v6 - Interfaces and overridden methods

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


My query is drawn from Question 46 of the 2nd mock exam on the CD. The answer notes that to allow compilation that the method on line 4 of Lame.java must be declared public. (full details below for reference)

As this is an override why can you not implement a concrete method with a lesser or equal level of access.
1. Assuming the interface is public (i.e. in fulfillment of Answer A) the method could be public, protected, default or private. As it is already default, making accessible to the Lamentable class, any subclass or any other class in the same package. Why must it also be made public - it is not clear from the question that this method need be accessible more widely than its within its class.

2. Then on a more general note - what if I dont want the Lame version of lament to be more accesible than default allows? For example if any other classes which implement Lamentable have their own lament methods. Is there perhaps an underlying reason that this may create later code problems?


Question 46
Given two files:





In order to compile these two files (which are both located in appropriate directories for their
packages), what changes are necessary?

Correct Answer
A: On line 3 of Lamentable.java, this interface must be declared public.
D: Method on line 4 of Lame.java must be declared public.
G: Lines 12-14 in Lame.java must be deleted.
H: Lines 16-18 in Lame.java must be deleted.

Regards
Scott
 
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Scott Gordon Sutherland wrote:Hi


My query is drawn from Question 46 of the 2nd mock exam on the CD. The answer notes that to allow compilation that the method on line 4 of Lame.java must be declared public. (full details below for reference)

As this is an override why can you not implement a concrete method with a lesser or equal level of access.
1. Assuming the interface is public (i.e. in fulfillment of Answer A) the method could be public, protected, default or private. As it is already default, making accessible to the Lamentable class, any subclass or any other class in the same package. Why must it also be made public - it is not clear from the question that this method need be accessible more widely than its within its class.

2. Then on a more general note - what if I dont want the Lame version of lament to be more accesible than default allows? For example if any other classes which implement Lamentable have their own lament methods. Is there perhaps an underlying reason that this may create later code problems?


Question 46
Given two files:





In order to compile these two files (which are both located in appropriate directories for their
packages), what changes are necessary?

Correct Answer
A: On line 3 of Lamentable.java, this interface must be declared public.
D: Method on line 4 of Lame.java must be declared public.
G: Lines 12-14 in Lame.java must be deleted.
H: Lines 16-18 in Lame.java must be deleted.

Regards
Scott



Hi!
Remember the overriding method can be LESS restrictive than the original method, but canNOT be more restrictive. The void lament() method in the interface is PUBLIC ABSTRACT by default regardless of what is listed (in our case public and abstract have been omitted).
So a concrete method can be ONLY PUBLIC, because default, private or protected are MORE restrictive than the public method in the interface.
 
Boris Mechkov
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also on your point 2:
If you wanted a LAMENT method that is more restrictive than what the interface allows, just overload the method and do some in-method coding to make it work similarly to the overridden method. This might be a solution.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is all about the Liskov Substitution Principle. This states that any place where an instance of a particular class is used, you should be able to substitute an instance of any subclass for it. Allowing a method to be overridden with greater restrictions breaks this principle. For example:

That code should be valid. On line 2, the compiler sees the reference type is Lamentable, and knows that it has a public lament method. But the object is actually a Lame, and that doesn't. If we allow the class to be defined with more restrictive accessibility, then we've broken compile-time protection against using invalid methods.

Making methods more accessible when you override (which doesn't apply here, because all interface methods are implicitly public) doesn't break this principle, and is allowed. An example is Object.clone() - it's protected, but when making an object cloneable you're expected to override it and make it public.
 
Scott Gordon Sutherland
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew

Thanks - this is the type of information I am looking for.
 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I just remark that Lamentable is my favorite interface example I've ever seen?
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!