Abstract class, interface, ordinary class

I didn’t write it yesterday, I must make it up today, and punish myself again.

The difference between abstract class and ordinary class

1 There can be abstract methods in abstract classes, but not in ordinary ones.

2 Abstract classes cannot be instantiated but can be declared.

3 Both abstract and ordinary classes are inherited, but the subclass of the abstract class must override all abstract methods of the abstract class, unless the subclass is also an abstract class.

4 can define ordinary member variables

Note: Abstract style has no method body (ie {}) and cannot be modified with private, static, synchronized, native, and final

Why can't it be finalized? Because the abstract class must have subclasses, it is not possible, but you can use final to modify the constants and methods in the class

The difference between interface and abstract class 

1 Abstract classes can define ordinary member variables, and interfaces can only define static constants.

2 Abstract classes can only have ordinary methods, not interfaces

3A class can inherit an abstract class, but can implement multiple interfaces

4 Abstract classes can have initialization blocks, but not in interfaces, but both can be declared

5 Abstract classes can have constructors, but interfaces cannot. (The constructors in abstract classes are not meant to create objects, but to complete the initialization of abstract classes)

Abstract class

Before JDK1.8, the default access permissions of abstract class methods were protected

After JDK1.8, the default access permission of the abstract class method is default

interface 

Before JDK1.8, the method of the interface must be public

After JDK1.8, it can also be default static

Note: The abstract class is an incomplete class, which is equivalent to an intermediate layer between the interface and the ordinary class, which satisfies the abstraction of the interface and the specific realization

The interface provides a unified "protocol" in which all the variables are constants

Assumption: Variables can be defined in the interface, but the methods in the interface are still abstract classes, so the properties cannot be modified through methods. If someone says that it doesn’t matter that they can be modified through the implementation class, it will not reflect the characteristics of the interface providing a unified protocol. / Provide a unified abstraction, the meaning of the interface does not exist, so it is not possible. The attributes in the interface must be constants, read-only and do not change

There is no need to declare anything in the interface, identify the interface (there can be an interface without content), and there is no declaration for the interfaces under the JDK

Interface multiple inheritance, class single inheritance. But an error is reported when the interface inherits List and Map because there are many duplicate methods between them

 

Guess you like

Origin blog.csdn.net/weixin_40728070/article/details/91126359