Let\'s look at the following simple code snippet in Java.
interface Sum
{
abstract public void showSum();
}
interface Mul
{
abstract public void showMul
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.
This ought to be comment to JB Nizet's answer, but I cant post comments yet :( Note that you could provide the implementation for the showSum() method in your abstract class, and still have that class remain abstract without any problems. An abstract class could have all methods implemented and still be abstract!
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.
From Oracle's Java tutorial:
a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface's methods, provided that the class is declared to be abstract.
Reference document: https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html , section When an Abstract Class Implements an Interface.
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.