Difference between abstract class and interface in java

Difference between abstract class and interface in java

Features of abstract classes
:
1. Methods can be constructed in
abstract classes 2. Common properties, methods, static properties and methods can exist in abstract classes.
3. Abstract methods can exist in abstract classes.
4. If there is an abstract method in a class, then the current class must be an abstract class; an abstract class does not necessarily have an abstract method.
5. An abstract method in an abstract class needs to be implemented by a subclass. If the subclass does not implement it, the subclass also needs to be defined as abstract.
Interface
1. In an interface, there are only method declarations, no method bodies. 2. There are only constants in the interface, because the defined variables will be added with public static final
by default when compiling. 3. The methods in the interface are always modified by public. 4. There is no constructor in the interface, nor can the object of the interface be instantiated. 5. The interface can implement multiple inheritance 6. The methods defined in the interface need to be implemented by the implementation class. If the implementation class cannot implement all the methods in the interface 7. The implementation class is defined as an abstract class.





 

 

A class with the abstract modifier is an abstract class, an instance object that cannot be created by an abstract class. A class with an abstract method must be defined as an abstract class, and the methods in an abstract class class do not have to be abstract. abstract class
An abstract method defined in a class must be defined in a concrete
(Concrete) is implemented in subclasses, so there cannot be abstract constructors or abstract static methods. If the subclass does not implement all the abstract methods in the abstract superclass, then the subclass must also be defined as abstract.
An interface can be said to be a special case of an abstract class, and all methods in an interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to the public static final type.
Let's compare the syntax differences between the two:
1. An abstract class can have a constructor, but an interface cannot have a constructor.
2. There can be ordinary member variables in the abstract class, but there are no ordinary member variables in the interface
3. An abstract class can contain non-abstract ordinary methods, all methods in the interface must be abstract, and there cannot be non-abstract ordinary methods.
4. The access type of an abstract method in an abstract class can be public, protected and (the default type, although
No error is reported under eclipse, but it should not work), but the abstract method in the interface can only be of public type, and the default is public abstract type.
5. An abstract class can contain static methods, but an interface cannot contain static methods
6. Both abstract classes and interfaces can contain static member variables. The access type of static member variables in abstract classes can be arbitrary, but variables defined in interfaces can only be of public static final type, and the default is public static final type.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325633305&siteId=291194637