Difference between Java interface and abstract class

Source: Internet
Author: User

I may be asked frequently during interviews. What is the function of interfaces? Compared with the class, why do you need to use the class to implement interfaces and so on. If you don't think about it, sometimes it gets stuck. This articleArticleThis section briefly introduces the functions of interfaces.
An interface is a product used to mark a class. Each interface corresponds to one or more classes that implement it. Otherwise, this interface becomes redundant.Code. Because management interfaces are much simpler than management interfaces, they reflect abstract points of view. In other words, an interface is a class without attributes and behaviors. Class implements multiple interfaces to solve the mechanism that the class cannot inherit multiple classes. How to Use the interface?
1. Functions of interfaces
There are two aspects:
1. the Java multi-state interface dynamically loads instances.
This implementation method can be seen in many occasions. The following is a sample:
1) interface class:

 
Public interface personservice {/*** calculate the monthly salary * SAL: wage base * type: employee grade */Public double calcumonthlysalary (double Sal, int type );}

2) implementation class:

Public class managerservicebean implements personservice {/*****/Public managerservicebean () {}/ * (non-javadoc) * @ see COM. java. test. interface. personservice # calcumonthlysalary (double, INT) */Public double calcumonthlysalary (double Sal, int type) {system. out. println ("managerservicebean:" + (Sal * type * 2); Return Sal * type * 2 ;}} public class employeeservicebean implements personservice {/***/Public employeeservicebean () {}/ * (non-javadoc) * @ see COM. java. test. interface. personservice # calcumonthlysalary (double, INT) */Public double calcumonthlysalary (double Sal, int type) {system. out. println ("employeeservicebean:" + (Sal * type * 2); Return Sal * type * 1.5 ;}}}

3) test class:

Public class salarymanagetest {/*** @ Param ARGs */public static void main (string [] ARGs) throws exception {try {// The write here is not intelligent, you also need to pass in the transmission personservice = (personservice) class. forname (ARGs [0]). newinstance (); If (ARGs [1]! = NULL & ARGs [1]! = "" & ARGs [2]! = NULL & ARGs [2]! = "") {Int sal = integer. valueof (ARGs [1]). intvalue (); int type = integer. valueof (ARGs [2]). intvalue (); // you can call different implementation classes based on different input parameters. // if a new class of members is added, we only need to add a new class, you do not need to modify the personservice method. calcumonthlysalary (SAL, type) ;}} catch (exception ex) {ex. printstacktrace ();}}}

In this case, when retrieving class objects through interfaces, I personally think that writing is not very good .. But maybe anyone who has used EJB should know that the client obtains the EJB bean in this way:

Java code

 
Personservice = (personservice) Context. Lookup ("personservicebean/remote ");

2. Passing Java interfaces as parameters
A sample is listed below:
1) interface. The implementation class is the same as above.
2) person entity class:

 
Public class person {private double Sal; private int type;/*****/Public Person () {}/*** @ Param Sal * @ Param type */Public Person (double Sal, int type) {This. sal = Sal; this. type = type;}/*** @ return the Sal */Public double getsal () {return Sal ;} /*** @ Param Sal the Sal to set */Public void setsal (double Sal) {This. sal = Sal;}/*** @ return the type */Public int GetType () {return type ;} /*** @ Param type the type to set */Public void settype (INT type) {This. type = type ;}}

3) interface as parameter input class:

 
Public class salarymanager {/*****/Public salarymanager () {} public void getmonthlysalary (personservice, person) {personservice. calcumonthlysalary (person. getsal (), person. getType ());}}

4) test class:

 
Public class salarymanagetest {/*** @ Param ARGs */public static void main (string [] ARGs) throws exception {try {managerservicebean = new managerservicebean (); employeeservicebean = new employeeservicebean (); salarymanager = new salarymanager (); // The call interface is the implementation class method of the incoming parameter salarymanager. getmonthlysalary (managerservicebean, new person (1000, 7); salarymanager. getmonthlysalary (employeeservicebean, new person (1000, 4);} catch (exception ex) {ex. printstacktrace ();}}}

Differences between interfaces and abstract classes
I personally think there are the following points:
1. abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship once. However, a class can implement multiple interfaces. This makes up for the multi-Inheritance Problem of classes.

2. abstract class can have its own data members or non-Abstarct member methods. In interface, only static data members that cannot be modified (that is, they must be static final, but data members are not defined in interfaces) are allowed. All member methods are abstract.

3. abstract class and interface have different design concepts. Abstract class represents the "is-a" relation, and interface represents the "like-a" relation. Note: When a behavior is not associated with the behavior method of a class, the interface should be used to implement this behavior. abstract classes cannot be used. Otherwise, the interface segregation principle is violated) rules and OCP principles.

4. classes that implement abstract classes and interfaces must implement all the methods in them. Abstract classes can have non-abstract methods. There is no implementation method in the interface.

5. the variables defined in the interface are of the public static final type by default and must be given the initial values. Therefore, the Implementation class cannot be redefined or changed. Of course, the variable definition can also be friendly.

6. the variables in the abstract class are friendly by default. Their values can be redefined in the subclass or assigned again.

7. The methods in the interface are public and abstract by default.

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.