Everyone together and Snailren learn java-(a) object introduction

Source: Internet
Author: User

OOP, which is a feature of the Java language. Object-oriented thinking runs through the entire Java development.

So what is object-oriented? What is an object?

In the object-oriented design language, everything is an object. An object in life, has its own attributes, and has its own activities. For example, a car, tires, seats, etc., can also speed up, slow down. This is the property and method in Oop. By programming, each object is linked together, through the method to pass the message to each other, thus constituting a set of procedures.

Each object has its own type, such as a BMW car, and its "class" can be used for automobiles. Objects all belong to a large class. Therefore, if you are under a specific class, all objects can receive the same message. In other words, such as cars under the car, SUVs, buses, are different objects, but if the "Step on the throttle" instructions, these objects can accept the instructions and do their own processing. Each object has an interface. That is, every object is done by means of methods. Through the method, the object can provide services to the outside. When designing an object, it can improve the cohesion of the program if it is designed with the idea of "providing service".

There are two roles in the development and use of a program. The first is the developer of the class. This developer has developed a library to provide services. The second is obviously the user of the program. The user develops the application through the library developed by the developer. Therefore, as a developer of the class or in the development of the class, you need to pay attention to the design of the hidden part. Hiding some data or methods can not only prevent users from mistakenly manipulating sensitive and vulnerable parts when they call. It also allows the developer of the class to modify the hidden part of the program in a transparent manner to the user. Use public, private, protect, and so on for access control.

After the class has been created, then we are going to use the class. If we want to reduce the amount of code, or facilitate the extension of the program, maintenance, robustness, and so on, it can be used in the form of reusable classes to achieve the purpose. There are two ways to reuse code. The first is "combination", which combines the ready-made classes into a new class. One is inheritance. You can also reuse code by extending and overwriting existing classes.

We know that encapsulation, inheritance, polymorphism are the three main features of object-oriented. Java polymorphism is in fact the most important feature in code reuse. Polymorphic achievement of the interface! Generalization (generic) is a form of representation of polymorphism. Different parameter types, so that you know exactly what code to run when the code is running. This is very different from process-oriented procedures. Process-oriented programs are specific to an absolute address for each function during the compilation phase. This is "early binding". Java is late-bound, and when an object sends a message, it knows what code to run. The compiler's role is to ensure that the method being called exists, and that the parameters and return values are validated. Java calculates what method needs to be called by using the information stored in the object.

Also, in Java, all objects are inherited from the object class. The benefit is to ensure that all objects have some common functionality that simplifies object creation and garbage collection. When a Java object is created, it is created by new to allocate space and is created in the heap. When an object is unreachable, it is automatically reclaimed by the GC. Here we can see that the creation and recycling of objects are two different ways to go. GC needs to monitor the individual objects through soft pointers, which is one of the reasons why Java performance is poor. Really become also GC, FAIL also GC.

Everyone together and Snailren learn java-(a) object introduction

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.