Inheritance: Class, superclass, and subclasses; inheritance of superclass

Source: Internet
Author: User
Tags class manager

Inheritance: Class, superclass, and subclasses; inheritance of superclass

Http://user.qzone.qq.com/1282179846/blog/1470248763

Introduce a simple example:

// Import java. util. *; public class Employee {private String name; private double salary; private Date hireday; public Employee (String n, double s, int year, int month, int day) {name = n; salary = s; GregorianCalendar calendar = new GregorianCalendar (year, month-1, day); hireday = calendar. getTime () ;}public String getName () {return name;} public double getsalary () {return salary;} public Date gethireday () {return hireday ;} public void raisesalary (double bypercent) {double raise = salary * bypercent/100; salary = salary + raise;} // public static void main (String [] args) {// Employee [] staff = new Employee [3]; // staff [0] = new Employee ("diyige ); // staff [1] = new Employee ("dierge",); // staff [2] = new Employee ("disange ); // for (Employee e: staff) // {// e. raisesalary (5); //} // for (Employee e: staff) // {// System. out. println ("name =" + e. getName () + ", salary =" + e. getsalary () + ", hireday =" + e. gethireday ());//}//}}
// Manager class public class Manager extends Employee {private double bonus; public Manager (String n, double s, int year, int month, int day) {super (n, s, year, month, day); bonus = 0;} public void setbonus (double B) {bonus = B;} public double getsalary () {double basesalary = super. getsalary (); return basesalary + bonus;} // public static void main (String [] args ){//}}
//Testmanagerpublic class Testmanager {    public static void main(String[] args) {        Manager boss = new Manager("bossa",80000,1987,12,15);        boss.setbonus(5000);        Employee[] staff = new Employee[3];        staff[0]=boss;        staff[1]= new Employee("diyige",50000,1989,10,1);        staff[2]= new Employee("dierge",40000,1990,3,15);        for(Employee e : staff)        {    System.out.println("name="+e.getName()+",salary="+e.getsalary());        }    }}

1/The keyword extends indicates inheritance. Indicates that a new class is being constructed and derived from an existing class. An existing class is called a superclass, a base class, or a parent class. A new class is called a subclass, a derived class, or a child class.

Sub-classes have more functions than superclasses. (It's a nonsense)

During the design, general methods should be placed in the superclass, and special-purpose methods should be placed in the subclass.

2/to access a method in a subclass to access a superclass, use the specific keyword "super. For example:

public double getsalary()    {        double basesalary = super.getsalary();        return basesalary+bonus;    }

You can add fields, add methods, and overwrite superclasses in subclass. However, you cannot delete any integration fields or methods.

3/In the above Code, there are:

public Manager(String n,double s,int year,int month,int day)    {        super(n,s,year,month,day);        bonus = 0;    }

Where

super(n,s,year,month,day);

The constructor that calls the hyperclass containing the n, s, year, month, and day parameters. Because the sub-class constructor cannot access the private fields in the super class, you must use the constructor in the super class to initialize the private fields. We can use super to call the super class constructor.

The super statement that calls the constructor must be the first statement of the sub-class constructor.

Related Article

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.