Differences between abstract classes and interfaces-a very in-depth article

Source: Internet
Author: User

Today, IT learners see a good article written by crabs, which is useful for sharing.

Address: http://www.itxxz.com/a/javashili/2014/0530/2.html

 

Abstract class and interface can be described as the dual functions in java. They complement each other and perform their respective duties. I believe that some practitioners are more casual before they grasp the mysteries, why is such a ambiguous pair in java? Let's talk about the design intent of java.Differences between abstract classes and interfaces


Normally, we always say that abstract classes can be used to write implementation methods, but interfaces are not allowed. There will be no embarrassing scenes during interviews or when new users ask? As a result, the crab was repeatedly asked, but did not think deeply. After being unable to endure, he began to search for it on the Internet. He also consulted the experts with considerable development experience, then I finally had to share some snacks.

I. Concept

In the concept of object-oriented, all objects are depicted through classes, but in turn, not all classes are used to depict objects, if a class does not contain enough information to depict a specific object, such a class is an abstract class.

Interfaces in java are a series of methods declarations and a set of methods features. An interface has only the features of a method and does not implement the methods, therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors.

Ii. Syntax

Abstract class
Abstract class Itxxz {
Abstract void wenzi ();
Void tupian (){
System. out. println ("I am an IT learner's website picture ");
}
}

Interface
Interface Itxxz {
Void wenzi ();
Void tupian ();
}

We can see from the above two definitions that the two methods define our website IT learners (itxxz) and provide the common text and image methods on the website, abstract classes can have their own method definitions, as in the green section above. As an old saying, everyone must be familiar with it.

3.In terms of Usage

During development, especially when subscribing to a protocol, you can always hear that you need a XXX interface, and seldom hear that you need a XXX abstract class. Why?

As a matter of fact, I have been able to feel that as an interface, it is provided to the outside, a bridge for communication between the two sides, and a channel for method calls. What about internal classes? When do we usually use internal classes?

If you often look at some open-source frameworks or researchApacheOpen-source projects on will find that abstract classes are often used for themselves. For example, will the dao Layer Design in the layered management of web projects often show that many mature frameworks inherit a base class similar to BaseDao, this base class defines our most commonly used methods, such as adding, deleting, modifying, querying, and paging. We do not want to let the outside world know or call these methods. The channels provided to the outside world are either tool classes or a reserved space. At that time, all protocol methods will be written there, this is the interface!

ThuJava design concepts

In the final analysis, java is an object-oriented language, which must be implemented in any way we think. Classes can be inherited, and the inheritance relationships are all "is a" relationships. Let's take a classic example in java programming ideology as an example:

We all know that there are two actions for the door: open and close. This is not a problem whether it is described using interfaces or abstract classes. But if this is a door with alarm functions, how should we design it?

If we put the door opening, closing, and alarm in the abstract class, is alarm an inherent attribute of the door? If you put them all in the interface, can opening, closing, and alarming be the same behavior?

It is always awkward to think about it, but it is difficult to express where it is inappropriate. We should demonstrate it through code:

Abstract class Door {
Abstract void open ();
Abstract void close ();
}

Interface Alarm {
Void alarm ();
}

The above defines a class (door) and an action (Alarm). The door opening and closing are the two execution actions of the door. What kind of door is this, what other functions can be used to unify these similar behaviors in the interface. The implementation result is as follows:

Public class Alarm extends Door implements Alarm {
Void open {...}
Void close {...}
Void alarm {...}
}

At this point, you should have another understanding of the differences between abstract classes and interfaces. If you have any questions, please add them. As a mature, open-source and well-sought-after language, everyone may have different starting points. You can share some new ideas.

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.