Chapter 8 of Java (1) Inheritance

Source: Internet
Author: User

Inheritance of class 8.1

The integrated class becomes the parent class or superclass, and the class obtained by integration is called subclass ). A parent class can have multiple child classes, but Java does not support multiple inheritance, so a class can only have one direct parent class.

A parent class is actually a set of public members of all child classes, and each child class is a special feature of the parent class. It is an extension of the functions and connotations of Public member variables and methods.

Java has a special class that becomes java. Lang. Object. All classes are obtained by directly or indirectly inheriting the class.

1) create a subclass

In Java, class inheritance is implemented through the extends keyword. subclasses can inherit all non-private members as their own members.

The format is as follows:

Class subclass extends superclass

{......}

Eg:

Class person

{

Private string name; // constructor and class member

Private int age;

Public Person ()

{

System. Out. println ("called personal constructor person ()");

}

Public void setnameage (string name, int age)

{

This. Name = Name;

This. Age = age;

}

Public void show ()

{

System. Out. println ("name:" + name + "Age:" + age );

}

}

Class Students extends person

{

Private string department;

Public student ()

{

System. Out. println ("student () called the student's constructor ()");

}

Public void setdepartment (string dep)

{

Department = Dep;

System. Out. println ("I am a" + department + "student ");

}

}

Public class app8_1

{

Public static void main (string [] ARGs)

{

Student Stu = new student (); // create a student object

Stu. setnameage ("zhangsan", 21); // call the setnameage () method of the parent class

Stu. Show (); // call the show () method of the parent class

Stu. setdepartment ("computer system"); // call the setdepartment () method of the subclass.

}

}

The execution result of the program is:

The personal constructor person () is called ()

Student ()

Name: zhangsan age: 21

I am a computer student

2) Call the specific constructor in the parent class.

Eg:

Class student extends person

{

Private string department;

Public student ()

{

System. Out. println ("students without parameter constructor student ()");

}

Public student (string name, int age, string dep)

{

Super (name, age); // call the parameter constructor of the parent class

Department = Dep;

System. Out. println ("I am a" + department + "student ");

}

}

3). Access the members of the parent class in the subclass.

Super can not only access the constructor of the parent class, but also access the member variables and member methods of the parent class, but super cannot access the members added in the subclass.

The format for accessing a parent class member is as follows:

Super. <variable name>

Super. <Method Name>

If a non-private member is in the parent class, the protected member can not only directly access the member in the parent class, but also access the member in the subclass.

4) overriding)

The concept of overwriting is similar to the method overload. They are one of the skills of the Java language "polymorphism" (polymorphism.

Override refers to defining the name, number of parameters, and type in the subclass with the same method as the parent class. It is used to override the function of the method with the same name in the parent class.

Note: The sub-classes cannot overwrite the methods declared as final or static in the parent class.

Once a method is overwritten, the parent class can call the method in the subclass. However, the parent class cannot call the method in the subclass.

Overwrite: The method name of the parent class. The number and type of parameters must be the same.

5). Non-inherited members and final classes

By default, all member variables can be overwritten. If the parent class idioms do not want the quilt class members to be overwritten, they can be declared as final. that is, the final variable, constant. can be accessed, but cannot be modified. this can increase code security.

If the member variable is declared as final, the variable cannot be inherited.

If a class is modified by the final modifier, it indicates that this class is the final class. This class cannot have subclasses. This class is called the final class.

6). Object Class

If a class does not use the keyword extends, the class is a subclass of the Java. Lang. Object Class by default.

Common Object methods:

Public Boolean equals (Object OBJ) // determines whether the two object variables direct to the same object

Public String tostring () // convert the pair sex that calls the tostring () method to a string

Public final class getclass () // return the class of the object that calls the getclass () method

Protected object clone () // returns a copy of the object that calls this method

 

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.