+ 3
Can you inherit more than one class?
Let's say you have a class called Motorbike and you want to inherit the classes Car and MiniCar, are you able to do so and how do you go about doing so? I know for instance this is how you would inherit another class: class Motorbike extends Car { code}
8 ответов
+ 5
One way in your case is to make MiniCar inherit car(isn't that logical anyway?) and then make Motorbike extend MiniCar(wait, your example is screwed up. Motorbike shouldn't logically inherit car since it's not a Car!!!)
Another way is that you could have either of Car or MiniCar declared as interface. Java allows you to implement multiple interfaces even while already inheriting a class.
+ 4
Java doesn't allow multiple inheritance. If u need it, possibly you're doing something bad.
+ 2
Not in Java 😂
+ 2
If you really need multiple inheritance for your model, I recommand that you write your code in C++, otherwise try to work with interfaces because then your class can implement as many as you want.
+ 1
Speaking specifically of your example
It's seems it's a wrong design, looks like miniCar can inherit from Car, so if motorcycle inherits from miniCar, will be able to use miniCar and Car methods
+ 1
Yes,it is possible through multiple type of inheritance.Java does not support it.But you can use multiple inheritance in C++.
+ 1
no
0
Yeah thanks guys, now it makes sense