Interfaces and abstract classes

java interview to share the meaning and the difference between abstract classes and interfaces -------

First look at the definition:
接口abstract of behavior, which is a collection of abstract methods, API can be achieved using the interface definition and implementation of the purpose of separation. Interface can not be instantiated; not contain any non-const member of any feld is implied meaning of public static final; at the same time, there is no non-static way to achieve that is either abstract methods, either static method.

抽象类Class is not instantiated, modified by the keyword abstract class, its main purpose is code reuse. In addition to not instantiated, and the general form of Java class and not much different, there may be one or more abstract methods, may not abstract methods. Most common abstract class for extracting relevant Java class that implements the method or the common member variables, and then reaches the object code reuse through inheritance manner.

In particular, the behavior of the interface is abstract, you can do what is abstract, abstract class is by what you are doing.

For example ArrayList, the source of which is this:

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable

Among them, he inherits an abstract class and four interfaces.

Abstract class how to write it? Take a look at ArrayListthe inherited abstract class AbstractList:

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    protected AbstractList() {
    }
    public boolean add(E e) {
        add(size(), e);
        return true;
    }
    abstract public E get(int index);
    ...
}

We can easily find the abstract class has a specific way to achieve, abstract functions, etc.,

Let us look at ArrayList inherited interfacesList:

public interface List<E> extends Collection<E> {
   
    int size();

    boolean isEmpty();
    
    boolean contains(Object o);

   
    Iterator<E> iterator();
    
    Object[] toArray();
    
    <T> T[] toArray(T[] a);
    
    default void sort(Comparator<? super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator<E> i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }

As can be seen, all of the interfaces which methods do not function body (except the default method java8 added later), and then look at the source code ArrayList:

 public boolean add(E e) {
     ...
 }
 public E get(int index) {
     ...
 }
  public int size() {
     ...       
  }
public <T> T[] toArray(T[] a) {
     ...
  }

It is easy to see, ArrayListto achieve a succession of abstract classes and methods interfaces, then the realization of what not to achieve it? There are the following definition:

  • A general category (non-Interface) inherits the abstract class must implement all abstract methods, but not necessarily its non-abstract method overrides.
  • When one interface inherits the abstract class, not inherited all of the abstract methods.
  • An abstract class inherits another abstract class, they do not inherit all of the abstract methods.
  • When an ordinary class inherits an interface, all of its abstract methods must be rewritten.
  • When one interface inherits another interface, without having to inherit all of its abstract methods.
  • When an abstract class inherits the interface does not have to inherit all of its abstract methods.

In summary, we present the following differences

Similarities:
(1) not be instantiated
(2) interface class or subclass of abstract class implements the methods have only interface or abstract class to instantiate.

Different points:
(1) defines an interface only, not for realizing the method, java 1.8 can define default method bodies, and there may be an abstract class definitions and implementation, the method may be implemented in an abstract class.
Keyword (2) for the implementation of the interface implements, inherited abstract class keyword extends. A class can implement multiple interfaces, but a class can only inherit an abstract class. So, using the interface may indirectly achieve multiple inheritance.
(3) interface to emphasize specific functions, and abstract classes emphasize affiliations.
(4) Interface member variables default to public static final, must initial value, can not be modified; all its members methods are public, abstract of. Abstract class member variables default default, can be redefined in a subclass, can also be reassigned; abstract abstract methods are modified, can not be private, static, synchronized and native and other modifications, must end with a semicolon, not with flowers brackets.
(5) interface is used to frequently used functions, to facilitate future maintenance and add deleted, abstract classes are more inclined to act as a kind of public, does not apply to code changes later re opposites. Abstract class when accumulation function required by the interface when no accumulation is required.

Interfaces vs abstract class vs class

  1. Support multiple inheritance: interface support; abstract classes are not supported; classes are not supported;
  2. Support abstract functions: semantic interface support; Support abstract class; class does not support;
  3. It allows functions to implement: the interface is not allowed; abstract class support; class allows;
  4. Examples of allowed: the interface is not allowed; abstract class is not allowed; class allows;
  5. Allows partial function implemented: the interface is not allowed; abstract class allows; class is not allowed.
  6. The content definition: public interface functions, and can include constants public static fnal; abstract class with no any limitation.
  7. When to use it: When you want to support multiple inheritance, or to define a type use interfaces; when it is intended to provide with a "template" partial implementation of the class, and will implement some of the features necessary to delay use of abstract classes; if you plan on giving use the full realization class.
Published 30 original articles · won praise 33 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42568510/article/details/104021722