Reputation: 10695
I have a base class A
that provides some functionality and a subclass B extends A
that provides additional functionality on top of A
.
Assuming classes C
, D
, and E
extend A
, how would I construct classes of C
, D
, and E
that extend B
instead (without making new classes that basically duplicate C
, D
, and E
)?
Classes C
, D
, and E
that extend A
are still needed in addition to those that extend B
.
For example (feel free to improve it):
A = Ball - provides basic things that are shared by all balls
B = Beach ball - provides some beach ball specific things
C has a method roll() that could allow A or B to roll
D has a method bounce() that could allow A or B to bounce
E has a method spin() that could allow A or B to spin
Any ball can have all, some, or none of the features C, D, and E
The goal is to be able to create objects of type A
or B
that include features from the set {C, D, E}
without having to make classes for all possible combinations
Upvotes: 0
Views: 136
Reputation: 91
My guess:
abstract class Ball {
< some common functionality >
}
Now BeachBall is a concrete class, it is a specific ball. So you should take it as a separated class, now
:
class BeachBall extends Ball {
< some common functionality inherited from Ball >
<additional functionality>
}
Other classes like C,D,E also is concrete classes and assume it is another type of balls, so :
class C extends Ball {
< some common functionality inherited from C >
<additional C funcionality>
}
<other classes are ommited>
Or if your C,E,D classes are more specific type of BeachBall, it can be done like this:
class C extends BeachBall {
< some common functionality inherited from BeachBall & Ball >
<additional C funcionality>
}
<other classes are ommited>
N.B <other classes are ommited>
means the same applies to E,D same as C.
Upvotes: 0
Reputation: 679
Add Basic functionalities of Ball Like roll(), bounce() and spin() in Ball Class as all Balls should have these three. And Extend Class A in Class B so by doing this you can instantiate Ball and Beach Ball where Ball and Beach Ball both will be having roll bounce and Spin and Beach Ball would be having its own features
Upvotes: 0
Reputation: 425418
In java, a class can only extend one class. If you want implementations of C, D and E that extend B instead of A, you must create new classes.
But, behaviour can be controlled in ways other that class hierarchy.
One option would be to use the delegation pattern, in combination with interfaces:
interface MyInterface {
public void someMethod();
}
class A implements MyInterface {
public void someMethod() {
// do something
}
}
class B extends A {
public void someMethod() {
// do something different
}
}
class C implements MyInterface {
private final MyIntegerface delegate;
public C(MyIntegerface delegate) {
this.delegate = delegate;
}
public void someMethod() {
delegate.someMethod();
}
}
The client constructing the C object passes to it the delegate the client wants C to use:
C x = new C(new A());
C y = new C(new B());
There's only one definition of class C, but multiple ways it can behave.
Upvotes: 1
Reputation: 1437
Class B extends Class A. So all functionality of Class A will be inherited in Class B.
Class B extends A{}
Now you want Class C, D, E to extend functionality of Class A and B Which can be done by simply extending Class B instead of Class A
Class C extends B{}
Class D extends B{}
Class E extends B{}
Upvotes: 0
Reputation: 107
Extend class B directly instead of class A. you can understand better if you look into this
Upvotes: 0
Reputation: 1264
If Class B extends A. Then you can make Classes (C,D and E) extends class B instead of A. By this the three classes (C, D and E) can use the public functionality(attributes and methods) available in both classes B and A (inheritance hierarchy).
Upvotes: 0