In the interview, the interviewer asks, what do you understand about object-oriented programming? I think a lot of people would recite the interview questions, object oriented, inheritance, encapsulation, polymorphism? Note that the interviewer is asking for your understanding of object orientation, not for you to memorize concepts.

Object oriented, is the simulation of the real world, below we simply simulate an animal world.

Object oriented one of the three basic characteristics of inheritance, here Primat inherited from Animal, Person inherited from Primat, inheritance is very simple, not to say more, look at the following code implementation, code comments are more important, please focus on.






In code, whether it’s an animal, a bird, a human, or a monkey, we can abstract it into a class. A class is a template for an object, and with the new keyword, we can create one object after another. Animal1 and Animal2 are the same Animal. When we call the same eat() method, we will intelligently match the implementation of the animal1 and animal2. This is called polymorphism.

Polymorphism has to satisfy three conditions.

First, there should be inheritance; Person inherited from Animal, Monkey inherited from Animal. Eat overrides the parent class’s eat method. (Red box content)

Person4 has access to the name, sex, eat(), printAge() methods, but not to the age, isLady() methods. This is because we added the private keyword in front of the property and method. We hide the age attribute and the isLady() method that we don’t want to expose to the client (in this case the client is main), but we provide a printAge method to the client to print the age, but before we print the age, we do a bunch of things with the age (we don’t print the woman’s age).

As for the hidden object attributes and implementation details, only the specified methods are exposed to control the access and modification of attributes in the program, which is called encapsulation. (Here we don’t provide a set method for age, we can’t change it at all, we provide a printAge() method for external access).

At work, we may have a requirement that when a Person object has the same name, age, and gender, it is treated as the same Person. If you’ve read my equals column in Java before, you probably don’t want to compare equals or equals directly. Here’s the result.



When == and equals fail, developers might write the following code to determine

When writing the age logic, we find that the age attribute is private and there is no getAge() method, so we can not obtain the age attribute. We may provide a getAge() method in the Person class or remove the private keyword of age. However, this will cause subsequent users of this class to stop calling printAge to printAge and instead directly access the age property or getAge() method, exposing the woman’s age. Obviously, this breaks our previous encapsulation of age and is not recommended.

Age is private, so why don’t we just move this code into the Person object? Good idea. Comparing two objects for equality should not be decided by the client, but by the objects themselves. This is also one of the object-oriented tricks.

With that in mind, we’ve extended a method in Person,



Everything seemed to work out perfectly, and it turned out exactly as we had hoped. The problem seemed to be solved.

We know that the elements in the Set are not placed in order and cannot be repeated. Please consider the following code:

I hope that after reading this chapter, readers have their own views and thoughts on object oriented, rather than the six words, polymorphism, inheritance, encapsulation that blindly appear in the interview questions. You can even smile at the interviewer and say, oh, I’m not dating yet and have plenty of time to devote to my work.

In the comments section, someone said Animal Primat should be declared as an abstract class instead of an interface, but you can do either. A class can implement multiple interfaces, but it can only inherit one class. The interface declaration is more flexible, and also makes the implementation class has better extensibility.

Note: This column was first published on the public account saysayJava. All sample codes have been uploaded to the public account, please pay attention to download.

If you liked this series, please give me a thumbs up or share it with me. Your support is what keeps me going, and leave a comment in the comments section where you can find out what you want to know about this column.

Reprint unlimited welcome, but please indicate “author” and “original address”. Please keep this paragraph in the article, thank you for your respect for the author’s copyright. For commercial reprint or publication, please contact the author for authorization.

Java Automatic boxing/unboxing – Java stuff

Next: Talk about equals (in) -Java