Differences and similarities between interfaces and abstract classes in Java

  • 2020-04-01 03:55:49
  • OfStack

1. Abstract class:
(1). Concept: abstract class is the abstraction of a thing, that is, the extraction of the class. Abstract class is the whole class abstract, including properties, behavior. Java abstract classes, like Java interfaces, are used to declare a new type. And as a starting point for the hierarchical structure of a type.
(2). Format:


   public abstract class abstractDemo{
           
            private String name;
           
            public abstract void fun();
        }

(3). Note:
A: an abstract class does not have to have an abstract method, but A class with an abstract method must be defined as an abstract class.
B: in the Java language, there are two kinds of classes, one is concrete and the other is abstract.
Concrete classes can be instantiated, abstract classes cannot.
C: for the abstract class, if you need to add new methods, you can directly add concrete implementation in the abstract class, the subclass can not change;
D: abstract methods have declarations, not implementations. Abstract classes exist for inheritance. If you define an abstract class and don't inherit from it, you create the class for nothing.
E: for a parent class, if one of its methods does not have any meaning in the parent class and must be implemented differently according to the actual needs of the subclass, then the method can be declared as the abstract method and the class becomes the abstract class.
2. The interface:
(1). Concept: interface is the abstraction of behavior. An interface is an abstraction of a class part (behavior).
(2). Format:


  public interface test {
   
    public abstract void fun();
}


(3). Note:

An interface can contain variables and methods, but the variables in the interface are implicitly specified as public static final. Methods are implicitly specified as public abstract methods and can only be public abstract methods.

3. Difference and connection between interface and abstract class:

(1). Abstract class is the abstraction of a kind of thing, that is, the abstraction of class, while interface is the abstraction of behavior.
(2) abstract class is to abstract the whole class, including properties and behaviors, but the interface is to abstract the part of the class (behavior).
(3). Inheritance is a "yes" relationship, while interface implementation is a "no" relationship. If a class inherits an abstract class, the subclass must be the class of the abstract class, and the interface implementation is a matter of whether or not.
(4). The interface only gives the declaration of the method, not the implementation of the method. General methods that can have abstract methods in an abstract class. If it is an abstract method, there is only a declaration of the method. If it is a general method, there are both method declarations and method implementations.

4. Reasons for using the interface:

(1). No interface, pluggability is not guaranteed. Because Java is single inheritance.
(2). Any class in a class hierarchy can implement an interface. If the class implements the interface, it will affect all the subclasses of this class, but not all the parent classes of this class.
(3) a class has at most one parent class, but several interfaces can be implemented simultaneously.

5. Questions:

In the book JAVA programming ideas, abstract classes are defined as "classes that contain abstract methods", but later it is found that if a class contains no abstract methods, it is also an abstract class. That is, an abstract class does not have to contain abstract methods. Because if an abstract class doesn't contain any abstract methods, why design it as an abstract class?

Personal understanding:

Abstract class purpose is used for inheritance, in the definition, there can be no abstract methods, just using abstract modification is not syntax errors, but there is no practical meaning. Abstract methods in the abstract class is to let the subclass inherit the abstract class, to achieve abstract methods in the abstract class, according to the needs of the subclass to achieve different functions.


Related articles: