- Perl ›
- Object Oriented ›
- What is inheritance?
What is inheritance?
Inheritance is one of the object-oriented features.
1. Consider Winwods application components as an example of inheritance
Consider a Windows application as a straightforward analogy to inheritance. Windows applications have roughly the same shape. There's a title bar at the top, a menu bar below it, a toolbar below it, and so on. There is a menu in the menu bar. There are buttons inside the toolbar.
What do all this have in common? What do menus, toolbars, menu items, and buttons have in common?
Let's find something in common. First, every part has a position and size.
2. Create a class that does not use inheritance
Consider implementing a component that does not use inheritance.
# Menu bar class package MenuBar; sub new {...} sub x {...} sub y {...} sub width {...} sub height {...} # Write other methods. sub method_for_menubar {...} # Toolbar class package ToolBar; sub new {...} sub x {...} sub y {...} sub width {...} sub height {...} # Write other methods. sub method_for_toolbar {...} # Button class package Button; sub new {...} sub x {...} sub y {...} sub width {...} sub height {...} # Write other methods. sub method_for_button {...}
I wrote three classes like this, but all of them have the common accessors x, y, width, and height. I want to do something about this.
Inheritance is a mechanism that removes such duplication.
3. Rewrite class with inheritance
Let's rewrite the class with inheritance. Please take a look.
# Component class package Component; sub x {...} sub y {...} sub width {...} sub height {...} # Menu bar class package MenuBar; use base 'Component'; sub new {...} sub method_for_menubar {...} # Toolbar class package ToolBar; use base 'Component'; sub new {...} sub method_for_toolbar {...} # Button class package Button; use base 'Component'; sub new {...} sub method_for_button {...}
As I will explain the inheritance method later, you can see that the methods x, y, width, and height have been added to the Component class.
Component is called a superclass, and classes that inherit from Component can call methods x, y, width, height.
I think there are various ways of thinking about inheritance, but I personally think that the role of inheritance is to create common parts .
I will write mathematical formulas to facilitate figurative interpretation.
pa + pb + pc = p (a + b + c)
The left and right are equivalent as expressions, but the description of three overlapping ps is reduced to one. If you don't understand the essential role of inheritance, remember this formula.
Inheritance is a function that pulls out common parts and eliminates duplication of code writing.
4. What is commonly thought of
In general, the image of inheritance may be seen as an extension of a class. But it's rare to use inheritance to extend an implemented class.
In most cases, you design a class that you plan to inherit and then inherit it.
In other words, the common functions should be created in the classes that are planned to be inherited.