aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding interface implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt regarding interface implementation" Watch "Doubt regarding interface implementation" New topic
Author

Doubt regarding interface implementation

Pad Ven
Ranch Hand

Joined: Apr 29, 2005
Posts: 38
Hi all,
I was thinking that when you implement an interface, you shouldf always prefix the "public" modifier before the return type of the method, and not any other modifier, and that this holds true even if the methods are declared with no modifier in interface declaration.

The answer to this very first question from K & B chapter 1 self-test explains thus:



1. Given the following,
1. interface Base {
2. boolean m1 ();
3. byte m2(short s);
4. }

Which code fragments will compile? (Choose all that apply.)
A. interface Base2 implements Base { }
B. abstract class Class2 extends Base {
public boolean m1() { return true; } }
C. abstract class Class2 implements Base { }
D. abstract class Class2 implements Base {
public boolean m1() { return (true); } }
E. class Class2 implements Base {
boolean m1() { return false; }
byte m2(short s) { return 42; } }

Answer:
C and D are correct. C is correct because an abstract class doesn't have to implement any or all of its interface's methods. D is correct because the method is correctly implemented.

A is incorrect because interfaces don't implement anything. B is incorrect because classes don't extend interfaces. E is incorrect because interface methods are implicitly public, so the methods being implemented must be public.(Objective 1.1)



As can be seen, the sentence in Italics was the reason behind my understanding. But the following program compiles even though "protected" modifier is used when implementing the interface:

[QOUTE]
interface Edible {
public double getCalories();
public boolean shouldBeCooked();
}

class RawVeg {
protected int calories;
protected boolean cooked;

public double getCalories() {
return calories;
}

protected boolean shouldBeCooked() {
return !cooked;
}
}

public class CookedVeg extends RawVeg implements Edible {

public boolean shouldBeCooked() {
return cooked;
}

public static void main(String[] args) {
final RawVeg veg = new CookedVeg();
Edible food = (Edible) veg;
System.out.println(food.getCalories());
}
}

[/QOUTE]


Am I missing something here? What is the explanation for this?

Thanks and regards,
TV Padmanabhan
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
Hmm! Thats really surprising.I thought it would give an error. But compiles fine. Hope some gurus can throw light on this.


SCJP 5.0 77%
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
The class CookedVeg which implements Edible interface has public shouldBeCooked() method and the public double getCalories() is inherited from class RawVeg. And since both methods are public you dont get any error.

But now try changing the acess specifier of getCalories() from public to protected. Now getCalories is inherited by CookedVeg class and getCalories method becomes private. And since you are reducing the visibility from public to private you get an error. Run this code and just have a look

marko salonen
Greenhorn

Joined: Nov 19, 2006
Posts: 8
Originally posted by Satish Kota:
The class CookedVeg which implements Edible interface has public shouldBeCooked() method and the public double getCalories() is inherited from class RawVeg. And since both methods are public you dont get any error.

But now try changing the acess specifier of getCalories() from public to protected. Now getCalories is inherited by CookedVeg class and getCalories method becomes private. And since you are reducing the visibility from public to private you get an error. Run this code and just have a look




No , its visibility is not private. If it was private the compilator would say that
CookedVeg is not abstract and does not override abstract method getCalories() in Edible
Protected is visible from the same package and from an extending subclass.
The problem is that you want to narrow the visibility to smaller smaller.

Why it works in the first case is that CookedVeg overwrites method shouldBeCooked with an wider visibility , public. Therefore when CookedVeg implements Edible it has implemented shouldBeCooked() by it self and implemented getCalories() in its subclass. Both methods are public and there are no compilation errors.
[ November 23, 2006: Message edited by: marko salonen ]
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
If a class inherits a class and implements a interface and both the interface and class has same method signature,the access modifier for the overriden method should not be more restrictive than what declared in interface means public.Consider the various cases...

Case 1:

Class Impl already has the inherited method protected void doNothin(){} which is more restrictive than what interface declares(always public).So compilation error.so make it compilable override in class Impl using public access modifier.

Case 2:

It is the interface access modifier(always public ) which rules.Overriding with the abstract modifier in Impl will break the conract with the interface.


~Sanjeev Singh<br />SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding interface implementation