user1309724
user1309724

Reputation: 79

Call some methods from interface without override all the methods in JAVA

Friends, I have an issue in Java: I'd like to implement one structure but I'm facing some difficulty in doing it, can anyone help me.

interface samp1{
    method1()
    method2()
    method3()
}

interface samp2{
    method4()
    method5()
}
class Samp implements samp1,samp2
{
  // this class needs only method1 from interface samp1 and method 4 from interface samp2
  // I don't want to override all the methods from interface 
}

can anyone propose some solutions for this?

Is there any design pattern available for this? If so, please provide links to reference.

Thanks in advance.

Upvotes: 3

Views: 17570

Answers (6)

Kartik Soneji
Kartik Soneji

Reputation: 1246

Java 8 allows default methods in an interface, that are used if the particular method is not overridden when the interface is implemented. For example:

interface MyInterface{
    //if a default method is not provided, you have to override it 
    public int Method1(); 
    public default int Method2(){
        return 2;
    }
}

public class MyClass{
    public static void main(String args[]){
        MyInterface in = new MyInterface(){
            public int Method1(){
                return 0;
            }
        };
        //uses the implemented method
        System.out.println(in.Method1()); //prints 0
        //uses the default method
        System.out.println(in.Method2()); // prints 2
    }
}

Upvotes: 2

ylnsagar
ylnsagar

Reputation: 644

There is no other way than to implement all the methods in the interface, when you implement the interface. In the above class "samp" you are implementing "samp1" and "samp2" interface's, so you have to implement all the methods in samp1 and samp2 in samp class.

you said override methods from the interface. you don't override methods from interface, you just implement methods.

you can solve your problem by using abstract class.

abstract class AbstractSamp implement samp1,samp2{

 method1(){...}
 method4(){...}
}

and you can extend Abstractsamp in your samp class as below

class samp extends AbstractSamp{

// here you can extend method1() and method4() by inheritence and you can also    override   them as you want

}

Upvotes: 1

Aniket Inge
Aniket Inge

Reputation: 25705

interface MySpecializedInterface{
   public void method1();
   public void method4();
}

class YourClass implements MySpecializedInterface{

  // now you can implement method1 and method4... :|
}

Upvotes: 0

Brian Roach
Brian Roach

Reputation: 76908

An interface is a contract. It says "My class implements all of these methods".

See: http://docs.oracle.com/javase/tutorial/java/concepts/interface.html

If you don't want that, don't use an Interface.

Upvotes: 10

jlordo
jlordo

Reputation: 37813

Without having more context on what you are trying to achieve, this is what I suggest to do:

interface BaseSamp {
    void method1();
    void method4();
}

interface Samp1 extends BaseSamp {
     void method2();
     void method3();
}

interface Samp2 extends BaseSamp {
    void method5();
}

class YourClass implements BaseSamp {
 ....
}

Upvotes: 0

Dan D.
Dan D.

Reputation: 32391

Declare the class as abstract and you won't need to implement all the methods. However, please note that abstract classes cannot be instantiated.

Upvotes: 0

Related Questions