Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An abstract class in Java need not implement any methods from its implementing interface. Why?

Tags:

java

Let's look at the following simple code snippet in Java.

interface Sum
{
    abstract public void showSum();
}

interface Mul
{
    abstract public void showMul();
}

abstract class Super implements Sum
{
    protected int x;
    protected int y;

    public Super(int x, int y)
    {
        this.x=x;
        this.y=y;
    }

    //No error, though the method showSum() of the implementing iterface Sum is commented. Why?

    /*public void showSum()
    {
        System.out.println("Sum = "+(x+y));
    }*/
}

final class Calculation extends Super implements Mul
{
    public Calculation(int x, int y)
    {
        super(x,y);
    }

    public void showSum()
    {
        System.out.println("Summation = "+(x+y));
    }

    //If showMul() is commented , it would issue a compile-time error. Why?
    public void showMul()
    {
        System.out.println("Multiplication = "+(x*y));
    }   
}

final public class Main
{
    public static void main(String[] args) throws IOException
    {
        Scanner s=new Scanner(System.in);

        System.out.print("\nEnter a number : ");
        int x=s.nextInt();

        System.out.print("\nEnter another number : ");
        int y=s.nextInt();

        Calculation c=new Calculation(x,y);
        c.showSum();
        c.showMul();
    }
}

Since the interface Sum containing only one method showSum(); is being implemented by the abstract class Super , there should be necessary for the abstract class Super to implement that method showSum();. The compiler however doesn't complain at all and the program is working well with no problem at all. Why?


Similarly, the non-abstract final class Calculation is implementing the Mul interface and contains the actual implementation of the method showMul(); presented in it's implementing interface. In case, if this method showMul() in the class Calculation is commented, it issues a compile-time error. Why is the same thing not applicable to that abstract class Super?

like image 886
Bhavesh Avatar asked Nov 06 '11 11:11

Bhavesh


2 Answers

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.

like image 161
gtiwari333 Avatar answered Oct 13 '22 01:10

gtiwari333


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.

like image 28
Oleg Mikheev Avatar answered Oct 13 '22 02:10

Oleg Mikheev