Abstract classes and Multiple Inheritance

前端 未结 5 1327
南笙
南笙 2021-01-19 15:02

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

abstract class Animals
{
    pu         


        
相关标签:
5条回答
  • 2021-01-19 15:28

    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...?

    0 讨论(0)
  • 2021-01-19 15:32

    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.

    0 讨论(0)
  • 2021-01-19 15:42

    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.

    0 讨论(0)
  • 2021-01-19 15:49

    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 ;)

    0 讨论(0)
  • 2021-01-19 15:53

    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.

    0 讨论(0)
提交回复
热议问题