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

How to understand Java abstract classes and interfaces

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

Share

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

In this issue, the editor will bring you about how to understand Java abstract classes and interfaces. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

In interviews, we are often asked: what is the difference between abstract classes and interfaces in Java? Then, we said that abstract classes can have methods, interfaces can not have actual methods; a class can only inherit one abstract class, but can inherit multiple interfaces, balabala a lot, like saying the standard answer skillfully.

Abstract classes and interfaces this article talks about their differences and relationships, they do have a lot in common, but in essence, or from a language design point of view, this is not their most essential difference.

Regardless of the point, my personal understanding of these two:

A class is the abstraction of a concrete instance, such as an json string; an abstract class is the abstraction of a class; an interface is an abstraction of an abstract class, and an interface is more like a protocol.

Listen to me slowly.

Make a sarcastic comment

First of all, I have to complain about this kind of interview. I think it is irresponsible for the interviewer to ask questions like "tell the difference between abstract class and interface", "tell the difference between process and thread" and so on.

Why?

One reason is that the interviewer does not have his own evaluation criteria for the person he or she wants to hire, and another reason is that he is not responsible to the interviewer. This kind of question can not test the level of the interviewer at all.

So, if I came to interview someone, I would ask: please tell me how you understand abstract classes and interfaces; if you were asked to explain the difference between processes and threads to your grandmother, how would you explain it?

I think this can test the interviewer's understanding of the question. I think Microsoft's interview question (how do you explain Excel to your grandmother) tests a person's understanding of something (although, so far, I can't figure it out very well). -)

The difference between abstract classes and interfaces

When it comes to abstract classes and interfaces, you have to talk about classes.

A class is the abstraction of real things.

For example, defining a BenzCar class requires a good abstraction of real Mercedes-Benz cars (of course, there are many series of Mercedes-Benz cars, and there are no pitfalls here). In other words, if you want to build a Mercedes-Benz, you need a class like BenzCar (this Mercedes-Benz is an Instance in memory).

Then an abstract class is an abstraction of a class.

How do you understand it? That is to say, there are many automakers working together to define a specification (Car class), saying that to build a car, you need engines, tires, audio equipment. (these are equivalent to abstract methods.) specific engines, tires, and audio equipment are done by each car manufacturer. So there are all kinds of cars, Mercedes-Benz, BMW, Toyota.

An interface is an abstraction of an abstract class

This is only my personal understanding.

We can see all kinds of "interfaces" in our daily life, and the power outlet is one of them. At first I saw that Mouse Uncle's blog began to understand the concept of "control flipping"-IoC/DIP is actually a management idea | Cool Shell-CoolShell.cn. Later, I thought, this thing is actually everywhere. Factories that manufacture power sockets and factories that manufacture electrical appliances only need to agree on a kind of "interface"-- two sockets or three sockets. Of course, the interfaces are different in each country. Adapters are needed for conversion between different interfaces.

In fact, the same is true in the program, for example, all vehicles can be abstracted into an interface Drivable (perhaps due to experience, I do not think very well), indicating that the objects created by the classes that implement this interface (such as cars, airplanes, ships, etc.) are driveable public interface Drivable {public void drive } then, we can create an AbstractCar class that represents an abstraction of all car classes, which all cars that can drive must inherit this class, which specifies some abstract methods, such as getEngine, and of course, you can also inherit the AbstractCar class, abstracting all cars that may have the same engine. Drive is set to abstract. I think the two implementations are functionally the same, but different in class design. For the implementation in the following code, I refer to java.util.AbstractList* @ throws UnsupportedOperationException * if adding to this List is not supported.public abstract class AbstractCar implements Drivable {public abstract Engine getEngine; public abstract Wheel getWheel; @ Override public void drive {throw new UnsupportedOperationException;} / / omitting other methods and attributes} by default, "car" cannot be driven. After you have implemented a car class, you need the Override method to implement your own drive method. Take List in the java container as an example.

Look in the source code and you will find that the top level of inheritance relationship is Iterable, which means that List can be traversed, and it also produces an Iterator interface object. This means that a list can be traversed through this iterator.

As mentioned above, all vehicles are drivable, and all lists are traversable.

As you go down layer by layer, the class becomes more specific.

Why can interfaces be inherited?

Actually, this principle is very simple. Because there is always a most essential agreement to bind everyone, such as all vehicles can be driven, all easy can be traversed. Then the agreement will gradually become more specific:

Iterable

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