Object-oriented encapsulation, inheritance, polymorphic understanding

Source: Internet
Author: User
Tags control characters

Packaging

1. What is encapsulation

Encapsulation (encapsulation) is also known as the hidden implementation (hiding the implementation). is to expose only the external interface of the code unit, and hide its specific implementation.

In fact, everywhere in life are packaged, mobile phones, computers, televisions these are packaged. You just need to know how to operate them, don't need to know how they are constructed, how to implement this function.

2. How to Implement Encapsulation

In programming, encapsulation is often achieved through access control. C++,JAVA,AS3 have public, Protected, Private, and other access control characters. By exposing information to public, private,protected information is hidden to achieve encapsulation.

3. The meaning of encapsulation

A. Encapsulation makes it more secure and easy to modify the code. The code is divided into relatively separate units.

As long as your computer's interface is not changed, such as USB interface Power Interface, no matter how the computer updates later, how powerful, as long as these interfaces remain unchanged, you will still use the computer.

The benefit of encapsulation is that it clearly indicates that those properties and methods are externally accessible. So when you need to adjust the code for this class, just make sure the public property is the same, the parameters and the return value type of the public method are the same, then you can modify the class without affecting the rest of the program, or other programs that use the class. That's why the tighter you control your code access, the more freedom you'll have to change your code later.

B. Encapsulation greatly reduces the overall complexity of software development.

You can use other people's classes very well without worrying about how their internal logic is implemented. You can easily learn to use code written by someone else, which makes it much less difficult to develop software collaboratively.

C. Encapsulation also avoids the problem of naming conflicts.

Encapsulation has an isolation effect. The buttons on the phone and the buttons on the TV remote are certainly of different use. But they can all be called keys, why don't you get mixed up? Apparently one belongs to the phone class one belongs to the remote control class. Different classes can have methods and properties of the same name, but are not confused.

Inherited

The core idea is not just to reuse existing code, but to create new classes with some of the classes already in place.

The main idea of inheritance is to use the object of the subclass as the object of the parent class .

An example of a specific instantiation is a bird as a parent, a magpie as a subclass, a bird including a magpie, a magpie is a bird.

Composite

When it comes to inheritance, you have to mention compounding, which combines only some of the existing classes to form a new class.

In the program design, the composite is embodied in the new class generated to use the existing class instance.

For example, a car is made up of tires, engines, and glass, where we define tyres, engines, glass, but not cars. So now when we define the car class, we just need to combine the tires, engine, glass and so on.

When to use inheritance, when to use composite?

In the actual programming, the use of composite frequency is far more than inheritance, for the novice, to be cautious with inheritance, use complex.

  A. Consider inheritance when you need to use an upward conversion.

B. When the two classes are combined, if it is possible to formulate a property of another class for the secondary class, that is, when the parent-child relationship is inherited, the proposed secondary class cannot be used entirely as another type of attribute to use, and the compound is used. For example, birds and sparrows, cars and tires.

Polymorphic

Polymorphic definition: means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)

Conditions for polymorphism:

First, to have inheritance;
Second, to have a rewrite;
The parent class reference points to the subclass object.

Benefits of Polymorphism:

Polymorphism is replaceable for existing code.

Polymorphism has extensibility for code.

It embodies the flexible operation in the application, and improves the use efficiency.

Polymorphism simplifies the process of coding and modifying the application software, especially when dealing with the operation and operation of a large number of objects.

implementation of polymorphism in Java: interface implementation, inheriting the parent class for method overrides, and method overloading in the same class .

Cases:

 package   Com.deng;  public  class   Deng { public  void      Qidong () {System.out.println ( "light on"  public  void   Guanbi () {System.out.println (" light off "
 package   Com.deng;  public  class  Dengguan extends   Deng { public  void   Qidong () {System.out    . println ( "lamp on"  public  void   Guanbi () {System.out.println (" lamp off "
 package   Com.deng;  public  class  Dengpao extends   Deng { public  void   Qidong () {    System.out.println ( bulb on " public  void   Guanbi () {System.out.println (" bulb off "
 package   Com.deng;  public  class   House { public  void      Kai (Deng d) {d.qidong ();  public  void   Guan (Deng d) {d.guanbi (); }}
 Package Com.deng;  Public class Test {    publicstaticvoid  main (string[] args) {        =New house ();         New Dengpao ();        Dengguan DGNew  Dengguan ();                H.kai (DP);        H.guan (DP);                H.kai (DG);        H.guan (DG);}                    }

The output is:

Bulb turn bulb off lamp switch off lamp

Object-oriented encapsulation, inheritance, polymorphic understanding

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.