JAVA programmers must read: Basics (2) object-oriented programming concepts

Source: Internet
Author: User
If you have never used object-oriented language before, you need to understand this concept before writing JAVA code. You need to understand what is an object, what is a class, how is the relationship between an object and a class, and how messages are used to communicate between objects. The previous sections of this tutorial will describe the concept of object-oriented programming, and the subsequent tutorials will teach you how to compile this concept into code.
2.1 What is an object
Objects are software sets of related variables and methods. Software objects are often used to imitate objects around us in the real world. Objects are the key to understanding object-oriented technologies. Before learning, you can look at objects in real life, such as dogs, tables, TVs, and bicycles. You can find that objects in the real world share two common characteristics: they both have states and actions. For example, a dog has its own status (such as name, color, birth, hunger, etc.) and behavior (such as tail shaking ). A bicycle also has its own status (such as the current position and two wheels) and behavior (such as braking, acceleration, deceleration, changing the position and so on ).
The software object is actually the shape of the real world object because it also has state and behavior. A software object uses one or more variables to maintain its state. A variable is a data item named by a user identifier. The software object uses its method to execute its behavior. The method is a function (subroutine) associated with the object ).
You can use software objects to represent objects in the real world. You may want to use an Animation Program to represent a dog in the real world, or a program that can control an electronic bicycle to represent a bicycle in the real world. Similarly, you can use software objects to shape abstract concepts. For example, an event is a public object used in the GUI window system. It can represent the response of a user clicking the mouse button or pressing the buttons on the keyboard.
Figure 1 shows a public visual representation of a software object.
(Figure 1)
The state and behavior of software objects can be expressed in variables and methods of objects. To construct a real-world bicycle, the software object must have a variable indicating the current status of the Bicycle: The speed is 20 mph, and its current position is the third. These variables are well-known instance variables because they contain the states used for special bicycle objects, and in object-oriented technology, special objects are called instances.
As shown in Figure 2, a bicycle shape is used as a software object.
(Figure 2)
2.1 What is an object
In addition to variables, software bicycles also have methods for braking, changing the pedal pace, and changing the gear. These methods are well-known instance methods because they check or change the status of special bicycle instances.
The above object graph shows that the object variables constitute the center of the circle. The method is around the variable and hides the core of the object from other objects in the program. Variables that wrap objects using the protection method are called encapsulation. This object graph is the ideal representation of objects and is also the final goal of object-oriented system designers. However, this is not all content. Generally, for some reason, an object may expose some of its variables or hide some methods. In JAVA programming language, an object can specify one of the four access levels for variables and methods. This access level determines which object and class can access variables or methods. Access variables and methods in JAVA can be converted to member functions that control the callback class. Encapsulating related variables and methods into a concise software set is a simple and powerful method that provides two main benefits for software developers:

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.