Introduction to the Java language Tutorial (12): Basic concepts of inheritance in the Java language

Source: Internet
Author: User
Tags inheritance

Starting with tutorial (10), you have moved from learning the basic syntax of a single class to learning about relationships between multiple classes. In tutorial (10), we learned the relationship between class and class length, association and dependency. To maintain a 1-many-to-many relationship, in the tutorial (11), we learned the array. In addition to association and dependency, classes and classes have a very important and common relationship, that is, inheritance. This article will introduce the concept of inheritance, the role, use scenarios, and so on.

Suppose there is such a simple requirement (in fact not a requirement, just for beginners to understand and fabricated): a training center to develop an internal staff management system. The training centre currently has two departments, the technical resources department and the operations department. The staff of the Technical Resources department are responsible for lectures, called Lecturers. Business Department staff is responsible for contacting business, known as sales. In a management system, you need to manage the names, salaries, and technical orientation of all employees, and the amount of tasks you sell. .....

For the simple description above, let us analyze what the main management objects are in the system of the training center. It is not difficult to see, mainly two kinds of employees, that is, lecturers and sales. Lecturers and sales are different, such as instructors have instruction direction attributes, sales have task volume attributes, the two have a lot of similarities, such as all need to manage their name, salary. That is, if we create two classes, the Trainer class, the sales class, then the two classes must have the same part. One of the big goals of object-oriented programming is reuse, how can we make the same parts of multiple classes reusable? Can be accomplished by virtue of the inheritance feature. It can be said that the main purpose of inheritance is to reuse. We can extract the same parts of multiple classes and put them in a class called the parent class, or the superclass, which other classes can inherit to reuse the properties and methods in the class, called subclasses, or derived classes. In the Java language, inheritance is done using the extends keyword.

Next, we use the code to show the employee classes in the training center.

 package com.csst.inherit; public class Employee {private String name;
    private double salary;
       Public employee () {} public employee (String name, double salary) {super ();
       THIS.name = name;
    This.salary = salary;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    Public double getsalary () {return salary;
    The public void Setsalary (double salary) {this.salary = salary;
}} package Com.csst.inherit;
       public class Trainer extends Employee {private String course; Public Trainer () {} public Trainer (string name, double salary, string course) {Super (name, S
              Alary);
       This.course = course;
       Public String GetCourse () {return course;
       } public void Setcourse (String course) {this.course = course;
}} package Com.csst.inherit;public class Sales extends Employee {private double task;
       Public Sales () {super ();
              Public Sales (String name, double salary, double task) {super (name, salary);
       This.task = task;
       Public double Gettask () {return task;
       public void Settask (double task) {this.task = task;
}} package Com.csst.inherit;  public class Testemployee {/** * @param args */public static void main (string[] args) {//TODO
       auto-generated method Stub sales Sale=new sales ("Kate", 3000,100000);
       System.out.println ("Sale.name:" +sale.getname () + "Sale.salary:" +sale.getsalary () + "Sale.task:" +sale.gettask ());
       Trainer trainer=new Trainer ("Rose", 3000, "Java"); System.out.println ("Trainer.name:" +trainer.getname () + "Tainer.salary:" +trainer.getsalary () + "Trainer.course:" +
    Trainer.getcourse ()); }
}

The results of the operation are:

Sale.name:Kate sale.salary:3000.0 sale.task:100000.0

Trainer.name:Rose tainer.salary:3000.0 Trainer.course:Java

In the above code, employee is the parent class, trainer and sales are subclasses and inherit employee through extends. Because of the inheritance of employee, trainer and sales can reuse methods in employee, such as Getname,getsalary, and of course, you need to be aware of permissions issues.

Inheritance is a "is-a" relationship, where a subclass object is an object of the parent class type. Objects such as trainer can be said to be an employee type object. In the Java language, it is important to remember that Java is a single inheritance, that is, a class can inherit only one parent class.

There are many other details in the inheritance, such as method coverage, construction methods, abstract classes, polymorphic parameters, etc., which are described in the following tutorials.

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.