Abstract class is a class containing abstract methods, abstract classes can contain non-abstract methods, but the abstract method must be defined in the abstract class, abstract classes cannot be instantiated, abstract classes in the abstract method must be implemented in a specific subclass.
Specific abstract class code for analysis:
Public Abstract classAnimal {//Abstract methods defined in abstract classes must be implemented in specific subclasses, so abstract constructors or abstract static methods cannot be used//Public Abstract Animal () {// // }//Public abstract static void Sleep (); PublicAnimal () {} PublicString Eye;//Normal member variables Public Abstract voidEat ();//Abstract Method Public voidDrink () {//Non-abstract methodsSystem.out.println ("All Animal drink water!"); }}Abstract classCrawlanimalextendsanimal{//if there are methods in the parent class that are not implemented by subclasses, the class should be defined as abstract classes Public Abstract voidJump (); }Abstract classFlyanimalextendsanimal{ Public Abstract voidfly ();}classDogextendscrawlanimal{//abstract classes are inherited@Override Public voidJump () {System.out.println ("Dog is jumping!"); } @Override Public voideat () {System.out.println ("Dog eat Bone"); } @Override Public voiddrink () {System.out.println ("Dog Drink Milk"); } }}
An interface is a special case of an abstract class, and all methods in an interface are abstract. The member variable of the interface defaults to the public static final type, and the method defaults to the public abstract type.
The specific interface code is as follows:
Public InterfaceAnimalinterface {//interface is not allowed to have a construction method//Public Animalinterface () {// // } Public Static FinalString eye = "BLACK";//no ordinary member variables, non-modifiable static member variables, need to initialize variables Public Abstract voideat (); voidSleep ();//Default public abstract//Public static void Jump (), no static method available in interface }classCrawlAnimal2Implementsanimalinterface{//interface is the implementation@Override Public voideat () {//TODO auto-generated Method Stub} @Override Public voidsleep () {//TODO auto-generated Method Stub } }
The difference between an abstract class and an interface is that
The differences from constructor methods, member variables, methods, access types, static methods and variables, inheritance syntax:
1. Abstract classes can have construction methods, and interfaces cannot have constructors.
2. There can be ordinary member variables in the abstract class, there are no ordinary member variables in the interface
3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.
4. The access type of an abstract method in an abstract class can be public,protected, but the abstract method in an interface is only of the public type, and the default is the public abstract type.
5. Abstract classes can contain static methods, and interfaces cannot contain static methods
6. Both abstract classes and interfaces can contain static member variables, and the access type of static member variables in an abstract class can be arbitrary, but the variables defined in the interface can only be public static final types, and the public static final type is the default.
7. A class can implement multiple interfaces, but can inherit only one abstract class.
The difference between the two applications: the interface is more in the system architecture design method to play a role, mainly used to define the communication contract between the modules. Abstract classes play a role in the implementation of code, and can implement code reuse.
This article for learning notes, if there is inappropriate, please point out the joint study to correct, thank you.
Abstract classes and interfaces for the Java Foundation