Three major features of Java Encapsulation Inheritance polymorphism-a simple summary

Source: Internet
Author: User

Briefly summarize

Encapsulation-that is, extracting the same code from many classes in a class.

The benefit is the reuse of code and security .

Inheritance-reduces the writing of the Code.

Its benefits are also the reuse of code .

Polymorphic-The different sub-class objects are considered as the parent class, you can block the differences between different sub-class objects, write generic code, and make general programming to adapt to the changing needs.

In general, it is the reuse of interfaces .

So to summarize, the three main features of Java are the reuse of code and Interfaces .

Encapsulation can hide implementation details and make the code modular;

Inheritance can extend existing code modules (classes);

Their purpose is to--code reuse.

And polymorphism is for another purpose--interface reuse! The role of polymorphism is to ensure that classes are called correctly when inheriting and deriving a property of an instance of any class in the family tree.

Here is my Baidu to explain, very detailed:

The three basic characteristics of object-oriented are encapsulation, inheritance, polymorphism. The encapsulation package is best understood. Encapsulation is one of the object-oriented features and is the main feature of object and class concepts.encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding. One of the main functions of inheriting object-oriented programming (OOP) languages is "inheritance".inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class. New classes created through inheritance are called "subclasses" or "derived classes." The inherited class is called the base class, the parent class, or the superclass. The process of inheritance is from the general to the special process. To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition). In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can have only one base class, and to implement multiple inheritance, it can be implemented by multilevel inheritance. The concept of inheritance is implemented in three categories: implementation inheritance, interface inheritance, and visual inheritance. Implementation inheritance refers to the ability to use the properties and methods of a base class without additional coding; Interface inheritance refers to the ability to use only the names of properties and methods, but the subclasses must provide the implementation; Visual inheritance is the ability of a subform (class) to use the appearance of a base form (class) and to implement code. When considering using inheritance, it is important to note that the relationship between the two classes should be a "belongs" relationship. For example, the Employee is a person and the Manager is a person, so these two classes can inherit the People class. But the Leg class cannot inherit the person class, because the leg is not a human. Abstract classes only define generic properties and methods that will be created by subclasses, and when creating abstract classes, use the keyword Interface instead of class. OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (inheritance and compositing) → Designing and implementing several stages with classes and instances. Polymorphism Polymorphism (POLYMORPHISN) is a technique that allows you to set a parent object to be equal to one or more of his child objects, and after assignment, the parent can operate differently depending on the attributes of the child object that is currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type. To achieve polymorphism, there are two ways, covering, overloading. Overwrite refers to the practice of redefining the virtual function of a parent class by a subclass. overloading, which means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (perhaps the number of parameters is different, perhaps the parameter types are different, perhaps the two are different). In fact, the concept of overloading is not "object-oriented programming", the implementation of overloading is: The compiler according to the function of a different parameter table, the name of the same name is decorated, and then the same name function is a different function (at least for the compiler). For example, there are two functions of the same name: function func (p:integer): Integer, and function func (p:string): integer; Then the compiler has done a modified function name may be this: Int_func, Str_func. The invocation of these two functions has been determined between the compilers and is static (remember: Static). That is, their addresses are bound (early bound) at compile time, so overloading and polymorphism are irrelevant!The true and polymorphic correlation is "overlay"。 When a subclass has redefined the virtual function of the parent class, the parent pointer is dynamically based on the different child-class pointers assigned to it (remember: it's dynamic!). Call to the function of the subclass, such that the function call cannot be determined during compilation (the address of the virtual function of the calling subclass cannot be given). Therefore, such a function address is bound at run time (late bonding). The conclusion is:overloading is just a language feature that is independent of polymorphism and is not related to object-oriented! Quote a WORD from Bruce Eckel: "Don't be foolish, if it's not late, it's not polymorphic." "So what is the role of polymorphism?" We knowencapsulation can hide implementation details, make code modular, inheritance can extend existing code modules (classes), and they are intended for-code reuse. And polymorphism is for another purpose--interface reuse! The role of polymorphism is to ensure that classes are correctly called when they are inherited and derived using a property of an instance of any class in the family tree。

Three major features of Java Encapsulation Inheritance polymorphism-a simple summary

Related Article

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.