Inheritance of the characteristics of Java

Source: Internet
Author: User
Tags modifier

Inheritance --the way to solve

inheritance solves code reuse and allows our programming to be closer to human thinking. When multiple classes have the same attributes (variables) and methods, you can abstract the parent class from these classes (like just student), define these same properties and methods in the parent class, and all subclasses do not need to redefine these properties and methods. You only need to declare the inherited parent class through the extends statement:

Syntax:class Subclass extends parent class

In this way, subclasses automatically have certain properties and methods defined by the parent class.

Inheritance --in-depth discussion

1. What attributes (variables) of the parent class are inherited by the method quilt class? Not all properties and methods of the parent class can be inherited by the quilt class

Parent Class sub-class

Public property; Public property;

protected property; inherit the protected attribute;

private property; → properties ;

attributes ;

public method; public method;

protected method; protected method;

Private method; method ;

method ;

2. Conclusion

as you can see from the diagram, the properties and methods of the public modifier of the parent class; The properties and methods of the protected modifier; The default modifier properties and methods The quilt class inherits, and the properties and methods of the private modifier of the parent class cannot be inherited by the quilt class.

Inheritance --considerations

1, subclasses can inherit at most one parent class (refer to direct inheritance)

2, Java All classes are subclasses of the object class (all subclasses can be inherited in succession, for example: Sun------son)

3. There are 202 packages in JDK6 3,777 classes, interfaces, exceptions, enumerations, annotations, and errors

4, in the development of the time, we strongly recommend that you check the JDK Help document

5, in the use of classes, really do not know how to do, more use of search engines

Defining improvements to classes

After the package is presented, the definition of our class is more complete:

Class class Name {Class class name {class Name {package name;

member variables ;   → member variables ;   → member variables ; →class class Name {

} member Method ;        construction Method ; member variables ;

} member Method ; construction Method ;

} member Method ;

}↓

↓←←←←←←←←←←←←←←←←←←←←←←←←←

Package name;

Class Name extends parent class {pending

member variables ; →....

construction Method ;

member Methods ;

}

Method Overloading (overload) Concept

Simply put: A method overload is a variety of implementations of the same function in a class, whichever method is used, depending on the parameters given by the caller.

Precautions:

1. Same method name

2, the method parameter type, the number, the order has at least one different

3. The method return type can be different (only the return type is not the same and cannot form an overload)

4. The modifier of the method can be different (just control the access modifier, cannot form the overload)

method Overrides (override) Concept

Simply put: The method overrides is the subclass has a method, and the parent class A method of the name, return type, parameter, then we say that the subclass of this method overrides the parent class that method. like the last case. The Cry method in the Cat class overrides the Cry method of the animal class.

Precautions:

Methods covered by a lot of conditions, some of the books are relatively thin, the total is said to have 2.1 fixed note:

1, the method of the subclass of the return type, parameter, method name, and the parent class return type, parameter, method name exactly the same, or compile error.

2. The subclass method cannot reduce the access rights of the parent class method.

Inheritance of the characteristics of Java

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.