Can you inherit more than one class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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}

17th Dec 2016, 3:30 PM
Yamkela Kutu
Yamkela Kutu - avatar
8 Answers
+ 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.
19th Dec 2016, 2:58 PM
Caffeinated Gamer YT
Caffeinated Gamer YT - avatar
+ 4
Java doesn't allow multiple inheritance. If u need it, possibly you're doing something bad.
17th Dec 2016, 3:36 PM
Sheemin
Sheemin - avatar
+ 2
Not in Java 😂
20th Dec 2016, 8:59 AM
Gabe Rust
Gabe Rust - avatar
+ 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.
21st Dec 2016, 1:59 AM
A B
+ 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
18th Dec 2016, 6:09 AM
Dago VF
Dago VF - avatar
+ 1
Yes,it is possible through multiple type of inheritance.Java does not support it.But you can use multiple inheritance in C++.
21st Dec 2016, 7:11 AM
Amrita.A
Amrita.A - avatar
+ 1
no
21st Dec 2016, 8:57 AM
utpatti mahajan
utpatti mahajan - avatar
0
Yeah thanks guys, now it makes sense
18th Dec 2016, 6:17 AM
Yamkela Kutu
Yamkela Kutu - avatar