Abstract classes and Multiple Inheritance

前端 未结 5 1331
南笙
南笙 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...?

提交回复
热议问题