I know that Java does not allow us to extend more than one class. I know that interfaces also exist and I don't want to use them this time. Is there some kind of trick or workaround to extend multiple classes in Java ? If yes, please include the sample code also.
-
You can use inerfaces – Ajouve Apr 26 '13 at 09:42
-
Question edited - I also say "I know that interfaces also exist and I don't want to use them this time." Now what will the answer be ? – david blaine Apr 26 '13 at 09:44
-
Still no, I'm afraid. – Quetzalcoatl Apr 26 '13 at 09:45
-
possible duplicate of [Extending multiple classes](http://stackoverflow.com/questions/15429357/extending-multiple-classes) – Stephen C Apr 26 '13 at 09:46
-
If you still want multiple Inheritance in spite of knowing about Interfaces perhaps you need to rethink your application design. – Ravi K Thapliyal Apr 26 '13 at 09:48
-
possible duplicate of [Multiple inheritance without multiple inheritance](http://stackoverflow.com/questions/10774143/multiple-inheritance-without-multiple-inheritance) – duffymo Apr 26 '13 at 09:49
-
@Ravi - I want my class to implement Observable and Jpanel ? What do I do ? – david blaine Apr 26 '13 at 09:50
12 Answers
No, unlike C++ you cannot extend multiple classes but you can implement multiple interfaces.

- 3,037
- 2
- 18
- 27
-
Question edited - I also say "I know that interfaces also exist and I don't want to use them this time." Now what will the answer be ? – david blaine Apr 26 '13 at 09:46
-
It simply isn't possible, its omission in Java was a conscious language design choice that they made for fear of causing "confusion". – Quetzalcoatl Apr 26 '13 at 09:48
You can do it easily with interfaces and composition.
The question is why you would ask such a thing? Why do you not want to use interfaces "at this time"?
You have to know that Java only allows single inheritance of implementation. You've spent seven months at SO, so surely you must know how to use the search feature. The question has been asked here repeatedly.
Be more creative. There's no reason that a JPanel has to be Observable. JPanel is for rendering. I agree that its model data might want to be Observable. By all means do it with composition.

- 305,152
- 44
- 369
- 561
-
I want my class to implement Observable and Jpanel ? What do I do ? – david blaine Apr 26 '13 at 09:49
-
2There's no good reason to do so. Extend JPanel and give it an Observable model object - done. – duffymo Apr 26 '13 at 09:49
-
How will i make a model given that - I want to make a reusable class called button holder which extends JPanel. This will hold a grid of buttons, each of which has a unique value/name. The button holder can be used by any device like elevator panel, calculator etc. These devices are panels which hold a text field and button holder. They will find out which button is pressed inside button holder and then put the name of button in text field. – david blaine Apr 26 '13 at 09:58
-
Every button will have to have a Listener to respond to events. Wouldn't the model be a good candidate to be that Listener, responding to every button's press event? Now you also have text boxes? Aren't those part of the model? It's possible to do what you want without multiple inheritance, but I'm not going to design and implement it for you in a comment. – duffymo Apr 26 '13 at 10:40
-
haha - dont expect you to do the work for me. Just needed something to get me started. I was confused. – david blaine Apr 27 '13 at 02:29
NO.
go for interfaces
.No multiple inheritance in Java.
Multiple inheritance can cause the diamond problem.
JAVA omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions _ Dr. James Gosling

- 120,458
- 37
- 198
- 307
-
Question edited - I also say "I know that interfaces also exist and I don't want to use them this time." Now what will the answer be ? – david blaine Apr 26 '13 at 09:46
Multiple inheritance in java is usually done by implementing multiple interfaces. You can not extend more than one class. A possible workaround may be to use some kind of object composition for instance aggregation.

- 69,226
- 18
- 123
- 176
-
Question edited - I also say "I know that interfaces also exist and I don't want to use them this time." Now what will the answer be ? – david blaine Apr 26 '13 at 09:45
-
1@davidblaine you can not extend multiple classes. You could use composition for instance - put a member of the type of the class you want to inherit – Ivaylo Strandjev Apr 26 '13 at 09:47
-
@davidblaine depends on what you are trying to do. Composition `is a way to combine simple objects or data types into more complex ones` so if it works for you you may consider it a workaround – Ivaylo Strandjev Apr 26 '13 at 12:58
may be this helps
class One{
void oneMethod(){
}
}
class Two extends One{
void TwoMethod(){
}
}
class Abc extends Two{
@Override
void oneMethod() {
// TODO Auto-generated method stub
super.oneMethod();
}
@Override
void TwoMethod() {
// TODO Auto-generated method stub
super.TwoMethod();
}
}

- 2,559
- 4
- 25
- 37
Java not support this for classes.You can use multiple interfaces.You can find what the problem with multiple inheritance in java
You can use inner classes like
public class class1 extends class2{
class Inner extends class3{
}
}

- 39,804
- 41
- 111
- 151
-
Question edited - I also say "I know that interfaces also exist and I don't want to use them this time." Now what will the answer be ? – david blaine Apr 26 '13 at 09:45
-
Multiple inheritance adds complexity with little benefits, that's why it is not present in java Why is Multiple Inheritance not allowed in Java or C#?
One way I can think of is to write your program needing multiple inheritance in a language that supports it e.g.C++ and then make your Java code interact with the output from that program e.g using files or databases.
Answer is No. But you can do this if helps:
A extends B
B extends C
so A extends B and C

- 803
- 14
- 30
Use interfaces instead. The following is how that would look in code:
public Class ClassName implements Interface1, Interface2 {
Methods, fields etc...
}

- 3,953
- 3
- 29
- 49
To avoid diamond problem Java does not support Multiple Inheritance through classes but it supports using interfaces. So you may use Association Relationship. e.g.
Class A {}
Class B {}
Class C implements SomeInterface {
A a;
B b;
// setter getter for a and b and other methods
}

- 12,348
- 19
- 73
- 82
You can use a lookup of capabilities.
class Vehicle {
T <T> lookup(Class<T> klazz) {
return map.get(klazz); // typically
}
}
interface Flyable {
void fly();
}
Vehicle x;
Flyable f = x.lookup(Flyable.class);
if (f != null) {
f.fly();
}
This decouples classes. It requires runtime checking though.
It uses delegation: local members implementing the lookup'ed class/interface.
Advantage:
You can derive a class from Vehicle that can both fly (Flyable) and dive (Diving). The flying can be implemented by an instance of a shared class. You can even dynamically compose capabilities (add wheels to a boat).
That often is better than implementing several interfaces, where fly()
would either be a code copy, or delegate to a Flyable field (with shared implementation class).
Especially with multiple interfaces/classes involved you otherwise risk code on base class objects (Vehicle here) with cases and unsafe casts, forgetting if (x instanceof Boat)
.

- 107,315
- 7
- 83
- 138
-
Joop I don't understand what this code is doing. Can you explain it to me ? – david blaine Apr 26 '13 at 09:47
I am going to upset an awful lot of people by saying yes you can - kind of.
It is certainly not easy and you have to use reflection but here you will find an example that allows you to dynamically extend one object with another. Obviously, once you can do that you can then extend another one too.
Essentially, it uses the Proxy class to intercept method calls and direct them to one or other of two objects - this implements A overrides B
. This is not quite what you are looking for but it could easily be tweaked to detect methods that exist in both classes and call them both - thus achieving A extends B
dynamically - kind of.
Note that you need to read the section entitled Dynamic Object Adapter using Dynamic Proxies section of the link.

- 64,482
- 16
- 119
- 213