Java Object-oriented programming: encapsulation, inheritance, polymorphism

Source: Internet
Author: User

Three main features of Java object-oriented:

encapsulation: through the Java class to achieve the encapsulation of data and operation methods, the outside world can treat each Java class as a black box, just call the method provided by the black box to complete the operation you want.

inheritance: through the inheritance of the class, the unified function is centralized in the parent class, which realizes the reuse and maintainability of the code.

Polymorphism : by overloading, rewriting and covering, different morphological features of different classes are realized.


A class member that is defined as private is such that it cannot be accessed by all code outside of the class, including subclasses.


If the constructor of the parent class is not explicitly called in the subclass, the default constructor of the parent class is called, if any.


Mutual conversions between parent and child classes

In Java we can assign a reference to a subclass to the object of the parent class, and then those members of the subclass that are not inherited from the parent class are no longer visible, and we can convert the parent class to a subclass type by casting the type, and those members become visible, what happens when the conversion occurs?


A class in the construction of the time, the first of its various levels of the parent class are constructed, the family is to Qi Qi whole. Write a test example here to see if you can get the parent reference.

It appears that you cannot get a reference to the parent class instance.


Classes that must be inherited: abstract classes

Classes that cannot be inherited: the final class


The private member method of the parent class cannot be overridden by a class method, so the private type method defaults to the final type.


Final method

If its subclasses are not allowed to overwrite a method, you can declare this method as the final method.

There are two reasons to use the final method:

Lock the method to prevent any inherited classes from modifying its meaning and implementation.

Efficient, the compiler goes into the embedding mechanism when it encounters the final method, which greatly improves execution efficiency.


For a variable of the final static type, static causes the variable to have only one copy, and final makes it immutable.


Final member assignment rules:

The final member can and can only be initialized once.

The final member must be initialized at the time of declaration, or in the constructor method, and not elsewhere.


What are the differences between classes, abstract classes, and interfaces?

Java Object-oriented programming: encapsulation, inheritance, polymorphism

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.