OOP programming has three main features, encapsulation, inheritance, polymorphism
Packaging
Encapsulation is the restriction of access to classes, properties, and methods using access restrictions.
If you do not use encapsulation, the following conditions occur:
Privatization of properties can only be done in one way for variables in the variable class, because the privatized properties and methods cannot be accessed.
Inherited
Inheritance requires the use of two classes, one for the subclass and one for the parent class. Subclasses can use non-private methods and properties of the parent class. The constructor method of its parent class is called when the subclass's constructor is called. The subclass default is to call the parent class's parameterless constructor method. If the parent class overrides the constructor method, the subclass uses the Super Keyword class declaration to invoke the constructor of the parent class.
When instantiating a subclass object, subclasses can automatically transform to the parent class, and the parent class cannot transform to the subclass. The method used to instantiate an object depends on the data type that is received, and if it is a parent data type, you cannot use a method of a subclass, and if it is a data type of a subclass, you can use the method of the parent class.
If the subclass overrides the method of the parent class after the inheritance, the overridden method is called when the subclass object is created. If the data type is transformed to the parent class, calling this method again will use the method overridden by the subclass, because creating a memory space creates the properties and methods of the copied subclass.
Polymorphic
Because of the inheritance and override mechanism, the same type of object, called the same method, gets the result may not be the same
Dynamic polymorphism:
Static polymorphism:
Abstract class
Abstract classes need to use the abstract keyword to declare a class, the abstract class can write specific methods, you can also write abstract methods, but abstract classes cannot be instantiated. Abstract classes require subclasses to implement, and if subclasses of an abstract class are abstract classes, the abstract methods in the parent class are not implemented. Otherwise, all of the abstract methods in the parent class need to be implemented. There are constructor methods in the abstract class, but they cannot be instantiated.
Interface
Only abstract methods and constants can be defined in an interface, only public modifications can be used, and the order of constants can be changed sequentially;
Interfaces can implement multiple inheritance relationships, but cannot implement other interfaces, and there are no constructors in the interface
Java Learning--oop