Three major characteristics of Java language

  1. encapsulation
  2. inherit
  3. Polymorphism
  4. abstract class

1. Encapsulation

The concept of encapsulation: hiding certain information of a class inside the class and not allowing direct access by external programs. Instead, the operations and access to the hidden information are implemented through the methods provided by the class.
Define a Person class and make the member variables private

Test class
Make the constructor private
Test class
Benefits of encapsulation:
Hide the implementation details of the class
and can only be accessed through specified methods. It
is convenient to add control statements
and to modify the implementation.

2.Inheritance

1. Inheritance is an indispensable design idea in object-oriented programming,
the foundation for code reusability, and the main way to improve code scalability.
● Inheritance is to derive a new class from an existing class. The new class can absorb the attributes and behaviors of the existing class and
expand new capabilities.
● Use the extends keyword in JAVA to express inheritance relationships.
● JAVA does not support multiple inheritance. Single inheritance makes JAVA's inheritance relationship very simple. A class can only have one
direct parent class.
● After inheritance, the subclass can call all non-private properties and non-private methods of the parent class
● Inheritance form:
● [Access permission modifier] [Modifier] Subclass name extends Parent class name {subclass body}
2. Inherited Transitive class
C inherits from class B, and class B inherits from class A.
Then class C has all the non-private properties and non-private methods of class B and class A.
When a class does not inherit any class, jvm will let the class inherit by default. Object class
Object is the base class provided by Java for all classes
● Constructor method in inheritance
The subclass construction method will first call the parent class construction method.
Use the super keyword to call any construction method of the parent class, which must be written in the first line of the construction method.
If the subclass's constructor does not explicitly call the base class constructor, the system calls the base class's parameterless
constructor by default.
• Purpose of super keyword
• Use super keyword to access parent class members
• Use super.member variable name to refer to parent class member variables
• Use super.method name (parameter list) to access parent class method.
• Use the super.constructor method (parameter list) to access the parent class constructor.
Misunderstanding: Do not mistake super for the parent class object. When creating a subclass object,
the parent class object will not be created.
Only the information in the parent class will be loaded. Stored in the subclass object.
● Overriding of methods (OverRide)
● Application scenarios
When the method implementation of the parent class cannot meet the needs of the subclass, the method can be overridden (override)
● In the subclass, the subclass can be overridden as needed. Override methods inherited from the base class.
● Method rewriting rules
• The method name is the same and the parameter list is the same;
• The return value type is the same;
• Access permissions cannot be less than the parent class permissions;
Note: Constructors and static methods cannot be overridden, and member variables do not have to be overridden.
Define an animal class as the parent class
Design a dog class as a subclass
Design a cat class as a subclass
Test class
Test class
Design a dog class that inherits the dog class

3. Polymorphism

The same thing shows different states at different times
● Three necessary conditions for the existence of polymorphism
● There must be inheritance (including the implementation of interfaces) (prerequisite)
● There must be overriding (prerequisite)
● The parent class reference points to the subclass Object
When the compile-time type is a parent class and the run-time type is a subclass, it is called a parent class reference pointing to the subclass object.
For general members:
To put it simply: when compiling, look to the left, and when running, look to the right.
For static members:
To put it simply: look to the left when compiling and running.
Define the dog class to inherit the animal class
Define cat class to inherit animal class
Define humans, the parameters of the method are objects of the animal class, and achieve polymorphism.
Define animal class (abstract class)
Test class
Test class

abstract class

● Abstract method
• An abstract method is a special method: it only has a declaration, but no specific implementation.
• An abstract method must be modified with the abstract keyword.

● If a class does not contain enough information to describe a concrete object, such a class is an abstract
class.
● Except that abstract classes cannot instantiate objects, other functions of the class still exist, including member variables, member methods
and constructors.
● A class modified with abstract is an abstract class. If a class contains abstract methods, then the class must
be defined as an abstract class.
● Features:
• Abstract classes cannot be instantiated, but they can have constructor methods. Because abstract classes contain methods without concrete implementation,
objects cannot be created with abstract classes.
● Abstract classes can only be used as base classes, representing an inheritance relationship. A non-abstract class that inherits an abstract class must implement
all abstract methods in it, and the parameters and return values ​​of the implemented methods must be the same as those in the abstract class
. Otherwise, the class must also be declared abstract.
● Abstract classes and abstract methods are design-level concepts in the software development process. In other words,
designers will design abstract classes and abstract methods, and programmers will inherit these abstract classes and
override abstract methods to implement specific functions.

Define the animal abstract class as the parent class
Define cat class to inherit animal class
Define the dog abstract class to inherit from the animal class
Define the dog class to inherit the dog class

Guess you like

Origin blog.csdn.net/weixin_64189867/article/details/130537280