Bneac
Bneac

Reputation: 89

Object Oriented Class Interfaces

After the first time you use a class that implements a interface in Java, and define the methods for that interface, do you still need to define the methods for that interface in all other classes that implement that interface or can you just implement the interface and begin using the interface's methods with an object from that class even if the methods for that interface are not explicitly defined in that class?

Upvotes: 0

Views: 94

Answers (5)

shashwatZing
shashwatZing

Reputation: 1640

For Example you have a interface

public Interface A 
{

    int add (int a , int b);

int subtract (int a , int b)

}

//Now you have a class that implements A interface

public class AImpl implements A{

@override

public int add ()
{
return a+b;
}

@override 
public int subtract(){

//do something

}

}

Now you want to write a class C that uses similar method as A interface, you can either implement that interface and write the implementation for it as per your need or you can extend the AImpl class and use the already implemented method if it suits to your need. The class that implements a interface should always literally implement all the methods of that interface.

Upvotes: 2

Keith
Keith

Reputation: 3151

So you have

public class Interface {
    public void someMethod();
}

and an implementation

public class InterfaceImpl implements Interface {
    public void someMethod() {
        System.out.println("Hello, world");
    }
}

You can subclass InterfaceImpl and use the subclass' inherited someMethod(). If InterfaceImpl is abstract, then its subclass needs to be concrete.

If you have another class that unrelated to InterfaceImpl (e.g. not inherited), then yes, that other class would have to implement someMethod() to meet Interface's contract.

Upvotes: 1

RajSharma
RajSharma

Reputation: 1971

If the class is not the subclass of the class that already implemented the interface, you have eto define all the methods again. But if a class is a subclass of a class that already implemented the interface no need to define the methods again unless you want to override the behaviour.

Upvotes: 1

Can't Tell
Can't Tell

Reputation: 13476

Not considering default methods,

For each interface a non-abstract class or one of its ancestor classes implements, either that class or one of its ancestor classes should define all the methods in that interface.

Considering default methods,

For each interface a non-abstract class or one of its ancestor classes implements, either that class or one of its or one of its ancestor classes should define all the methods in that interface. If that non-abstract class or one of its or one of its ancestor classes do not define the method, the interface should define the method via the default method feature.

Upvotes: 0

Jmrapp
Jmrapp

Reputation: 520

You need to implement the methods for each class individually, but say for example you have an interface "MyInterface" with a class "MyFirstClass" implementing it and it's methods. If a class "MySecondClass" implements the interface it too will need to implement the methods. But if "MySecondClass" extends "MyFirstClass" it won't need to implement the interface methods because they've already been implemented in "MyFirstClass".

Upvotes: 2

Related Questions