The Artima Developer Community
Sponsored Link

Java Answers Forum
Protected method in an Interface

5 replies on 1 page. Most recent reply: Nov 16, 2005 10:47 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
JavaAdd Ict

Posts: 5
Nickname: javaaddict
Registered: May, 2004

Protected method in an Interface Posted: Nov 14, 2005 11:29 PM
Reply to this message Reply
Advertisement
Why can't I have a protected method in an Interface?


When I try to do so it says ".. modifier protected not allowed here .. ".


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Protected method in an Interface Posted: Nov 15, 2005 12:15 AM
Reply to this message Reply
Wouldn't make much sense, since the implementing class MUST implement all methods.

In these 2 examples I include the package in the class name for easier reading (hopefully)

public interface packageA.MyInterface {
    protected void methodA();
}
 
public class packageB.MyClass implements packageA.Interface {
    protected void methodA() {
        //...
    }
    //who should have access to this mehtod? Members of packageA od members of packageB?
}
 
A similar problem would occour if the interface itself would be protected
 
protected interface packageA.MyInterface {
    public void methodA();
}
public class packageA.MyClass1 implements packageA.Interface {
    public void methodA() {
    }
}
 
public class packageB.MyClass2 extends packageA.MyClass1 {
    
}
//This class would actually implement an interface it is not allowed to implement.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Protected method in an Interface Posted: Nov 16, 2005 4:23 AM
Reply to this message Reply
All methods declared in an interface are public; they cannot be reduced in visibility.

public interface Unspeakable {
   public horrify();
}

is the same as
public interface Unspeakable {
   horrify();
}

Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: Protected method in an Interface Posted: Nov 16, 2005 8:01 AM
Reply to this message Reply
Why would you *want* a protected method in an interface? Who would call it?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Protected method in an Interface Posted: Nov 16, 2005 10:40 PM
Reply to this message Reply
I think he wanted some method to be restricted to the package in which the interface lies.
But as I showed in the example (which won't compile, by the way), that would create problems, therefore the developers didn't make it possible

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Protected method in an Interface Posted: Nov 16, 2005 10:47 PM
Reply to this message Reply
But there is one way to do this. Seems I had an error in my second example, because this code does compile.

    public interface PublicInterface {
        public void publicMethod();
    }
    protected interface ProtectedInterface extends PublicInterface {
        public void protectedMethod();
    }


This way you can create a instance of the class implementing ProtectedInterface inside the package, but pass it as PublicInterface to whoever needs it outside the package.

The Class itself will offer potectedMethod, but as long it is handled as PublicInterface, no one will notice.

Note that I didn't test this to the end, so my whole theroy could be wrong. But I was able to compile this code.

Flat View: This topic has 5 replies on 1 page
Topic: Java & XML Previous Topic   Next Topic Topic: pls help me with my HW

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use