Java Reference
In-Depth Information
Another idea would be to try to define an abstract class Stretchable as follows:
abstract class Stretchable
{
public abstract void stretch( double factor );
}
We could use Stretchable as our missing type in the stretchAll method.
At that point, we would try to have Rectangle , Ellipses , and Triangle extend
Stretchable , and provide the stretch method:
// Does not work
public class Rectangle extends Shape, Stretchable
{
public void stretch( double factor )
{ ... }
public void area( )
{ ... }
...
}
The picture that we would have at this point is shown in Figure 4.15.
In principle this would be fine, but then we have multiple inheritance,
which we have previously said was illegal, because of concerns that we
might inherit conflicting implementations. As it stands now, only the Shape
class has an implementation; Stretchable is purely abstract, so one could
argue that the compiler should be willing to grant leniency. But it is possible
that after everything is compiled, Stretchable could be altered to provide an
Shape
Stretchable
Circle
Square
Rectangle
Ellipse
Triangle
figure 4.15
Inheriting multiple classes. This does not work unless either Shape or Stretchable is
specifically designated as being implementation-free
Search WWH ::




Custom Search