Talking about the similarities and differences between Java interfaces and abstract classes

Talking about the similarities and differences between Java interfaces and abstract classes

Abstract classes and interfaces of these two concepts bothering me for a long time, in my opinion, interfaces and abstract classes really very similar. Many have been looking for during the data refer to the great God of the brightest ideas, it can only be simple to understand the difference between the two on the grammar. Bite the bullet and make a small summary of a wave. Perhaps, after learning the knowledge, understanding it will be even more profound, to Oliver!

Grammatical differences

  • A class can only inherit a maximum of an abstract class, but can inherit multiple interfaces .

  • Variable declaration in the interface default is final , but the abstract class can contain non-final variables.
  • Interface members must all be public , and members of the class may be abstract usual style common class members, namely public, private and protected can be modified.
  • Interface Application keyword implementsto implement an abstract class should use keyword extendsexpansion.

  • If you want a general class implements an interface, it is all the interface methods must be implemented , but the situation inherited abstract class that allows derived classes with abstract modified, so as not to directly implement abstract methods abstract class.

  • An interface can inherit an interface, the abstract class can inherit other classes or more interfaces, and interfaces may be implemented in the abstract class without providing an interface implemented method.

  • Abstract class can have a constructor, but can not be used to create instances, only for the sub-class constructor to call it. And there are construction method does not allow interface .

Depth understanding

  • Before JDK1.8, implicit interface all abstract methods, there can be specifically implemented method, and ordinary methods can abstract class; after JDK1.8, the interface is allowed to declare a default method or a static method to provide a specific method to achieve.

Appear before the default method in the interface, as compared to the abstract class interface has a distinct advantage in that: a better forward compatibility (forward-compatibility), i.e. without damaging the class continues in the case of existing code add new features . But the emergence of the default method, so that the interface can also achieve this effect. The one mentioned on: On Java's default and static methods

  • An abstract class is facing "object" , it needs to provide is the "object" should have the basic properties of basic or functional behavior , just like people with age attribute will work, and so on. Enjoy the same abstract class inherits from objects of the same basic features, students, teachers, police uncle ...... they have aged, but also will work, it is that we may be different ages, doing live not the same as nothing. Which is actually a concept of "is-a", that is, "student class is a human."
abstract class People{
    private int age;
    public abstract void doSomething();
}
  • And the interface is for "function" , it is necessary to define objects that have a function , and no matter what the subject, I just need to know what you can do on the line, I do not care who you are. Like learning function, whether machine or students are in place, they can implement this interface you can, without regard to their own type. Then the functions or skills of an object is certainly a lot, then, to achieve multi-interface is very reasonable. Different from the relationship between the "is-a", the interface represents is a "has-a" concept, which means "object has the skills to learn" .
interface studyable {
    void howToStudy();

    default void info() {
        System.out.println("study is important for everyone..");
    }
}
  • Interfaces and abstract classes can implement multi-state, multi-state the obvious benefits of using the upward transition and dynamic binding, that the object is determined when the type of operation, greatly increased scalability.
public static void main(String[] args) {
    studyable[] studyables = new studyable[]{new Robot(), new Student()};
    for (studyable s : studyables) {
        s.howToStudy();
        s.info();
    }
    System.out.println("*************************************");
    People[] peoples = new People[]{new Student(), new Teacher()};
    for (People p : peoples) {
        p.doSomething();
    }

}
//测试结果
Robot comes out
Student comes out!
robot need to practice
study is important for everyone..
student will read book..
study is important for everyone..
*************************************
Student comes out!
Teacher comes out!
student should study..
teacher should teach..

参考链接:what is the difference between an interface and abstract class

Guess you like

Origin www.cnblogs.com/summerday152/p/12149563.html