The difference between abstract classes and interfaces (cattle off network copy)

 

 Class contains abstract modifier is the abstract class, abstract class instance object can not be created. Abstract class containing the method must be defined as abstract class, abstract class class method need not be abstract. abstract class class must implement the abstract methods defined in a particular (Concrete) subclass, therefore, is not abstract or abstract static constructor method. If the child does not like all abstract methods abstract parent class implementation, then the subclass must also be defined as abstract types. 
An interface (interface) can be said to be a special case of the abstract class, all methods in the interface must be abstract. The method defined interface defaults to public abstract type member variable type interface defaults to public static final. 
Compare the difference between the two following syntax: 
1. An abstract class can have a constructor, interface can not have a constructor. 
2. abstract class can have an ordinary member variables, the interface is not an ordinary member variables 
3. abstract class may contain non-abstract ordinary methods, all interface methods are abstract must not have non-abstract ordinary method. 
4. Access Type abstract methods abstract class may be public, protected, and (default type, although 
the eclipse is not given, nor it should), but the abstract method only public interface type, and the default is the public abstract type. 
The abstract class may contain static methods, static methods can not contain an interface 
variables 6. The abstract classes and interfaces can contain static member variables, static member variable access type abstract class may be any, but is only defined in the interface public static final type can be, and is the default public static final type. 
7. A class can implement multiple interfaces, but can only inherit an abstract class. 
The following then say that the difference between the two on application: 
the interface is more play a role in system architecture design method, used primarily for communication between the contract definition module. Abstract classes play a role in implementing aspects of the code, code reuse may be implemented, 

For example, the Template Method design pattern is a typical application of abstract class, assuming that all the Servlet class for a project should be carried out to determine the permissions the same way, record access logs and exception handling, then you can define an abstract base class, so All Servlet inherit the abstract base class, service methods privileges to complete abstract base class is determined, the access log record and exception handling code in each subclass only complete their business logic code, pseudo-code as follows: 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.lei;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public abstract class BaseServlet  extends HttpServlet {
 
  /**
  * serialVersionUID属性概述
 
  */
  private static final long serialVersionUID = 1L;
 
  public final void service(HttpServletRequest request,
  HttpServletResponse response)  throws IOException, ServletException {
  // 记录访问日志
  // 进行权限判断
  if ( true ) // if条件里写的是“具有权限”
  {
  try {
  doService(request, response);
  catch (IOException e) {
  // 记录异常信息
  }
  }
 
  }
 
  protected abstract void doService(HttpServletRequest request,
  HttpServletResponse response)  throws IOException, ServletException;
}
 
    
Implementation class as follows: 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.lei;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class MyServlet  extends BaseServlet{
 
  /**
  * serialVersionUID属性概述
 
  */
  private static final long serialVersionUID = 1L;
 
  @Override
  protected void doService(HttpServletRequest request,
  HttpServletResponse response)  throws IOException, ServletException {
  // TODO Auto-generated method stub
  
  }
 
}

Middle class method parent uncertain piece of code, leaving the subclass dry, use the Template Method design pattern. 
Note: This question of interpretation of the overall idea is to start with the basic concepts of abstract classes and interfaces, and then compare the syntax details of both, and finally say the difference between the two applications. 
The difference comparing details of the syntax is structured: to start a class constructor, common member variables and methods (including abstract method), six aspects static variables and methods, inheritance and the like one by one to compare answered, then the third who inherited the angle of the answer, especially the last with a typical example to show their deep technical knowledge. 

 

 

 

Guess you like

Origin www.cnblogs.com/jiexing/p/11627248.html