Current location - Education and Training Encyclopedia - Resume - What are the advantages of single inheritance over multiple inheritance? Disadvantages?
What are the advantages of single inheritance over multiple inheritance? Disadvantages?
Advantages of single inheritance: avoiding some mistakes. Let me give you an example.

Parent class 1{

Public void method () {

//Method 1

}

}

Parent class 2{

Public void method () {

//Implementation of Method 2

}

}

Now you use a subclass to inherit two parent classes.

Class child extension parent 1, parent 2{

//……

}

Think about it. If you want to call the method method, which parent class of the method method should the compiler call? This will bring errors and is not conducive to the security of the program.

But java only supports this problem, and inheritance does not exist.

Some people may say, what if I want to inherit multiple classes with java? Don't worry, there are interfaces in java, which are more elegant and safer than multi-inheritance supported by c++.

2. Disadvantages: java is a single inheritance, so this inheritance is very precious, so you use inheritance in unnecessary places, so if you have to inherit this code in the future, you will be very hurt.

So I personally think that combination can be used as much as possible, instead of always thinking about inheritance.