Object-oriented _ inheritance

Source: Internet
Author: User

First, the basic concept of inheritance:

1, inheritance is one of the three main characteristics of object-oriented

2. The inherited class is called the parent class (superclass), and the class inheriting the parent class is called a subclass (derived class)
3, inheritance refers to an object directly using another object's properties and methods (after inheritance to use)
4. Code reuse can be achieved through inheritance

II. requirements for the use of inheritance

1, the generality of each class is extracted and placed separately in a class to describe

2. And then let this class have something to do with every other class.

3, note: Do not want to get other class functions, simplify the code and inherit

4, must be the class and the class has the relationship between can inherit

5, for example: the general characteristics of students and workers (common attributes, common methods) description extracted. be described separately. Just let the students and the workers have a relationship with the class described alone.

Third, the benefits of inheritance:
1, improve the reusability of the Code
2, let the class and the relationship between the class, with this relationship, has the characteristics of polymorphism

Iv. inherited Syntax:

  

 Public classExtendsdemo { Public Static voidMain (string[] args) {Homechicken C1=NewHomechicken ();    C1.desc (); }}//Parent Classclasschicken{String name;  PublicChicken () {} PublicChicken (String name) {Super();  This. Name =name; }         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public voiddesc () {System.out.println ("I'm a fighting chicken."); }}//sub-classclassHomechickenextendschicken{}//sub-classclassPheasantextendschicken{}


v. Code examples:

1  Public classExtendsDemo1 {2      Public Static voidMain (string[] args) {3 4     }5 }6 classperson{7 String name;8     intAge ;9 }Ten classStudentextendsperson{ One     voidStudy () { ASystem.out.println ("Good study"); -     } - } the classWorkerextendsperson{ -     voidWork () { -System.out.println ("Good work"); -     } +}

Vi. the use of inheritance should be noted

1. Java does not support multiple inheritance:

Only single inheritance in the Java language supports multiple inheritance (one class inherits another class and cannot inherit other classes)

Because multiple inheritance is prone to security implications when the same functionality is defined in more than one parent class, but the feature content is not the same, the subclass object is not sure which one to run.

But Java retains this mechanism and uses another form of manifestation to accomplish the performance called multi-implementation.

2. Java supports multiple layers of inheritance:

The Java language supports multiple layers of inheritance, which is an inheritance system

3. How to use the functions in an inheritance system:

To use the system, first look at the description of the parent class of the system, because what is defined in the parent class is the commonality in the system. By understanding the Commonality function, we can know the basic function of the system.

4, when the specific call, to create the most sub-class object, why?
One, because it is possible that the parent cannot create an object (abstract class)
Second, is to create sub-class objects can use more functions, including the basic also includes the special
The simple word is: Check the parent class function, create subclass object use function

Seven, The relationship between things and things :

1, inheritance (who is a kind of)

2, agglomeration (who has who):

Aggregation (Like a class, less a person is not related, the degree of tightness is not so tight)

Combination (as if the hand is part of a person's body, it's a lot less related and tight)

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.