Inherit hidden methods

I have an interface that has 4 methods and a class that implements the interface. Here comes the question: "How can I inherit from the interface only 2 of those methods and my class don't became abstract?"

interface Class1 {
    void method1();
    void method2();
    void method3();
    void method4();
}


public class Class2 implements Class1 {

    @Override
    public void method1() {

    }

    @Override
    public void method2() {

    }
}

Upvotes: 1

Views: 82

Answers (5)

Jim W
Jim W

Reputation: 512

You can't do that. It comes down to what the implements keyword implies about a class.

An instantiable class cannot implement an interface without having all the methods of the interface implemented. If you don't implement all the required methods, you have to declare the class abstract or you have to remove the implements declaration.

Upvotes: 0

Sandy Simonton
Sandy Simonton

Reputation: 607

You have to get tricky, and you have to lookup why this works, especially if it's an interview question. It's basically for compatibility (the default methods in the interface), and requires Java 8.

public interface One {
  void method1();
  void method2();
  void method3();
  void method4();
}

public interface Two extends One{
  default void method1(){}
  default void method2(){}
}

public class Three implements Two{

  @Override
  public void method3() {}

  @Override
  public void method4() {}

}

Non-abstract Three.class implements method3 and method4 of One.class without defining method bodies for method1 and method2. Method1 and Method2 are defined with default implementations in interface Two.class.

Upvotes: 1

Mathews Mathai
Mathews Mathai

Reputation: 1707

An interface is used when you want a set of programs to follow a certain trend or acquire a common set of properties. These properties are declared as methods in the interface. An interface can have abstract methods only and it is compulosory to inherit these methods and define them some where down the inheritance line.

An abstract method would look like:

public void hello();

It has no method body. You need to inherit it and define the method body.

Let us consider an interface animal.

public interface animals
{
 public void walks();
public void eats();
public void sleeps();
public void dog_breed(); 
public void barks();

}

Let us consider 2 classes named Jimmy_the_dog and Tom_the_cat.

We would want the these 2 classes to implement the interface animal to give it the properties of animals. But the problem is with the abstract methods barks() and dog_breed() in the interference. A dog can have all the properties mentioned in the interface animal but it does not make sense for a cat to inherit the methods barks() and dog_breed().

This is where we will split the interface. Here, we will split the animal interface to a dog interface and animal interface. Therefore, the properties in interface animal would become more common to animals in general.

public interface animals
    {
     public void walks();
    public void eats();
    public void sleeps();
    }


public interface dog
    {
     public void barks();
    public void dog_breed();

    }

How to work around with the above two interfaces?

  • public class Tom_the_cat implements animal

    • public class Jimmy_the_dog implements animal implements dog

Jimmy_the_dog implements both the interfaces to acquire dog specific properties. Any animal which is a dog can do so. Similarly, you could make cat specific interfaces too for all the cats in the world.

The above interface could work in the following manner too:

  • public interface dog extends animal
  • public class Jimmy_the_dog implements dog

Jimmy_the_dog gets all the animal and dog properties.

Note:

A class can extend a single class only but it can implement multiple interfaces.

Upvotes: 0

Rodrigo Del C. Andrade
Rodrigo Del C. Andrade

Reputation: 503

You don't.

Somewhere in your inheritance chain those methods need to be implemented. That's the purpose of interfaces.

If you are using Java 8, there are new default implementations in interfaces, take a look at this page for details and that might help your case, barring that you need to have your concrete class inherit from an Abstract that provides an implementation for those 2 unwanted methods (even if its to print a cheerful "//TODO") message or remove them form the interface.

Upvotes: 1

Rishi
Rishi

Reputation: 1183

Firstly, you would get all the methods from the interface and wouldn't skip any. Then you have to implement the methods to satisfy interface contract. So it is better in your case to make 2 different interfaces and use them as you can implement multiple number of interfaces for a class.

   interface ClassA {
        void method1();
        void method2();
    }

    interface ClassB {
        void method3();
        void method4();
    }

Upvotes: 0

Related Questions