This article has participated in the weekend study program, click to see more details.
Classes, abstract classes, and interfaces
1.1 Class definition
A class describes a collection of objects with the same properties (data elements) and behavior (functionality). It is an abstract collection of concepts, representing a common product. For example, all floating-point numbers have the same properties and behavior. Class and instance object of class are the most basic unit of whole object orientation.
1.2 the abstract class
1.2.1 Abstract methods
To talk about abstract classes, you need to understand what abstract methods are. An abstract method is a special kind of method: it has only method declarations and no concrete implementation. Abstract methods are decorated with the keyword abstract and declared as abstract void fun(). Abstract methods have access to either public or protected, which is public by default.
1.2.1 abstract class
Classes that contain abstract methods must all be declared abstract using the keyword abstract. An abstract class is also a Java class that can also have member variables and ordinary member methods.
Common ground between ordinary and abstract classes
- Both ordinary and abstract classes can be used for inheritance, and both follow the single inheritance specification.
- Each can have its own member variables, constructors, and ordinary member methods
Differences between ordinary and abstract classes
- Abstract classes have constructors but cannot have instance objects
- A class inherits from an abstract class, and the subclass must implement the abstract methods of the parent class. A subclass must also be declared abstract if it does not implement the parent class’s abstract methods
- Abstract classes exist for inheritance. Defining an abstract class but not inheriting it makes no sense to create an abstract class
1.3 interface
An interface is a further abstraction than an abstract class. Interface can be said to be a completely abstract class, interface methods do not provide concrete implementation, are abstract methods. Interfaces can also contain member variables, all of which are implicitly specified as public static final.
Interface features
- Interface methods are abstract methods and default to **public static **
- The default member variables of the interface are public static final
- The interface has no constructor
- A class follows a specific interface, uses the Implement keyword, and a class can implement more than one interface
1.4 Differences between Abstract classes and interfaces
1.4.1 Grammatical differences
- Abstract classes have constructors; interfaces do not
- Interfaces cannot have static code blocks and static methods, whereas abstract classes can have static code blocks and static methods.
- A class can inherit only one abstract class, while a class can implement multiple interfaces.
1.4.2 Differences in design ideas
An abstract class is an abstraction of a thing, that is, a class abstraction, whereas an interface is an abstraction of behavior. An abstract class abstracts the class as a whole, including properties and behaviors, whereas an interface abstracts parts of the class (behavior). Inheritance is a “no” relationship, whereas interface implementation is a “no” relationship. If a class inherits an abstract class, the subclass must be the class of the abstract class, and the interface implementation is whether or not it has the relationship, such as whether birds can fly (or whether they have the characteristics of flight). If birds can fly, the interface can be implemented, and if they cannot fly, the interface will not be implemented.
The design level is different, abstract class as the parent of many subclasses, it is a kind of template design. Interface is a code of conduct, it is a radiation design. For abstract classes, if you need to add new methods, you can directly add concrete implementation in the abstract class, the subclass can not change; This is not the case with interfaces. If an interface changes, all classes that implement that interface must change accordingly.