4

We can achieve the same functionality as interfaces by using abstract classes, So why java doesn't allow the following code?

abstract class Animals
{
    public abstract void run();
}

abstract class Animals1
{
    public abstract void run1();
}

class Dog extends Animals,Animals1
{
  public void run() {System.out.println("Run method");}
  public void run1() {System.out.println("Run1 method");}
}

I know that multiple inheritance can be achieved by using only interfaces but the above code does the same thing as the interfaces would have done it.

Zephyr
  • 1,521
  • 3
  • 22
  • 42
  • Possible duplicate of [Why is Multiple Inheritance not allowed in Java or C#?](http://stackoverflow.com/questions/995255/why-is-multiple-inheritance-not-allowed-in-java-or-c) – eol Jul 03 '16 at 09:18
  • I know why multiple inheritance is not allowed in java and about the diamond problem but if you see in the example above, it's just the way interfaces would have been implemented so had a question about that. Peter Lawrey answered it anyway :) – Zephyr Jul 03 '16 at 09:33
  • I agree.. please see my comments below. – Abhijit Aug 13 '17 at 16:19

5 Answers5

8

This is not allowed because you can do more than this with abstract classes. It wouldn't make sense to allow multiple inheritance, provided you only used an abstract class when you could have used an interface.

It is simpler to only use abstract classes for things you can't do with an interface, in which case you wouldn't be able to use two abstract parent classes.

Note: with Java 8 there is less you can't do with an interface, you can have public instance and static methods with implementations.

In Java 9 you will be able to have private methods in interfaces ;)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    "able to have private methods in interfaces" ... really! This is the first time I've heard this. Because I find the present (J8) situation ludicrous: your "default" methods in J8 interfaces CAN access state... but only static state (interfaces can contain static fields). Isn't it about time they just made multiple inheritance possible and "deprecated" interfaces? And simply get the compiler to require that subclasses with ambiguities, um, resolve those ambiguities? – mike rodent Dec 07 '16 at 18:07
  • @mikerodent I see you point but Brian Goetz recently stated that state will only be on classes so I don't see this changing soon. State isn't intended to be on interfaces and field are always final (though they can be mutable) – Peter Lawrey Dec 07 '16 at 18:31
2

This is because abstract classes are still classes, and inheritance is different than implementing an interface. Please see the differences between abstract class and interface. Also please see differences between inplementing an interface and extending a class in Java.

This both questions covers all the information you need to know to understand why you can't have multiple inheritance of abstract classes.

Also let me give you an example why this should not be allowed: Suppose you have the both Animals and Animals1 implementing the same interface IAnimals:

interface IAnimals
{
    public string eat();
}

abstract class Animals implements IAnimals
{
    public abstract void run();
    public string eat(){ return "Animals eating"; } 
}

abstract class Animals1 implements IAnimals
{
    public abstract void run1();
    public string eat(){ return "Animals1 eating"; } 
}

If you now define your Dog class as you did:

class Dog extends Animals,Animals1
{
  public void run() {System.out.println("Run method");}
  public void run1() {System.out.println("Run1 method");}
}

It will have the method eat() too, which is not abstract so it can use it directly. What would be the return of this method for a dog? Which string will be returned, the one with Animals, or the one with Animals1?

This is called the diamond problem, and it is a reason why in some programming languages it is not allowed multiple inheritance.

Community
  • 1
  • 1
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • If these answers, answer the same question, you can vote to close this as a duplicate. – Peter Lawrey Jul 03 '16 at 07:54
  • 1
    @PeterLawrey should I vote to close the question if there are needed two answers to provide the information for a single question? Of course he can find the answer to his question by reading many articles, but as it is formulated, I don't think that his question is a "Duplicate of" one of the questions I suggested. But I need to admit that I was thinking for a seond the very same thing. – meJustAndrew Jul 03 '16 at 08:06
  • When the information is available but only by combining two answers I tend not to close but include the relevant information in a new answer as you have now done. – Peter Lawrey Jul 03 '16 at 08:11
  • 1
    @PeterLawrey I know this can be dicussed, and I also think that it's okay to leave the answer just like it is, because it's more helpful for anybody which is having the same problem, than closing the question. I think it is worth the effort. – meJustAndrew Jul 03 '16 at 08:17
  • I agree, a combined answer is a worth while addition. – Peter Lawrey Jul 03 '16 at 08:18
0

Java does not support multiple Inheritance. -" One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes. " Source https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html You may find this link useful.

Rini
  • 65
  • 1
  • 8
0

I agree with you(if i understood about what you are talking :) )this is no need of such specific naming conventions.

interface pet
{

    public abstract void pet();
}

interface pet1
{
    public abstract void pet1();
}

class TestTt implements pet,pet1
{
    public void pet() 
    {
        System.out.println("this is method of pet interface");

    }

    public void pet1() {
        System.out.println("this is method of pet1 interface");

    }   
    public static void main(String a[])
    {
        pet obj=new TestTt();

        pet1 obj1=new TestTt(); 

        obj.pet();

        obj1.pet1();
    }       
}

Now, Here if abstract class allows me to create object .then, i can create 2 different references for 2 abstract classes as in interface i can do.

If so, do i need Interfaces...?

Abhijit
  • 333
  • 4
  • 7
-1

In ABSTRACT class,we can't extends multiple abstract classes at a time. but In INTERFACE, we can implements multiple interfaces at time.

Therefore , interfaces are used to achieve multiple inheritance in java.

  • 3
    Nope. Implementing an interface has nothing to do with inheritance. In inheritance you're supposed to inherit the parent's behaviors as well. When implementing an interface you are just "assuring" that your class has the same method the interface has. – Federico klez Culloca Jan 20 '20 at 10:11