Three basic features of object-oriented in Java

Source: Internet
Author: User

Reproduced from http://blog.sina.com.cn/s/blog_5f79a56a0100c6ig.html

As we all know, the three basic features of object-oriented in Java are: [encapsulation], [inheritance], and [polymorphism]

I. Encapsulation
Objects must have a clear boundary; Division of boundary (Objects perform their respective duties, object granularity, and object reusability)
Specifically:
1. attribute: Private (set and get methods are provided)
2. Method: public or private public/private
3. method declaration --> Public
Method implementation --> hide
There is no impact on the user of the object when changes are implemented.
 
Ii. Inheritance
Put the commonalities in the parent class, and put the features in the subclass; the Child class, the parent class --> is actually special --> General
1. Keyword: extends example: Dog extends animal {}
2. A class in Java can only have one direct parent class, that is, single inheritance (with simplicity and tree structure ),
In C ++, it is a single inheritance.
Java implements Multi-inheritance through interfaces.
3. All attributes and methods in the parent class can be inherited to the subclass. Private methods in the parent class cannot be inherited to the subclass.
4. Access modifiers in Java

Access Modifier

Access permission

Inheritance

Private

Internal

Cannot inherit

Default

This class + same package

The same Steamed Stuffed Bun class can be inherited

Protected

This class + same package + different sub-packages

Yes

Public

Public

Yes

 

5. Object Construction Process
(1) Allocate space
(2) recursively construct a parent class Object
A. parent class initialization attributes
B. parent class Constructor
(3) initialize attributes
(4) Call the constructor

6. Super
Usage:
Super () calls the constructor of the parent class and can only appear in the first line of the constructor.
Super. The method name super indicates the object of the parent class. It is used to call the method of the parent class.
Note: When writing a class, you must write a constructor without parameters by default.
When the first sentence is neither this () nor super (), it will be implicitly called here
The construction method of the parent class without parameters, that is, the implicit super ().

Iii. Polymorphism
Run-Time polymorphism.
The subclass object is placed in the reference of the parent class. For example, animal A = new dog is used as the parent class object.
1. polymorphism principle:
(1) The object type remains unchanged.
(2) the method defined in the reference type can only be called by reference.
(3) during runtime, find the method after the subclass overwrites according to the actual object type
Example:
The animal class has two methods: Eat () and sleep (), sleep () for 8 hours, and subclass dog has
The eat () method, sleep () method, and Wangwang () method.
Currently, animal A = new dog () is created. A. Wangwang () cannot be called, and A. Sleep () can be called to output sleeping for 6 hours.

2. Forced conversion of Objects
Format: Reference instanceof type
Whether the referenced object is consistent with the class. The return value is boolean.
Usage:
Animal A = new CAT ();
If (A instanceof dog)
{
DOG d = (DOG);
D. Wangwang ();
}
NOTE: If only DOG d = (DOG) A is used, an error occurs during running because a is cat rather than dog (the first article of the polymorphism principle)

3. Flexible transformation of Polymorphism
(1) used on the parameter list:
Public void M (A) {} can use any subclass object of Class A as a parameter
(2) used in return values:
Public a m () {} This method may return any subclass object of Class

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.