10day_java Object-oriented

Source: Internet
Author: User

1. Inheritance

(1) In Java, the inheritance of a class is to construct a new class on the basis of a class, and the new class is called a subclass, the existing class is called the parent class, and the subclass automatically owns the inheritable properties and methods of the parent class;

The members of the parent class can be called directly in the subclass;

  

Inherited formats & Uses

Using the Extends keyword

Format:

Class Subclass extends parent class {}

1 /*2 * Parent Class3 * Define Employee Class4  */5  Public classEmployee {6String name;//name7 8      Public voidWork () {9SYSTEM.OUT.PRINTLN ("Employees at Work");Ten     } One}
1 /*2 * Define research and Development department employee category. Inheriting employee Classes3 * Keyword extends4 * Subclass is develop, parent class is employee5  *     6  */7  Public classDevelopextendsEmployee {8      Public voidprint () {9 System.out.println (name);Ten     } One}
1 /*2 * Test Class3  */4  Public classTest {5      Public Static voidMain (string[] args) {6         //create an object for the development employee class7Develop d =NewDevelop ();8         //object of the child class, calling members in the parent class9D.name = "Zhang San";Ten d.print (); One d.work (); A     } -}

Operation Result:

(2) Benefits and considerations of succession

1, the emergence of inheritance to improve the reusability of code, improve the efficiency of software development;

2. The emergence of inheritance causes the relationship between classes and classes, and improves the precondition of polymorphism.

3, a class can have only one parent class, cannot have more than one parent class, only allow single inheritance, do not allow multiple inheritance;

4, Java support Multilayer inheritance, such as: Class B Inherits Class A, Class C inherits Class B, Class C has both the properties and functions of Class A and Class B;

5. In Java, subclasses and parent classes are a relative concept, that is, a class is a class parent class, or it can be a subclass of another class.

6, the extraction of things, the common extraction of each specific thing, sub-category of the common extraction, the formation of the parent class;

7, the Inheritance system: The parent class, the content most common, the subclass, not only has the generality, but also has own characteristic

(3) After inheritance, the characteristics of the member variables in the parent class of the subclass

Subclass Object Call member variable (nearest principle)

1, subclasses own, use their own; subclass no, call the parent class;

2. In the subclass, call the member of the parent class, using the keyword super. Call the member of the parent class;

3. Subclass (derived class) Inherits parent class (superclass, base class)

4, this. Call a subclass member;

5,super. Call the parent class member;

(4) After inheritance, the characteristics of the member methods in the parent class of the subclass

1, subclasses own, use their own; subclass no, call the parent class;

2. Rewriting the method (override)

The subclass overrides the method of the parent class when a method that is identical to the parent is present in the child class.

3. You can override a method in a parent class when the subclass needs the functionality of the parent class, and the feature body subclass has its own unique content.

(5) Considerations for method rewriting

1, the subclass method overrides the parent class method, must ensure that the child class method's permission is greater than or equal to the parent class permission;

2. Four permissions: Public protected default (not written) private

3, the wording should be noted:

Must be identical: The return value type of the method, the method name, and the parameter list are the same.

2. Abstract class

(1) Definition of abstract class

Method declaration is the same, but the method function body is different, only the method declaration is extracted, and the method body is not extracted, then this method is an abstract method;

Abstract method: No principal method, must use the keyword abstract decoration;

When the class that defines the abstract method must also be decorated with the abstract keyword, the class that is decorated by the abstract keyword is an abstract class.

(2) How to use abstract classes

An abstract class cannot instantiate an object and cannot be new;

The reason why the object cannot be created: Abstract method has no subject and cannot run at all;

How abstract classes are used: Defining classes to inherit abstract classes, overriding abstract methods, creating objects of subclasses

10day_java Object-oriented

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.