An abstract class in Java need not implement any methods from its implementing interface. Why?
The abstract
class is not real implementation class. It may contain abstract
methods and doesnot need to implement
the methods from the interface
. It is concern of the REAL implementing class to define the abstract/interface methods.
See this difference between abstract class and interface
Interface:
public interface InterfaceClass {
void interfaceMethod();
//no method definition
}
//Abstract Class implementing InterfaceClass
abstract class AbsClass implements InterfaceClass{
abstract void abstractMethod();
public void doSomethingCommon() {
System.out.println("Abstract class may contain method definition");
}
//no need to implement methods of InterfaceClass because AbsClass is abstract
}
And here is real class that extends AbsClass
: Its duty of RealClass to define all abstract methods and interface methods. Additionally, it may override
the defined methods in abstract
class as well.
public class RealClass extends AbsClass{
@Override
public void interfaceMethod() {
//implement interface method here
}
@Override
void abstractMethod() {
}
// you may override the doSomethingCommon() of AbsClass too
@Override
public void doSomethingCommon() {
// TODO Auto-generated method stub
super.doSomethingCommon();
}
}
Why there is no compile time error on AbsClass
:
We cannot create instances of abstract class. That's why there is no meaning of displaying error at compile time.
Abstract classes behavior resembles interfaces in this case.
From the Java Tutorial: ...abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead
You don't implement methods in an interface that extends another interface. And you don't HAVE to implement methods in an abstract class that implements an interface.
Because it's abstract. An abstract class is a class which is allowed to declare some methods without providing any implementation of those methods, forcing concrete subclasses doing it. You could add the method
@Override
public abstract void showSum();
to the abstract class, but it would just be redundant with the method already declared in the interface.