An important feature of object-oriented--inheritance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.