I. Encapsulation
Function: Implements code reuse to shield some details.
For example, in the following example, I encapsulate a method public void pname () {} in the class Person. In this case, you do not need to use System again in the main method. out. println (); this statement avoids repeated use of code, and the program looks concise.
public class test{ public static void main(String agrs[]){ Person p1 = new Person(); p1.name = "jack1"; Person p2 = new Person(); p2.name = "jack2"; Person p3 = new Person(); p3.name = "jack3"; Person p4 = new Person(); p4.name = "jack4"; p1.pname(p1); p2.pname(p2); p3.pname(p3); p4.pname(p4); }}class Person{ String name; public void pname(Person person){ System.out.println(person.name); }}
Ii. Inheritance
Concept: it refers to the relationship between objects.
Role: code reuse and Function Extension
In JAVA, the extends keyword is used to represent the inheritance of two classes. When the subclass inherits a parent class (base class, superclass), The subclass automatically owns all the members of the parent class.
1. Method rewriting (voerread)
Is occurring in the inheritance relationship. The methods that are overridden in the parent class and the methods that are rewritten in the subclass must have the same method name, the same return value type, and the same parameter list, the method to be rewritten in the subclass cannot have more strict access modifiers than the parent class.
The following is a simple example:
public class test{ public static void main(String[] args){ Animel animel = new Animel(); animel.name = "mouse"; animel.age = "12"; animel.named(); Cat cat = new Cat(); cat.name = "cat"; cat.age = "13"; cat.sex = "boy"; cat.named(); } } class Animal{ String name; String age; public void named(){ System.out.println("name is:" + name + "it is age is :" + age); } } class Cat extends Animal{ String sex; public void named(){ System.out.println("name is:" + name + "it is age is :" + age + "sex is " + sex); } }
2. Method overload)
In the same class, you must have the same method name and different return value types, but you must have a list of parameters that are not supported.
Take the following simple example as an example:
public class test{ public static void main(String args[]){ Numbsum bsum = new Numbsum(); bsum.sum(20); bsum.sum(20,30); bsum.sum(20,30,40); } } class Numbsum{ public void sum(int a){ System.out.println(a); } public void sum(int a,int b){ System.out.println(a+b); } public void sum(int a,int b,int c){ System.out.println(a+b+c); } }
3. Super keyword
It is equivalent to the reference of the current parent class object. each subclass object has a parent class object, because the subclass automatically owns all the members of the parent class. JAVA builds the subclass object, you must first construct the parent class object and then create a subclass object.
The following frame chart visualizes this abstract concept:
Figure 1
Note: 1) when the member variables of the subclass and the member variables of the parent class have the same names, you must use the super. member variable name to access the member variables of the parent class.
2) during the rewriting process, the method to be rewritten by the subclass needs to use the super. method name when calling the method whose parent class is overwritten.
3) if the parent class does not have a constructor without parameters, you cannot create a subclass object. You must manually add the constructor super to the subclass constructor. ()
The following simple program code illustrates its principle:
Public class test {public static void main (String [] args) {Student person = new Student () ;}} class Person {public Person (int num) {}} class Student extends Person {public Student () {super (100); System. out. println ("successfully created subclass object ");}}
Note: the detailed process of creating objects by subclass in inheritance
1) create a parent class object before creating a subclass object
2) JAVA uses constructor to create objects.
3) JAVA needs to call the constructor of the parent class before calling the constructor of the subclass.
4) JAVA calls the constructor without parameters in the parent class by default.
5) Add a parameter-based display constructor to the parent class, so the implicit non-parameter constructor does not exist.
Iii. Polymorphism
Determines which method to bind based on input parameters during program execution.
Condition: 1. inherit 2. Override method 3. Reference of parent class pointing to subclass object