CSC/ECE 517 Fall 2010/ch1 25 ag: Difference between revisions
Jump to navigation
Jump to search
Line 2: | Line 2: | ||
= What is Inheritance? = | = What is Inheritance? = | ||
Inheritance is one of important attribute of Object Oriented Languages. Using Inheritance a class reuse code defined in | 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: | Example: |
Revision as of 17:44, 18 September 2010
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)