Basic object-oriented features
Packaging
encapsulation is to hide the inner details of the object as much as possible, to form a boundary, and to keep only the limited interfaces and methods to interact with the outside world. The principle of encapsulation is to prevent parts of the object from being arbitrarily accessed and manipulated by the internal properties of the object, thus avoiding the external destruction of the object's internal properties. You can implement information hiding for members of a class by setting certain access permissions on the members of the class.
Private: A member of a class that is limited to private and can only be accessed by the class itself. If the constructor method of a class is declared private, other classes cannot generate an instance of the class.
Default: A member of a class that does not have any access permission is the default (default) Access state and can be accessed by the class itself and the classes in the same package.
Protected: A member of a class qualified as protected, which can be accessed by the class itself, its subclasses (including the same package and subclasses in different packages), and all other classes in the same package.
Public: A member of a class that is qualified as public and can be accessed by all classes.
Inherited
The object of the subclass has all the properties and methods of the parent class, called the child class inheritance of the parent class.
In Java, a parent class can have more than one subclass, but a subclass may inherit only a single parent class, called one-inheritance.
Inheritance implements the reuse of code.
All classes in Java are obtained by inheriting the Java.lang.Object class directly or indirectly.
Subclasses cannot inherit member variables and methods in the parent class that have access rights private.
Subclasses can override the parent class's method, which is to name a member variable with the same name as the parent class.
Java uses Super to access the members of the parent class, and super is used to refer to the parent class of the current object. There are three ways to use super:
Accesses a member variable that is hidden by the parent class, such as: super.variable;
Call the overridden method in the parent class, such as: Super. Method ([Paramlist]), super () calls the parent class construction methods;
Call the constructor of the parent class, for example: Super ([paramlist]);
Polymorphic
An object's polymorphism refers to a property or method defined in the parent class that, after the quilt class inherits, can have different data types or behave differently. This makes the same property or method have different semantics in the parent class and its subclasses. For example, the drawing method for geometry, ellipse and polygon are subclasses of geometry, and the drawing method functions differently.
The polymorphism of Java is embodied in two aspects: the static polymorphism implemented by the method overloads (compile-time polymorphism) and the dynamic polymorphism (run-time polymorphism) implemented by the method overrides.
Compile-time polymorphism: In the compile phase, specifically called which overloaded method, the compiler will statically determine the invocation of the corresponding method according to the different parameters.
Run-time polymorphism: Subclass objects can be used as parent objects because they inherit all of the properties of the parent class (except private). In a program where the parent object is used, the subclass object can be used instead. An object can invoke a method of a subclass by referencing an instance of the child class.
Java Object-oriented (iii) theory