Java basics: 33rd parent class and inheritance

Source: Internet
Author: User

In the real world, we can classify various objects, and classes have a certain level and level. Next we will take the classification of familiar animals as an example to introduce the characteristics of classification.

Specializers classify animals according to their various characteristics (morphology, cell, genetics, physiology, biochemistry, ecology, and Earth distribution), that is, natural classification. That is, the seven main levels of the world, door, outline, object, section, genus, and species. These are the basic units used for classification. Each animal can give them a proper name and position in the rank sequence.

The giant panda belongs to the animal community, Jiwei animal door, nursing class, carnivals, the giant panda family, and the giant panda family.

The status of modern humans in the biological classification system is: Animal, spine animal, mammal, rankeye, human, human, and intellectual.

At this level, the higher the level of abstraction, the more common features of the next level of classification are abstracted, the less attributes there will be, because the abstract process is actually the process of extracting commonalities and removing individuality, It is the intersection of attributes of the next level of classification.

At this level, we add feature attributes when describing down. For example, when we describe spine animals, spine animals are all animals with spine in the body, in addition to the three basic features of the spine, the spine, the back neural tube, and the branching, the spine also has some other characteristics (http://baike.baidu.com/view/345393.htm ).

That is to say, the spine is first an animal and then has some notable characteristics. In this way, the attributes of the spine are the attributes that the animals have and the attributes that the spine animals have.

In the real world, classification and class hierarchies are the same as those of objects in software systems.

1. parent class, subclass, ancestor class, and descendant class

In the hierarchy of classes, classes in the adjacent hierarchy are called parent classes at the top layer, and classes in the lower layer are called Child classes.

Child classes have all attributes of the parent class, and child classes have more attributes than the parent class. For example, a spine has all attributes of an animal, while a spine has some special attributes.

If an object belongs to a subclass, it must belong to the parent class. For example, if a person belongs to a spine, a person must belong to an animal.

The parent class contains more objects. Animals include not only all spine animals, but also non-spine animals. They are abstract objects that can represent more objects.

In the class hierarchy, all layers above a class can be called the ancestor class of the current layer. For example, the animal community, the spine animal door, the mammal class, And the arniaceae class can all be called the ancestor class of the human family.

In the class hierarchy, all layers under a class can be called the Child classes of the current layer. For example, the spine animal door, the mammal class, the arniaceae and the human family all belong to the children of animals.

In some cases, superclasses are used to represent parent classes. Normally, parent classes and ancestor classes are not strictly distinguished, and child classes are not required.

2. Single inheritance and multi-Inheritance

In the real world, when we classify in a certain way, the hierarchy of classes is usually represented as an inverted tree. The specific characteristics are as follows: each node in the tree can have one parent class or no parent class (top-level). It can have multiple child classes or no child classes (bottom-layer ). This is called single inheritance.

In some cases, according to the classification standard, a subclass may have multiple parent classes. For example, the employees in the school are classified and divided by work conditions, it can be divided into students, teachers, and logistics. It can be divided into Party members and the masses according to whether they are Party members. In this way, for a student member, his parent class can be a student or a member, and there are two parent classes. There are two or more parent classes called multi-inheritance.

In Java, single inheritance is directly supported. Multi-inheritance must be implemented through the implementation interface. The interface is described later.

3. Implement inheritance

Use the keyword extends to implement inheritance. For example, if Class B inherits Class A, it can be written:

Public Class {

// Attributes in parent Class

}

Public Class B etends {

// Unique attributes of B

}

After class B is inherited, the attributes of Class B include the newly defined attributes of class B and the attributes inherited from Class.

4. Object Class

The object class is the ancestor class of all classes and the top-level class.

If a class does not define a parent class, the parent class is an object.

The parent class of Class A is not declared, and its parent class is object.

The usage of objects is described separately later.

5. abstraction and Derivation

In 3, we first define class A and define attributes in Class. Then Class B is defined, inherited from Class A, and some attributes are added. In this way, Class B attributes include attributes inherited from Class A and attributes redefined. This process is called from Class A to Class B.

How do I know what attributes are in the parent class, what attributes are in the subclass, and how many classes are to be created?

This is a task that needs to be completed for Object-Oriented Analysis and Design. The primary purpose of the parent class is to share code among multiple child classes, that is to say, we extract repeated code from multiple child classes to form a parent class. This is an abstract process that abstracts the common code of each child class. For example, the course selection system of a school contains students and teachers. Normally, the primary and secondary classes are created, but the student and primary classes contain the same attributes and methods, such as name, gender, and contact information. If you want to maintain this information in both the student and student categories, You need to modify the attributes when they are changed, in addition, there is a problem of code duplication. At this time, we can consider extracting the common attributes and methods to generate a new class, as the parent class of the student class and student class.

The following code shows the process:

Before Abstraction:

Public class student {

Public string name;

Public String studentid;

Public string class;

Public String sex;

Public String address;

Public String phone;

// Related methods

}

Public class teacher {

 

Public string name;

Public String teacherid;

Public String department;

Public String sex;

Public String address;

Public String phone;

// Related methods

}

After Abstraction:

Public class person {

 

 

Public string name;

Public String sex;

Public String address;

Public String phone;

// Sharing Method

}

Public class student extends person {

Public String studentid;

Public string class;

// Student-Specific Method

}

Public class teacher extends person {

Public String teacherid;

Public String department;

// Unique methods of the category class

}

Can all methods and attributes be inherited? What can be inherited? Please refer to the following explanation.

Last time: 32nd talk about static membersNext: 34th lecture package Li xucheng csdn blog: Why? U= 124362 & C = 7be8ba2b6f3b6cc5

 

 

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.