Through the object-oriented understanding, object-oriented has four characteristics: abstraction, encapsulation, inheritance and polymorphism.
What we have come to understand is the most important object-oriented feature inheritance:
Inheritance is a hierarchical model of a junction class, and inheritance allows and encourages the reuse of classes. The use of inheritance can be very good
The method of expressing generality also can better embody the reuse in Java. We can extend a derived class from an existing class,
A derived class can inherit all the properties from an existing class. The existing classes are also known as the parent class, the superclass, and the derived classes are also called subclasses. Derived classes
It is more applicable to modify and add special needs, which also reflects the special and general relationship of nature. In order to better understand the inheritance, we
Use the following example to understand inheritance:
There are two kinds of salary in the employee table, one is to take the annual salary, and the other is to pay by the hour.
/** * Employee Parent class * This is a property shared by the annual salary and hourly salary * @author ASUS1 * */public class Employee {public string name;//name public string address;//mailed to Address public int ssn;//Social Security Number public int number;//employee number public void println () {System.out.println ("Mail Check to employee" +name+ "is" +address ");} /** * Subclass, inheriting all properties and methods in the Employee * which is used to inherit the keyword extends * @author asus1 * */public class Salary extends Employee{public double sal ary;//annual salary, for this class-specific attribute/** * Calculate the annual salary * @return salary per week */public double Computerpay () {System.out.println ("Calculate wages for employees with fixed wages" +name+ " //name is the inheritance of the attribute in the Employee parent class return SALARY/52; }}/** * Take hourly class * Inherit the keyword from the Employee parent class extends Inheritance * @author Asus1 * */public class Hourly extends Employee{public double Hourlyra te;//No hourly remuneration public double hoursword;//weekly working hours/** * Calculate hourly salary for employees * @return pay employee Wages */public double Coumuterpay () { System.out.println ("+name+");//name is inheriting the attribute in the employee parent class Double pay = 0.0;if (Hoursword <=40) {paid = Hourlyrate*hoursword;} else{//Calculate Overtime pay = (Hourlyrate * +) + (Hourlyrate * (hoursword-40) *1.5);} return pay; }
An important feature of object-oriented--inheritance