Question

Question: What happens when a class specifies that it implements an interface, but does not provide declarations of all the methods in the interface?

Was it helpful?

Solution 2

You have to declare the all methods inherited by that interface, other wise it will not compile.

Example:

interface SomeInterface {
    public void methodOne();
    public void methodOne();
    public void methodOne();
}

And:

class SomeClass implements SomeInterface {
    //this wont complie
}

But if the class is an Abstract class it could declare the method inherited from that interface or some of them,

Example:

interface SomeInterface {
    public void methodOne();
    public void methodOne();
    public void methodOne();
}

And:

abstract class SomeClass implements SomeInterface {
    //this will compile
}

OTHER TIPS

A compile error, if the class is not abstract. If the class is abstract, it is ok to leave some methods of the declared interfaces undefined. The missing methods must be at the end defined in the derived non abstract classes.

Well it just does not compile at all unless your class is abstract.

E.g. Compiler says no!

It will fail to compile. " If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile." from Object-Oriented Programming Concepts

A compile error will generate stating to override abstract methods. Remember that, an interface, unlike an abstract classs, can provide no implementation at all. This means that, any class that implements an interface must provide the implementation for all methods.

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