Inheritance (1) - Base
Inheritance is an importance concept in OOP. This article is based on Java Object-oriented Programming.
Type of Class
- Supper Class 超类
- Base Class 基类
- Parent Class 父类
- Subclass 子类
- Derived Class 派生类
- Child Class 孩子类、
In Java, using extends
to inheritance. In this case, Manager is the subclass of super class Employee.
1 | public class Manager extends Employee |
SUbclass will have the properties and methods in parent class, so it generally have more preperties and methods than parent. Subclass cannot directly access to the private field of super class.
To access the super class, use super
1 | double baseSalary = super.getSalary(); |
In C++, using ::
1 | double baseSalary = Employee::getSalary(); |
Construct a Subclass
1 | public Manager(String name,double salary,int year,int month,int day) |
In this case, the field in superclass Employee
is private, so it need super()
to initiate the values. If it does not have super()
constructor. It automatically call the default constructor in super class(which does not have parameter). It the default constructor not exist, compiler will output wrong message.
Polymorphism
According to techopedia
Polymorphism is an object-oriented programming concept that refers to the ability of a variable, function or object to take on multiple forms. A language that features polymorphism allows developers to program in the general rather than program in the specific.
For example, Employee can use any subcloss like Manager and Secretary.
1 | Manager boss = new Manager(); |
Even staff[0]
and boss
point to the same instance. Compiler see staff[0]
as a Employee.
Static & Dynamic Bind Method
- Static: private static final
- Dynamic: method called depend on the parameters. The JNM search in all method to find the correct one, which cost running time.
Final
When method or properties is final
, the subclass cannot override it.
Inheritance (1) - Base