How I understand Java abstract classes and interfaces

Source: Internet
Author: User

In the interview we are often asked:What is the difference between abstract classes and interfaces in Java?

Then, we have to say that an abstract class can have a method, the interface cannot have the actual method, a class can inherit only one abstract class, but may inherit multiple interfaces, Balabala a large pile, as if the standard answer is familiar to say the same.

Abstract classes and interfaces This article describes their differences and connections, they do have many similarities, but in essence, or from a language design point of view, this is not their most essential difference.

Not suspense, my personal understanding of these two:

A class is an abstraction of a concrete instance, such as an abstraction of a JSON string, and an abstract class is an 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

Spit Groove

First of all, I have to spit out this interview, I think the interviewer always ask this kind of similar "to say the difference between abstract class and interface", "talk about the difference between the process and the thread" and so on, are not responsible for the performance.

Why is it?

One reason is that interviewers do not have their own evaluation criteria for the person they want to recruit, and another reason is that they are not responsible for the interviewer. This problem simply does not test the level of the interviewer.

Well, if I came to interview someone, I would ask: Would you please tell me how you understand abstract classes and interfaces; What would you say if you asked your grandmother to explain the difference between the process and the thread?

I think this can test the interviewer's understanding of the problem, and I think Microsoft's face test (how you explain Excel to your grandmother) tests a person's understanding of something (though, to this day, I can't really think about it well.) -)

The difference between an abstract class and an interface

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

A class is an abstraction of a real thing.

For example, the definition of a Benzcar class, it is necessary to the real Mercedes-Benz car has a very good abstraction (of course, Mercedes-Benz car has a lot of series, here is not dead). That means if you're going to build a Mercedes, you need to benzcar this class (the Mercedes-Benz is a instance in memory).

Then the abstract class is the abstraction of the class.

How do you understand it? That means there are a lot of car manufacturers to define a specification (car Class), said to build a car will need to have engines, tires, audio equipment ... (These are the equivalent of abstract methods), the specific use of engines, tires, audio equipment by each car manufacturer to do their own. So there are all kinds of cars, Mercedes-Benz, BMW-brand, Toyota ...

An interface is an abstraction of an abstract class.

That's just my personal understanding.

In our daily life can see a variety of "interface", power outlet is a kind. At first I was seeing Uncle Mouse's blog at the beginning of the understanding of the concept of "control rollover"--ioc/dip is actually a management idea | Cool Shell-coolshell.cn. Later, I thought, this thing is in fact everywhere, manufacturing power outlet factory and manufacturing electrical appliances as long as the agreement of an "interface"--two or three sockets, of course, each country's interface is different, the conversion between the interface needs to use the adapter.

In fact, the same in the program, for example, all the means of transport can be abstracted as an interface Drivable (perhaps due to empirical reasons, I consider not very perfect), the implementation of this interface class created objects (such as: cars, airplanes, ships, etc.) are can be driven

 Public Interface drivable{    publicvoid Drive ();

Then we can create a class that AbstractCar represents an abstraction of all cars, all cars that can be driven must inherit this class, and this abstract class prescribes some abstract methods, such as getEngine() methods, which show that each car's engine is different and needs to be customized in subclasses (of course, You can also inherit the AbstractCar class, which is an abstraction of all cars that might have the same engine.

Why is Drivable the drive() default implementation of the method, but the default implementation throws an exception directly?

In fact, this is a way to implement the interface, there is another way is to drive() set the abstract. These two implementations, I think functionally is the same, but from the class design is different.

The implementation in the following code, I refer to the design of the java.util.AbstractList<E> add(int location, E object) method in, it is written in the document:

* @throwsunsupportedoperationexception*ifAdding to ThisList is not supported. Public Abstract classAbstractcarImplementsdrivable { Public AbstractEngine getengine ();  Public AbstractWheel Getwheel (); @Override Public voidDrive () {Throw Newunsupportedoperationexception (); }    //omit other methods and properties}

The above code, then, drive() can be understood as:

By default "Car" is not open, you implement a car class, you need to override this method to implement your own drive method

Example of a list in the Java container

Find it in the source code, you will find List<E> the top of the inheritance relationship is that the Iterable list can be traversed , and it will also produce an Iterator interface object. This means that a list can be traversed by this iterator .

It's like the above saying that all vehicles are capable of driving , and that all the lists can be traversed.

The class becomes more specific as it goes down one layer at a level.

At last

Why can interfaces inherit?

In fact, this principle is very simple. Because there is always a most essential agreement to constrain everyone, such as all vehicles can be driven, all the easy can be traversed. Then the protocol becomes more specific:

iterable <-Collection <-list <-abstractlist <-list

From the bottom up, there is a layer of abstraction than a layer.

As I said at the beginning of the article,

    • You can use a ArrayList class to create many objects, that ArrayList is, an abstraction of these objects
    • And AbstractList It's an ArratList abstraction that you AbstractList can create, ArrayList create Stack , or LinkedList wait for.
    • ListAn interface is an abstraction of all the list classes
    • CollectionIs the abstraction of the container for all the single elements
    • Iterableis the highest level of abstraction, which means that all containers can be traversed.
Note:

There should be a lot of places I'm not thinking about, welcome to correct and discuss

How I understand Java abstract classes and interfaces

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.