Using Java to implement object-oriented chapter II

Source: Internet
Author: User

Tag: value type cannot strong OID top must be variable property design

Chapter II inheriting one, using inheritance

inheritance is one of the three major object-oriented special Java only single inheritance is supported. Inheritance expresses The relationship of is a.

in the Java in, all the Java classes are inherited directly or indirectly. Java.lang.object class, all classes are Object sub-class

1. Writing the parent class

Class Pet {

Public properties and methods

}

2. Writing subclasses, inheriting parent classes

Class Dog extends Pet {

Subclass-specific properties and methods

}

Class Penguin extends Pet {

}

Second, about the modifier can be quilt class inheritance

Inherit public and protected decorated properties and methods, regardless of the same package.

Inherit the properties and methods of the default permission modifier, only in the same package

Properties and methods for private adornments cannot be inherited

Cannot inherit constructor of parent class

Access modifiers

Local

Same package

Sub-class

Other

Private

Default

Protected

Public

Ii. understanding of Inheritance

1. Subclass access to parent class members

Using the Super keyword

Accessing parent class Properties

Super.name; (Super represents the parent object)

2) Accessing the parent class method

Super.print ();

2. Some parent class members cannot inherit

1) Private Member

2) The subclass is not in the same package as the parent class, and the member using the default access permission

3) Construction method

3. Access modifier protected

1) Properties and methods can be modified

2) This class, the same package, sub-class can access

4.

1) Super must appear in the subclass (the method or constructor of the subclass), no other location

2) can access the parent class member (can be inherited)

3) access to the parent class constructor must be in the first statement of the subclass constructor

Iv. Design in accordance with IS-A relationship use inheritance

In 1.Java, inheritance (inheritance) is implemented by the extends keyword, where subclass is called a subclass, superclass is called a parent class, a base class, or a superclass.

2. A method that inherits from a parent class can be rewritten as required in a subclass, called a method override or method override.

The rewrite of the method satisfies the requirements:

Five, the construction method

1. The construction method cannot be inherited and therefore cannot be overridden.

2. Call the constructor method of the parent class in the subclass?

Super ();

Super (parameter table);

3.super represents the default reference to the immediate parent class object of the current object. Members of the parent class can be accessed through the Super keyword in a subclass.

Super's requirements

4. Calling rules for constructing methods under inheritance conditions:

5. Use this and super precautions:

Vi. construction methods in rewrite and inheritance relationships

overriding the print () method of the parent class

Rewrite: A method that inherits from a parent class can be rewritten as required in a subclass, called a method override or method override.

I. Method overrides must meet the following requirements:

1. The overriding method and the overridden method must have the same method name

2. The overriding method and the overridden method must have the same argument list

3. The return value of the overridden method must be the same as the return value type of the overridden method, or its child class.

4. The override method cannot narrow the access rights of the overridden method.

Second, what is the difference between overloading and rewriting? ”

Overloading: Overloading a method with the same name in the same class requires the same method name, with different parameter lists, regardless of the return value type, the access modifier.

Overriding: Overriding is a method of the same name between a subclass and a parent class, requiring the same method name, the same argument list, the same return value type, and the access modifier not strictly to the parent class.

Vii. keywords of abstract

1. Abstract and abstract methods are modified by the abstract keyword.

2.abstract can also be used for methods-abstract methods

1) There is no method body for abstract methods

2) Abstract methods must be in an abstract class.

3) An abstract method must be implemented in a subclass, unless the subclass is an abstract class.

public abstract void print ();--no method body

3.final modifier

1) A class that is final decorated and can no longer be inherited

2) Using the final modification method, cannot be overridden by the quilt class

3) A final modified variable (including member variables and local variables) becomes a constant and can only be assigned once.

Note: P57

4.abstract can be shared with private, static, final

Using Java to implement object-oriented chapter II

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.