Interfaces and Abstract Classes

Abstraction and interface
1. Both abstraction and interface can be declared as objects, but cannot be instantiated. An abstract class is a collection that extracts the same behavior from multiple different classes. Since there are multiple collections of different class methods, there is no real object. Really such an object. 2. An interface is a special abstract class (which is extended due to the insufficiency of abstract classes) 3. A subclass of an abstract class can be an abstract class or its implementation class 4. An abstract class can have methods that are not abstract , can have a constructor, but cannot be instantiated by new.    The abstract class can actually be instantiated, but its instantiation method is not to create an object through the new method,    but to indirectly realize the instantiation of the parent class by pointing to the instance of the child class through the reference of the parent class    (because the child class needs to Before instantiation, its parent class must be instantiated first. In this way, an object that inherits the subclass of the abstract class is created, and its parent class (abstract class) is instantiated).    However: the interface cannot be instantiated Yes (the interface has no constructor at all) Before jdk1.8 interface 1. Multiple implementations 2. The variable type defaults and can only be public static final 3. The function type can only be public by default and can only have static members of public type Functions and methods are public abstract 4. Non-static member functions do not have method bodies, and static member functions have method bodies 5. Subclasses must implement all interface functions 6. There can be a main method; you can create a new interface, which needs to be implemented in the method body All interface functions 7. No constructor Abstract class 1. Single inheritance



















 


2. Unlimited variable types (static variable + non-static variable)
3. Unlimited function type (static function + non-static function)
4. Non-static functions include abstract functions without method bodies. Ordinary functions with method bodies
5. Sub The class can not override the abstract method of the parent class, but the subclass must also declare it as an abstract class; the subclass can choose to override the non-abstract method of the parent class 6.
It can have a main method;
The constructor Jdk1.8 interface can have a method of default type, and the implementing class can choose to implement the method . Meaning : The main advantage of the default method is to provide a way to extend the interface without breaking the existing code. Another advantage is that this method is optional, and subclasses can override or default implementations according to different needs.
 



Guess you like

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