Abstract class + interface in Java

Source: Internet
Author: User

Usage:

In abstract class mode, a class can have its own data member, or it can have a non-abstract member method, and in the implementation of the interface method, the class can only have static data members that cannot be modified (that is, must be static final , but the data members are not generally defined in interface, all of the member methods are abstract. In a sense, interface is a special form of abstract class.

reason for existence:

Further understanding of the introduction of Java into the abstract class, interface purposes, to the expert to ask the answer is as follows:

1, from the hierarchical structure of the class, the abstract class is at the top of the hierarchy, but in the actual design, generally speaking, abstract classes should be in the back to appear. Why? In fact, abstract class acquisition is a bit like the extraction of mathematics common divisor: ax+bx,x is abstract class, if you do not have the previous formula, how do you know if x is common divisor? At this point, it is also in line with the process of understanding the world, first concrete and then abstract. So in the design process if you get a lot of concrete concepts and find their commonalities from them, the set of commonalities is that abstract classes should be right.
2, interface on the surface, and abstract class very similar, but the usage is completely different. Its basic function is to bring together some unrelated classes (concepts) to form a new, centrally manipulated "new class". One of the typical examples I give to students is "driver". Who can be a driver? Anyone can, just pick up the driver's license. So I don't care if you are a student, white collar, blue collar or boss, as long as you have a driver's license.
Interface Driverlicence {
Licence getlicence ();
}

Class Studentdriver extends Student implements Driverlicence {
}
Class Whtiecollaremployeedriver extends Whtiecollaremployee implements Driverlicence {
}
Class Bluecollaremployeedriver extends Bluecollaremployee implements Driverlicence {
}
Class Bossdriver extends Boss implements Driver {
}

When I define the "car" class, I can specify "driver".

Class Car {
Setdriver (driverlicence driver);
}
At this time, car objects do not care what the driver is doing, the only thing they have in common is to get a driver's license (all implemented the Driverlicence interface). This, should be the most powerful place of the interface is also the abstract class incomparable.
Summary: Abstract classes are common divisor that extract specific classes, and interfaces are meant to "hash" some unrelated classes into a common group. Usually we develop a good habit is to use the interface, after all, Java is a single inheritance, unlike C + +, but in the need to use abstract classes must still be used (a bit like the use of Goto), hehe.

Reference Links:

http://www.cnblogs.com/xiaoao808/archive/2008/03/11/1100886.html//Good at speaking

Http://dev.yesky.com/436/7581936.shtml

Abstract class + interface in Java

Related Article

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.