Three Elements of Classes in Java

1. Inheritance

Each class can only inherit one class, and object defaults to the parent class of all classes

After the subclass inherits the parent class, when instantiating the subclass, the system will first instantiate the parent class object and execute the parent class constructor
keywords first:
1. extends: Indicates inheritance, the usage is <subclass> extends<parent class>
2.super: Indicates the parent class of the current class, use super() to call the constructor of the parent class
3.this: Indicates the object of the current class, you can also use this() to call the constructor of the current class
4.fanil: Be Variables modified by fanil are constants, methods modified by fanil cannot be rewritten, and classes modified by fanil cannot be
inherited 4. Loading the subclass ——> loading the transformation object on the parent class: instantiating the subclass as the parent class, losing the new methods and variables added by the subclass, and can manipulate the objects and methods of the parent class, if The method is rewritten, and the method rewritten by the subclass is called. The upper transformation object can be cast into the rewrite and hide of the subclass method again: hidden means that the parent class method is overwritten by the subclass. Method rewriting rules : 1. The number of parameters and corresponding types must be the same 2. The return value must not be modified 3. The access level must not be lowered











Inherited Features
The subclass has the non-private properties and methods of the parent class.
Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.
Subclasses can implement the methods of the parent class in their own way.
Java inheritance is single inheritance, but multiple inheritance is possible. Single inheritance means that a subclass can only inherit one parent class. Multiple inheritance means, for example, class B inherits class A, and class C inherits class B, so according to the relationship, class B is C The parent class of the class, class A is the parent class of class B, which is a feature that distinguishes Java inheritance from C++ inheritance.
Improve the coupling between classes (the disadvantage of inheritance, the higher the coupling, the closer the connection between the codes and the worse the independence of the codes).

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDU2NDI0Nw==,size_16,color_FFFFFF,t_70

Two, polymorphism

Theoretical basis : parent class references can point to subclass objects

For example: the method parameter definition is the parent class, and the actual input is the subclass object

For example: the return type definition is the parent class, and the subclass object is actually returned

When there are multiple derived classes in a parent class, and each derived class rewrites a method in the parent class, then the method of the parent class will behave differently in different subclasses, which is polymorphic.

Keyword abstract : The method modified by abstract is called an abstract method. Non-abstract class subclasses must implement all abstract methods of the abstract parent class. If there are abstract methods, the class must be an abstract class. Abstract classes can have non-abstract methods, abstract classes Cannot be instantiated, abstract only cares whether this method exists, not whether this method is implemented.
Interface (interface) : All methods in the interface are abstract methods, and the keyword implements. <subclass>
interface and parent class : interface refers to a method, and parent class refers to a category. A subclass can have only one superclass but multiple interfaces.

premise: 

  • There must be an inheritance relationship.
  • The subclass overrides the method of the parent class.
  • Parent class references point to subclasses.

Polymorphism has the following characteristics:

  •  Parent class references to subclasses can only access methods and properties owned by the parent class.
  •  For methods that exist in the subclass but not in the parent class, this reference cannot be used.
  •  If the subclass overrides some methods in the parent class, these methods defined in the subclass must be used when calling these methods.

3. Packaging 

Hide members that cannot be exposed, and use public methods to expose access to the hidden members

Objects must have a clear boundary; the division of boundaries (objects perform their duties, the granularity of objects, and the reusability of objects). Enclosing objects and processes actually hides objects, and users can only access them through defined interfaces . Proper encapsulation can make programs easier to understand and safer.
Encapsulated access level:
public: accessible by the current class, other classes in the same package, subclasses in different packages, and non-subclasses in different packages protected
: accessible by the current class, other classes in the same package, and different packages Subclass access in
Friendly (that is, nothing): can be accessed by the current class and other classes in the same package
private: can only be accessed by the internals of the current class

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_30436011/article/details/129381545