Question

I have two methods in an interface:

public interface MyInterface {  
    public void methodOne(); 
    public void methodTwo();
}

public class MyClass implements MyInterface{    
    public void methodOne(){
       //implementation code    
    }

    public void methodTwo(){
       //implementation code    
    } 
}

Can I restrict one of them while implementing the interface?

Was it helpful?

Solution

You must implement all methods of your interface, unless the class implementing the interface is abstract.

If by restrict you mean that you want to predefine one or more of the methods, then you can use an abstract class instead of the interface. Abstract methods in an abstract class are methods that must be implemented by any class that extends the abstract class. Non-abstract methods are actually implemented in the abstract class, itself.

For example,

public abstract class MyClass 
{  
    abstract void methodOne(); 

    void methodTwo()
    { 
       //implementation code
    }
}

public class MyOtherClass extends MyClass
{    
    void methodOne()
    {
       //implementation code    
    }
}

Here's a reference for Abstract Classes and Methods.

EDIT 1 (in response to comment):
I'm not really sure what you mean by a burden. All I'm saying is that if you want all methods to be implemented by the class, then use an interface.

If you only want some of the methods implemented by a class, then you can either use an abstract class instead of the interface

or

If it makes sense, have an abstract class implement the interface (partially) and then have the remainder of the methods implemented by whatever extends the abstract class.

Both approaches are reasonable. It depends on what you really need to do.

EDIT 2 (in response to additional comments):
Providing one user class with additional features seems like the perfect application for just extending the "normal user class" with a "super user" class that has the additional features. If you need an interface for the "super user" class, you can create an interface that extends the interface implemented by the "normal user" class.

OTHER TIPS

No you have to implement all methods if your class implements an interface.

Unless it is abstract.

Check this discussion for more details.

You can use an abstract class; they do not need to implement all the entire interface. However, they cannot be instantiated. You must implement all methods in order to instantiate an implementation of an interface.

Except the abstract class, you are bound to implements all methods of interface.

You can not put restriction on Interface, but YES you can achieve In Abstract class and NO in concrete class.

Couple of solutions :

  • Either mark the class as abstract. But in that case you cannot instantiate your implementing class.
  • Simply add black implementation in your class.

Not sure what you mean by "restrict". You have to implement the method. However, you could simply do

public void method1(someargs) {
   throw new UnsupportedOperationException();
}

as is done by many Collection implementations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top