Why can't an abstract class extend an interface?
Asked Answered
D

3

13

I was just wondering why an abstract class can't extend an interface.

Since we can't instantiate an abstract class, can't I just extend the interface and then override those methods in the classes which are extending the abstract class?

For example

abstract class AbstractClass extends InterfaceA 
{

}

interface InterfaceA
{
   public void methodToBeImplemented();
}

class MyClass extends AbstractClass
{
  @Override
  public void methodToBeImplemented(){
      //do something
  }
}  
Desuetude answered 9/8, 2014 at 0:30 Comment(1)
Because that's the way the language is defined.Dore
C
23

Because you don't extends an interface - you implements it.

interface InterfaceA
{
   public void methodToBeImplemented();
}

abstract class AbstractClass implements InterfaceA 
{

}

class MyClass extends AbstractClass
{
  @Override
  public void methodToBeImplemented(){
      //do something
  }
}  

An abstract class is still a class, just like any other. Only interfaces can extends other interfaces.

Crumple answered 9/8, 2014 at 0:32 Comment(4)
Yes, I'm aware of the grammatical error. It was intentional.Crumple
I'm not the downvoter, but why aren't you consistent? Shouldn't it be implements?Introrse
@Introrse Yes it should. :)Crumple
@Qix I actually heard Popeye talking when you wrote "you implements it" - thanks for the laugh!Aragon
C
5

You can actually extend interfaces in Java, but it would still be called an interface.

Then you can use this extended interface implemented in your abstract class.

interface InterfaceName
{
    public void foo();
}

public interface ExtendedInterface extends InterfaceName
{
    public void bar();
}

public class ClassName implements ExtendedInteface 
{
    //implementation for all methods in InterfaceName and ExtendedInteface
    ...
}

Praveen, its just the way the designers designed it. It goes back to the philosophy of Java and the way classes and interfaces are meant to be used. Lets say you have following classes: Student, Professor, Employee, Technician. And following interfaces: Demography, Paydetails and Personaldetails.

Ideally, classes professor and technician can be both extended from the class employee and it makes sense. However, imagine extending class Technician from Demography or Personaldetails. It doesnt make sense. Its like extending class sky from class blue. Just cause you can do doesnt mean you should do it. However, it makes more sense to have a class technician extend from employee and implement interfaces like Paydetails or demography. Its like extending sky from nature but implementing interface blue (giving it function capabilities of blue).

This is the basic reason why Java developers were against multiple inheritance (unlike cpp) and instead use interfaces. In your question, it doesnt matter if the class is abstract or not. You simple cant extend from an interface cause it does not make sense logically.

Codification answered 9/8, 2014 at 0:35 Comment(4)
Perhaps an example would enhance this answer?Crumple
Yep! .. I understand that only interface can extend another interface but my question is, why sun/oracle didn't allowed an abstract class to extend the interface like (interface extending a interface)?? ... What would be consequences if that was actually allowed?Desuetude
Also extending multiple interfaces is allowed, not obviously not for classes.Marcionism
Although you can implements multiple interfaces :)Crumple
D
1

in interface extends only interface .when we use a interface in a class then it need to implements in class.and another thing is abstract class contain a both abstract and non abstract method while interface contain only abstract method that are define where implementation of inteface(subclass).

Dacoity answered 17/9, 2016 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.