Getting started with Java-object-oriented Basics

Source: Internet
Author: User

Getting started with Java-object-oriented Basics

1. Overview.
Java is an object-oriented language. Because Java is an object-oriented language, the birth of this language requires five basic features:
1) Everything is an object.
2) a program is a collection of objects.
3) each object has its own storage composed of other objects.
4) each object has another type. Each object is an instance of a class.

5) all objects of a specific type can receive the same message.

In Java, some of the concepts we commonly use are always used, but we don't know why we need them. Below I will explain them in a Q & A manner.

Ii. Some object-oriented questions.
2.1 Why does access control exist? What does it mean?
1). Make client programmers unable to touch what they should not touch-this part is necessary for internal operations on the data type, but is not required by users to solve specific problems.
2). allows the library designer to change the internal working mode of the class without worrying about the impact on the client programmer.
For the above reasons, the invention of the access-specific word determines who can use the things that follow it.
Public: indicates that the element following it is available to anyone.
Private: no one except the creator of the type and the internal method of the type can access the elements.
Proteced: similar to private, the inherited class can access the protecte member, but cannot access the private member.
Java has a default access permission package. The access permission class can access members of other classes under the same package, but the access control box private is the same for those outside the package.
2.2 What is a combination? What is aggregation?
Combination: the new class can be composed of any number of other objects of any type to implement the desired functions in the new class. The new class is a combination of existing classes.
The Dynamic occurrence of a combination is called aggregation.
2.3 what is a container? What are the containers in Java? How to select a container
When solving a problem, you do not know how many objects are needed or how long they will survive. I don't know how to store these objects when Java releases a container. In this way, we do not need to know how many objects will be placed in the container in the future. We only need to create a container object and then process all the details on it. Java has various types of containers that meet different requirements. For example, List is used to store sequences and Map is used to establish associations between objects. Set each object type only holds one.
Different containers provide different types of interfaces and external behaviors. Different containers have different efficiency for some operations. ArrayList and rule list. The random access element in ArrayList is an operation that takes a fixed period of time. The randomly selected elements in the shortlist must be moved in the list.
2.4 What is inheritance?
Inheritance is to solve this problem. When you create a class and there is a similar class, we usually create a new class. If we copy these classes based on the existing class this can also be achieved. The basic class is called the source class.
2.5what method does Java use to create and destroy objects? Are there several ways to create and destroy objects?
There are two ways to create and destroy objects:
First, you can determine the storage space and lifecycle of an object during writing. You can place the object in a stack or static storage area. This method improves efficiency but sacrifices flexibility.
The second method is to dynamically create objects in the heap memory pool. This method only knows how many objects are needed and how their lifecycles are during running. And their specific types.
Java uses the dynamic memory allocation method. Every time you create a new object, you must use the new keyword to construct a dynamic instance of this object. The language compiler that creates an object in the stack can determine the inventory time of the object and automatically destroy it. If the object is created on the stack, the compiler knows nothing about the object.
2.6 how does Java operate objects? Why should an object be assigned an initial value?
Java uses a reference operation object. Why do we need to assign an initial value? Just like a remote control, a remote control can exist independently but is not associated with a TV. The safest way is to initialize an object when it is referenced.
Iii. Summary.
Some concepts in Java come into being for some reason, not just from thin air. We not only need to know how to use these things, but also what problems they are produced to deal.

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.