Java12-java Grammar Basics (11) Inheritance

Source: Internet
Author: User

Java12-java Grammar Basics (11) Inheritance

First, inheritance

Learning Requirements:

1. Understanding the concept and role of inheritance

2. Mastering the realization mechanism of inheritance

3. Understanding overrides and overlays in succession

4. Mastering the use of super keyword

Review:

1, analysis of the objective world, abstract into a class, and the use of Java language implementation.

2, the use of private packaging.

3. Use the This keyword to refer to the data members and methods in the class.

1. The concept and role of succession

The same kind of things have both generality and their own particularity.

1) Inheritance mechanism

In a general-special structure formed by a general class and a special class , the properties and operations common to the object instances of the general class and all special classes are defined at once in the General class, The properties and operations defined in a generic class are no longer duplicated in special classes, and special classes automatically have the properties and operations defined in the generic class (and all the higher-level generic classes). Objects of a particular class have all the properties and operations of an object of its generic class (unless restricted), which is called a special class's inheritance of a generic class .

2) Inheritance Characteristics

(1) Pass: Class C Inherits Class B, Class B inherits Class A, and Class C has properties and methods inherited from Class B, as well as properties and methods inherited from Class A.

(2) The hierarchical relationship between classes: it simplifies the understanding and description of objective things, and can clearly reflect the hierarchical relationship between related classes.

(3) Software reuse function: Class B inherits from Class A, so when you build Class B, you only need to describe the characteristics (data members and member methods) that are different from the parent class (Class A). This method can reduce the amount of code and the redundancy of data, and greatly increase the reusability of the program.

(4) Maintainability: Reduce the interface between the modules and interface, greatly enhancing the ease of maintenance of the software.

(5) Multiple inheritance mechanism: interface mechanism to implement multiple inheritance, that is, a subclass has more than one parent class.

Report:

Coupling: The degree of tightness between modules and modules

Clustering: Functions inside the module

In development: high cohesion, low coupling good.

2. Implement the inheritance mechanism

In Java, inheritance is implemented through extend

Format:

Class subclass name Extend parent class name {

Subclass data members

Child class Member methods

}

Attention:

(1) Parent class: base class, Super class----> General class

Emphasis: In Java, the base class for all classes is the Object class: Java.lang.Object

(2) Subclass: derived class----> particularity

(3) In inheritance, subclasses automatically have accessible members of the parent class.

Classroom Exercises: Transport type

Note: Although a subclass has a parent class property but cannot directly access the private members of the parent class, non-private members can access

3. Override of methods and property overrides

(1) Method overwrite: Subclass has a member method with the same name as a non-private member method of the parent class

You must ensure that the quilt class override method cannot have more restrictive access than the parent class.

(2) Property overrides: Subclasses have data members with the same name as non-private data members of the parent class

In a data member override relationship, the member method uses data members in accordance with the nearest principle.

4. Super keyword

(1) You can access data members with the same name overridden by the quilt class in the immediate parent class: Super. Data member name

Classes directly after the parent class---> Extends

In Java, there can only be single inheritance, and only one of the immediate parent classes of a class.

(2) You can access a member method of the same name overridden by the quilt class in the immediate parent class: Super. Member method name (parameter);

(3) Call the constructor method of the immediate parent class, must put the first line: Super ();

Emphasis: The parent class must display a write-out empty construction method

The order in which the constructor method is called in the inheritance mechanism: The Father class again sub-class

5, Upward transformation

Parent class: Animal

Sub-Category: Cat,dog

Upward transformation:

Animal dog = new Dog ();

Animal cat = new Cat ();

Description: The animal type of dog can call methods of the dog subclass, and the animal type of dog can invoke the method of the dog subclass.

Transition up to hide some implementation details

Ii. Final and instanceof

(i) Final keywords

1, Modified class: The modified class cannot be inherited, that is, the subclass cannot be derived

Final class{}

2, modify the member method: The method of decoration can not be covered by the method of the Quilt class

Final public void Printinfo () {}

3, decorated data members: the modified data member or local variable represents a constant, cannot change its value, then the general data members are all uppercase, the words are separated by an underscore.

Final private String name;

Note: A common class that is final modified: String, System ...

Emphasis: When a subclass cannot overwrite a parent class method, the member method name in the subclass is the same as the member method name of the parent class, which means that a method with the same name as the parent class cannot appear in the subclass.

4. Final modification Reference variable: The variable can always refer to an object, but it may change the object's contents

(ii) instanceof keyword/operator

Java has hundreds of commonly used class libraries, in the software development process to create a lot of different types of objects , often need to determine which object belongs to which class , using instanceof keyword to determine which class an object is an instance of.

1. Syntax format:

Object instanceof class;

A result of TRUE indicates that the object is an instance of the class; False indicates that the object is not an instance of class

Example:

Class a{}

Class B extends a{}

Test:

A = new A ();

b b = new B ();

b insctaceof B;//true

b insctaceof A;//true

A insctaceof B;//false

Result: After the object quilt class is created, it is both an instance of the child class and an instance of the parent class.

(iii) Summary

1, the main role of the final keyword?

(1) Modifier class, cannot be inherited

(2) Modification method, not allowed to overwrite

(3) Modifier member variable, not allowed to change, constant

(4) Modifying a reference type variable, always referencing an object, but can change the object's contents

2. What are the final keyword retouching data requirements?

(1) Modifier local variable can only be assigned one time

(2) Modified non-static data members must be initialized in the constructor method, or directly initialized

(3) The modified static data member must be initialized

3, why use instanceof keyword?

Determine if an object is an instance of a class

Java12-java Grammar Basics (11) 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.