Limitations of Interface for Multiple Inheritance

Một phần của tài liệu object oriented proagramming and java 2nd edition (Trang 97 - 100)

Although the interface feature in Java provides an alternative solution to achieving multiple inheritance in class hierarchies, it has its limitations:

(a) No Inheritance Conflict

Consider the situation in Figure 6-17 where there is no inheritance conflict in attributes or methods.

a X

getA() b

getB()

Z

Y

Figure 6-17: No inheritance conflict.

A Z object has attributes a and b together with methods getA() and getB(). Without the support of multiple inheritance in Java, such class hierarchy cannot be realized.

(b) No Code Reuse

Let us, as examples, consider land vehicles such as motor cars and trucks. A class hierarchy showing Motor Car and Truck as subclasses of LandVehicle is given in Figure 6-18.

in situations where there is no inheritance conflict.

a. An interface does not provide a natural means of realizing multiple inheritance b. While the principal reason for inheritance is code reusability, the interface facility does not encourage code reuse since duplication of code is inevitable.

86 Object-Oriented Programming and Java

Motor Car and Truck have been declared as subclasses to distinguish the differ- ent brake system used. By means of inheritance, data attributes regnNumber and numberOfPassenger of LandVehicle are inherited by Motor Car and Truck as part of their properties.

Let us extend this example in our discussion of the issue—no code reuse and further extend the class hierarchy to include information that distinguishes the drive system between the land vehicles. We will consider three alternative representations.

Land Vehicle

regnNumber numberOfPassenger

Motor

Car Truck brake()

brake()

Figure 6-18: Motor car and truck.

In the first representation, the front-drive feature of a vehicle is defined as a super- class in Figure 6-19. The implementation of drive() method for the vehicles is indicated by the two statements:

frontWheelSys.engage();

this.accelerate();

The Front-Wheel-Drive class is an abstract class with a drive() method. This suggests that all motorcars and trucks are front-wheel-drive vehicles. This is incor- rect as some motorcars and trucks may be back-wheel-drive vehicles. The class hier- archy in Figure 6-19 is therefore inappropriate for representing motor cars and trucks.

Land Vehicle

regnNumber numberOfPassenger

Motor

Car Truck brake()

brake()

Front-Wheel Drive

drive()

frontWheelSys.engage() this.accelerate()

Figure 6-19: “Front-Wheel Drive Class as Superclass” Representation Scheme

Inheritance 87

Front-Wheel Drive Car

drive() Front-Wheel drive()

Drive Truck Land

Vehicle

regnNumber numberOfPassenger

Motor

Car Truck brake()

brake()

Figure 6-20: Front-Wheel Drive class as subclasses: representation scheme.

In the second representation, the front-drive feature is contained within a sub- class, Front-Wheel-Drive Car or Front-Wheel-Drive Truck in Figure 6-20. While this is semantically correct in satisfying the requirement, it does not take full advantage of inheritance – the drive() method is duplicated in Front-Wheel-Drive Car and Front-Wheel-Drive Truck class.

In the last representation, the drive() method is abstracted into a separate super- class and inherited by Front-Wheel-Drive Car and Front-Wheel-Drive Truck through a multiple inheritance chain in Figure 6-21.

drive() Front-Wheel

Drive Truck Front-Wheel

Drive Car

Land Vehicle

regnNumber

numberOfPassenger

Motor

Car Truck brake()

brake()

drive() Front-Wheel

Drive Figure 6-21: “Multiple inheritance” representation scheme.

This hierarchy is obviously desirable but implementing the solution with the in- terface construct would result in the definition of drive() method as an abstract method of a Front-Wheel-Drive interface and implemented in the Front-Wheel-Drive Car and Front-Wheel-Drive Truck classes. The effect is the same as that for the

88 Object-Oriented Programming and Java

“Front-Wheel Drive as Subclasses” representation scheme where the implementation of the drive() method was duplicated in the two subclasses.

Truck

Front-Wh

Drive Car Front-Wh

Dr Truck Back-Wh Dr Truck Back-Wh

Drive Car Front-Wh

Drive

Motor Car

VehicleLand

regnNumber numberOfPassenger

brake() brake()

Back-Wh Drive drive()

backWheelSys.engage() this.accelerate() drive()

frontWheelSys.engage() this.accelerate()

Figure 6-22: Front-Wheel and Back-Wheel Drive vehicles.

The problem with the interface solution is further amplified when we consider the need to implement the drive() method for Back-Wheel-Drive vehicles as well (see Figure 6-22). Using the interface solution, we need to implement the drive() method in Front-Wheel-Drive Car, Back-Wheel-Drive Car, Front-Wheel-Drive Truck, and Back-Wheel-Drive Truck. This approach can be error-prone.

From the above example, it is clear that the interface construct does not encour- age code reuse and duplication of method implementation is inevitable. If multiple inheritance had been available in Java, implementing the drive() method would be easier.

Một phần của tài liệu object oriented proagramming and java 2nd edition (Trang 97 - 100)

Tải bản đầy đủ (PDF)

(328 trang)