Three features of Java object-oriented
inherit
Keyword: extends
Concept: Inheritance mechanism is an indispensable key concept in object-oriented programming. It is the foundation of software reuse and the main way to improve the scalability and maintainability of software systems. Inheritance means that the definition of a class can be based on another existing class, that is, the subclass is based on the parent class, so as to realize the reuse of the code of the parent class. The subclass can absorb the data attributes and behaviors of the existing class, and expand new capabilities.
In Java, class inheritance is a single inheritance.
-
Inheritance calls to constructors
- The subclass constructor always calls the parent constructor first
- By default, the parent class parametric constructor is invoked
- In the first line of the subclass constructor, you can call any of the parent constructors using the super keyword
The reason why ** subclass instantiates call parent constructor method is that ** Java language requires subclass to be responsible for ensuring that its inherited parent class enters a stable and complete state as soon as possible. Without this constraint, a method inheriting from a parent class may use some variables in the parent class that are not initialized, resulting in some unpredictable consequences.
-
Code rewriting:
public class Father{ public void eat(){ System.out.pringln("Dad eats!"); } } public class Sun extends Father{ public void eat(){ System.out.pringln("My son eats!"); } }
-
The role of this keyword:
- Use this keyword to refer to member variables.
- Use this keyword to refer to other construction methods within its own construction method.
- Use this keyword to represent the object of your class.
- Use this keyword to refer to member methods.
- ** Note: The ** this keyword must be placed in a non-static method.
-
this refers to member variables:
Within a class's method or constructor, member variable names can be referenced in the format "this. member variable names", often used to distinguish member variables with the same name from local variables.
public class ReferenceVariable { private int a; public ReferenceVariable(int a){ this.a = a; } public int getA(){ return a; } public void setA(int a){ this.a = a; } }
-
this refers to member variables:
In the construction method of a class, you can also use this keyword to refer to other construction methods, which can reduce the repetition of the code, but also make all the construction methods remain unified, so that it is convenient for future code modification and maintenance, and also for code reading.
public class ReferenceConstructor { int a; public ReferenceConstructor(){ this(0); } public ReferenceConstructor(int a){ this.a = a; } }
-
this refers to member methods
Within a class, member methods can also be referenced by "this. method name (parameter)" when calling each other.
public class ReferenceObject { ReferenceObject instance; public void hello(){ System.out.println("helloWorld"); } public void sayHello(){ this.hello(); } }
-
this represents its own object:
Within a class, you can also use this to represent the object of your class, or in other words, each class has an implicit member variable whose type is the type of the class and whose name is this.
public class ReferenceObject { ReferenceObject instance; public ReferenceObject(){ instance = this; } public void test(){ System.out.println(this); } }
** super keyword: ** super usually refers to parent objects
-
To call the construction method of the parent class in the subclass construction method, it should be noted that the super statement can only appear in the first line of the subclass construction method body.
class Base { Base() { System.out.println("Base"); } } public class Checket extends Base { Checket() { super(); System.out.println("Checket"); } public static void main(String argv[]) { Checket c = new Checket(); } }
When a local variable in the body of a subclass method or a member variable of a subclass has the same name as a member variable of the parent class, that is, when the local variable of the subclass overrides the member variable of the parent class, the member variable name of the parent class is used to refer to the member variable of the parent class.
-
When the member method of the subclass covers the member method of the parent class, that is, the subclass and the parent class have exactly the same method definition (method body can be different), then the method of the parent class is accessed by "super. method name (parameter list)".
encapsulation
Concept: Encapsulation is to combine the properties and operations (or services) of an object into an independent whole and hide the details of its internal implementation as much as possible. Encapsulation is an information hiding technology, which is implemented in Java by keywords private, protected and public. Encapsulation combines all the components of an object. Encapsulation defines how the program refers to the data of the object. Encapsulation actually uses methods to hide the data of the class and controls the degree to which the user modifies and accesses the data of the class. Proper encapsulation can make the code easier to understand and maintain, and also enhance the security of the code.
public class Man{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
polymorphic
Meaning: There are many concrete implementations of external manifestations.
Specifically embodies:
Overload
Method coverage
-
Polymorphic parameters
Run-time type and compilation-time type
A a = new A(); a.show();
When declaring A, type A is used to represent the type of object, which is called compile-time type.
At compile time, the virtual machine assumes that a is of type A, and the validity of the attributes and methods used for a will be verified in class A.
A in construction method A() is the runtime type.
At runtime, methods in runtime types are executed
A parent reference points to a child class object
class Animal{ ...... } class Cat extends Animal{ ...... } Animal x = new Cat()
Calling member methods in polymorphic environments
class Animal{ void show() { System.out.println("Anmial"); } } class Cat extends Animal{ void show() { System.out.println("cat"); } } Animal x = new Cat() x.show()//Calling methods in subclasses
Calling static methods in polymorphic environments
class Animal{ static void show() { System.out.println("Animal"); } } class Cat extends Animal { static void show() { System.out.println("Cat"); } } Animal x = new Cat() x.show() //The static member method in the animal class is called.
Calling member variables in polymorphic environment
class Animal{ int num = 3; } class Cat extends Animal { int num = 4; } Animal x = new Cat() x.num; //It calls member variables in an animal class.
Object Modeling in Polymorphic Environment
When there is a polymorphic environment, type conversion is sometimes required. Here we will deal with a concept called
Mandatory type conversion, but remember that mandatory is a prerequisite and can only be mandatory to the runtime type of this objectAnimal animal=new Cat(); Cat cat=(Cat)animal;
instanceof operator
There are two types of objects in polymorphic environments, in which the runtime type is often determined at runtime.
Sometimes different processing is needed depending on the runtime type. At this time, the instance of operator is used to determine the runtime type of an object.