CSC/ECE 517 Fall 2010/ch1 25 ag

From Expertiza_Wiki
Jump to navigation Jump to search

Advantage and Disadvantage of Multiple Inheritance

What is Inheritance?

Inheritance is one of important attribute of Object Oriented Languages. Using Inheritance a class can reuse code defined in another class. The advantage of this approach is that, we can have an hierarchy of Inheritance and write all generic code in the parent class and add only specific functions in the child classes.

Example:

class ElectricAppliance
{
    public void on() { ... }
    public void off() { ... }
}
class TempControlAppliance extends ElectricAppliance
{
    public void increaseTemp() { .. }
    public void decreaseTemp() { .. }
}
class Heater extends TempControlAppliance 
{
    public void heat() { .. }
}
class AC extends TempControlAppliance 
{
    public void cool() { .. }
}

If we don't have inheritance, we might have to duplicate all the functions in both Heater and AC class. Inheritance is very important to achieve DRY (Don't Repeat Yourself)