3

I have two classes that need to extend one class. I am getting a compiler error since this cannot happen in Java. I know that you can implement as many interfaces you want to in Java but can only extend one other class. How can I fix this problem?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Bdk Fivehunna
  • 65
  • 2
  • 2
  • 5

4 Answers4

9

Use a "has A" relationship instead of "is An".

class A
class B

You (think) you want:

class C extends A, B

Instead, do this:

class C {
  A theA;
  B theB;
}

Multiple inheritance is almost always abused. It's not proper to extend classes just as an easy way to import their data and methods. If you extend a class, it should truly be an "is An" relationship.

For example, suppose you had classes,

class Bank extends Financial
class Calculator

You might do this if you want to use the functions of the Calculator in Bank,

class Bank extends Calculator, Financial

However, a Bank is most definitely NOT a Calculator. A Bank uses a Calculator, but it isn't one itself. Of course, in java, you cannot do that anyway, but there are other languages where you can.

If you don't buy any of that, and if you REALLY wanted the functions of Calculator to be part of Bank's interface, you can do that through Java interfaces.

interface CalculatorIntf {
  int add(int a, int b);
}

class Calculator implements CalculatorInf {
  int add(int a, int b) { return a + b };
}

class Bank extends Financial implements CalculatorIntf
  Calculator c = new Calculator();

  @Override // Method from Calculator interface
  int add(int a, int b) { c.add(a, b); }
}

A class can implement as many interfaces as it wants. Note that this is still technically a "has A" relationship

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • +1 I'd like to add that you should use interfaces if you really need the dual "is a" relationship. – Brian Sep 20 '12 at 18:56
  • it depends on if you need to export the data and methods of the other class, or just consume them. if you only need to consume them, you should not make it's methods / data part of your interface (by implementing interfaces). all implemented interface methods are implicitly public. – Jeffrey Blattman Sep 20 '12 at 19:16
1

"Two classes that extend one class" is legal.

"One class extending two classes" is against the specification of the language. If you do not want to consider interfaces, it cannot be done.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

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
}
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
0

Two classes extending ONE class? like so:

class A{ }
class B extends A{ }
class C extends A{ }

Here, B and C(two classes) extend A(one class). But I am sure you meant One class extending Two separate classes(Multiple Inheritance). WorkAround: you could make a composite Object that has two separate objects(Has-A relationship) like so:

class A { }
class B { }
class C extends A { /*define new functionality here */ }
class D extends B { /*define new functionality here */ }
class E { private C cObj; private D dObj; }
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • 1
    perverting the class hierarchy in this way is almost never the right solution, and in most cases you won't have the ability to make those changes as you are consuming the super classes from a library. – Jeffrey Blattman Sep 20 '12 at 19:14
  • The problem starts at design level. If at any point in your design, you MUST inherit from two classes, then obviously you're designing it wrong! – Aniket Inge Sep 20 '12 at 19:23
  • Not at all. If, in your domain, `E isA A` and `E isA B`, then a very logical way to model this in code is through multiple inheritance. The fact that Java doesn't support this doesn't make it "wrong", merely "inconvenient". Other languages such as Scala support this (via [traits](http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-5), in Scala's case). – DNA Sep 22 '12 at 20:29
  • @DNA I am not saying its wrong. For example a "Son" inherits from both father and mother. This is also easily seen in C++ which supports multiple inheritance. Because the question was "in Java" I had to use "Has A" relationship instead of "isA". Not saying either is right or wrong. – Aniket Inge Sep 23 '12 at 19:11