Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the object-oriented problems of Java?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the Java object-oriented problems". In the daily operation, I believe many people have doubts about the Java object-oriented problems. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the Java object-oriented problems?" Next, please follow the editor to study!

Question 1. What are the object-oriented features and your understanding and inheritance of these features?

1. Inheritance: inheritance is the process of obtaining inheritance information from an existing class to create a new class. The class that provides inheritance information is called the parent class (superclass, base class); the class that gets the inheritance information is called the subclass (derived class). Inheritance makes the changing software system have a certain degree of continuity, and inheritance is also an important means to encapsulate the variable factors in the program.

2. Encapsulation: it is generally believed that encapsulation binds data to methods that manipulate data, and access to data can only be accessed through defined interfaces. The essence of object-oriented is to depict the real world as a series of completely autonomous and closed objects. The method we write in the class is an encapsulation of the implementation details; we write a class is the encapsulation of data and data operations. It can be said that encapsulation is to hide everything that can be hidden, providing only the simplest programming interface to the outside world.

3. Polymorphism: polymorphism means that objects of different subtypes are allowed to respond differently to the same message. To put it simply, you call the same method with the same object reference but do different things. Polymorphism is divided into compile-time polymorphism and run-time polymorphism. If the method of the object is regarded as the service provided by the object to the outside world, then the run-time polymorphism can be interpreted as: when system An accesses the service provided by system B, system B has many ways to provide service. but everything is transparent to system A. Method overloading (overload) implements compile-time polymorphism (also known as pre-binding), while method rewriting implements runtime polymorphism (also known as post-binding). Runtime polymorphism is the quintessence of object-oriented, and there are two things you need to do to achieve polymorphism:

(1) method rewriting (the subclass inherits the parent class and overrides existing or abstract methods in the parent class)

(2) object modeling (referencing the subtype object with the parent type, so that the same reference calling the same method will behave differently according to the subclass object).

4. Abstraction: abstraction is the process of summarizing the common characteristics of a class of objects to construct a class, including data abstraction and behavior abstraction. The abstraction is only concerned with the properties and behaviors of the object, not the details of these behaviors.

Note: by default, object-oriented has three major features, encapsulation, inheritance, and polymorphism. If the interviewer asks for four features, then we will add the abstraction.

Question 2: the access modifiers public, private, protected, and the difference when not written (default).

The topic is relatively simple, and the differences between the different permission modifiers are shown in the following table.

Question 3: how to understand clone objects

1. Why use clone?

In the actual programming process, we often encounter this situation: there is an object A, which already contains some valid values in An at some time, and a new object B which is exactly the same as A may be needed. and any subsequent changes to B will not affect the value in A, that is, An and B are two separate objects, but the initial value of B is determined by the An object. In the Java language, using simple assignment statements can not meet this requirement. Although there are many ways to meet this need, implementing the clone () method is the simplest and most efficient way.

2. The difference between the process of new an object and that of clone an object

The new operator is meant to allocate memory. When the program executes to the new operator, it first looks at the type after the new operator, because it knows the type so that it knows how much memory space to allocate. After allocating memory, call the constructor to populate the fields of the object. This step is called object initialization. After the constructor returns, an object can be created and its reference (address) can be published externally. This reference can be used externally to manipulate the object.

Clone in the first step is similar to new, both allocate memory, when calling the clone method, the allocated memory is the same as the original object (that is, the object that calls the clone method), and then use the corresponding fields of the original object to fill the domain of the new object. After filling, the clone method returns, a new same object is created, and you can also publish the reference of this new object to the outside.

3. The use of clone object

(1) the difference between copying objects and copying references

Person p = new Person (23, "zhang")

Person p1 = p

System.out.println (p)

System.out.println (p1)

When Person p1 = p; is executed, is a new object created? First of all, look at the printed results:

Com.itheima.Person@2f9ee1ac

Com.itheima.Person@2f9ee1ac

As you can see, the printed address value is the same, and since the address is all the same, it must be the same object. P and p1 are just references, and they both point to the same object Person (23, "zhang"). This phenomenon can be called the replication of references. After the execution of the above code is complete, the scenario in memory is as follows:

And the following code really clones an object.

Person p = new Person (23, "zhang")

Person p1 = (Person) p.clone ()

System.out.println (p)

System.out.println (p1)

As you can see from the print results, the addresses of the two objects are different, that is, a new object is created instead of assigning the address of the original object to a new reference variable:

Com.itheima.Person@2f9ee1ac

Com.itheima.Person@67f1fba0

After the above code is executed, the scenario in memory is shown in the following figure:

At this point, the study of "what are the object-oriented problems of Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report