Class extending more than one class Java?

后端 未结 11 1535
执笔经年
执笔经年 2020-12-06 04:29

I know that a class can implement more than one interface, but is it possible to extend more than one class? For example I want my class to extend both TransformGroup<

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 05:30

    In Java multiple inheritance is not permitted. It was excluded from the language as a design decision, primarily to avoid circular dependencies.

    Scenario1: As you have learned the following is not possible in Java:

    public class Dog extends Animal, Canine{
    
    }
    

    Scenario 2: However the following is possible:

    public class Canine extends Animal{
    
    }
    
    public class Dog extends Canine{
    
    }
    

    The difference in these two approaches is that in the second approach there is a clearly defined parent or super class, while in the first approach the super class is ambiguous.

    Consider if both Animal and Canine had a method drink(). Under the first scenario which parent method would be called if we called Dog.drink()? Under the second scenario, we know calling Dog.drink() would call the Canine classes drink method as long as Dog had not overridden it.

提交回复
热议问题