Abstract class (abstract) interface (interface)

table of Contents

What are abstract classes and interfaces

Abstract class

The difference between abstract classes and ordinary classes

interface

The difference between abstract classes and interfaces


What are abstract classes and interfaces

Abstract class

 Method comprising one or more abstract class called abstract class, an abstract class can have specific methods and variables.

An abstract class is modified with abstract, must be public or protected (default is public)

abstract public void test();
//方法没有实现,留给子类去实现(所以抽象类不能是private,因为private子类无法继承也就无法实现)

 

Abstract class provides a template for subclass subclass of abstract class must implement the relevant

 

The difference between abstract classes and ordinary classes

1, abstract method must be public or protected, public default; the general category may also be default or private, default is default.

2, abstract class can not create object.

3, if you want to inherit an abstract class, and you want to create a sub-class object, then the sub-class specific methods abstract class must implement. If it does not provide an implementation, then the sub-class is also an abstract class.

 

interface

Interface also abstract than the abstract class, interface generates a completely abstract class, it does not provide any specific implementation.

Interface may contain variables and methods, a default public static final variable, and it is only public static final, default public abstract method, and is only public abstract. Under normal circumstances it is not defined in the interface variables.

interface test{
    (public static final)int VALUE=0;
    (public abstract)int getValue();
}

 

The difference between abstract classes and interfaces

1, a class can only inherit (extend) an abstract class (single inheritance in java), but they can achieve (implement) multiple interfaces ( "multiple inheritance").

2, the abstract class can have their own variables can have non-abstract methods (in one or more class method called abstract abstract class), and the interface can be static variable (static) and can not be modified (final ), the method can only be abstract method (abstract).

3, want to inherit an abstract class, and you want to create a sub-class object, then the sub-class specific methods abstract class must implement. If it does not provide an implementation, then the sub-class is also an abstract class. The subclass implements the interface, you must put all the abstract methods of the interface fully realized. An interface can inherit an interface, then the parent class does not need to implement the interface.

 

 

 

Published 38 original articles · won praise 6 · views 1899

Guess you like

Origin blog.csdn.net/weixin_43827227/article/details/104920386