Three characteristics of object oriented
encapsulation
Necessity of encapsulation:
Outside the object, assigning values to the attributes of the object may result in illegal data entry.
As far as the current technology is concerned, there is no way to control the assignment of attributes.
For example:
stu.age=10000;
encapsulation
Encapsulation concept: hide the internal implementation details of the object as much as possible, and control the modification and access permissions of the object.
Access modifier: private (the attribute can be modified as private, which is only visible to this class)
Private properties are not accessible outside the class
class Student{ String name; private int age; String sex; } public class Test{ public static void main(String[] args){ Student stu=new Student(); stu.age=10000;// Compilation error, private properties are not accessible outside the class } }
Provide public access methods to ensure normal data entry
In the form of access method, and then complete the assignment and value operation
Assignment: setXXX() uses method parameters to achieve assignment
Value: getXXX() uses the return value of the method to obtain the value
class Student{ String name; private int age; String sex; public void setAge(int age){ this.age=age; } public void getAge(){ return this.age; } }
Add logical judgment inside the public method, and then filter out illegal data to ensure data security.
class Student{ String name; private int age; String sex; public void setAge(int age){ if(age>0&&age<=150){//Specify a valid range this.age=age; }else{ this.age=20;//Default value when illegal data is entered } } public void getAge(){ return this.age; } }
Private property, which cannot be used outside the class, and can be accessed through the getset method
The get/set method is the only channel for the outside world to access the private properties of the object. The data can be detected and filtered inside the method
inherit
Inheritance in program is a gift or acquisition of characteristics and behaviors between classes.
The inheritance relationship between two classes must meet the relationship of "is a". For example:
Animal: parent Dog, Cat: child
Dog is an Animal
Cat is an Animal
Selection of parent class
Multiple specific classes can be used according to the needs of the program to extract commonness, and then define the parent class
The finer the function, the more coincident points, and the closer to the parent class.
The coarser the function, the fewer coincidence points, and the closer it is to the Object class. (all things are objects)
inherit
Syntax: class subclass extends parent class {} / / when defining a subclass, the inherited parent class is displayed
Application: after the inheritance relationship is generated, the subclass can use the properties and methods in the parent class or define the properties and methods unique to the subclass.
Benefits: it can not only improve the reusability of the code, but also improve the scalability of the code.
Features: java is single inheritance. A class can only have one direct parent class, but it can inherit at multiple levels, and attributes and methods can be superimposed level by level.
Non inheritable
- Construction method: the construction method of a class. It is only responsible for creating objects of this class and cannot be inherited.
- private modified attributes and methods: a kind of access modifier, which is only visible to this class.
- When the parent and child classes are not in the same package, the attribute and method modified by default: a kind of access modifier, which is only visible in the same package.
Access modifier
This category | Same package | Non identical steamed stuffed bun subclass | other | |
---|---|---|---|---|
private | √ | × | × | × |
default | √ | √ | × | × |
protected | √ | √ | √ | × |
public | √ | √ | √ | √ |
Do not write modifier default
Method coverage
When the method provided by the parent class cannot meet the needs of the child class, you can define the same method as the parent class in the child class to override
Method coverage principle:
- The method name, parameter list and return value type must be the same as the parent class.
- The access modifier cannot be more restrictive than the parent class.
Method overlay execution: when the subclass overrides the parent class method, the method after the subclass coverage is preceded by calling.
super
In the subclass, you can directly access the properties and methods inherited from the parent class. However, if the properties or methods of the parent and child classes have duplicate names (property masking and method overwriting), they need to be distinguished before special access.
super keyword to access the methods of the parent class in a subclass.
Use the form of "super." to access the methods of the parent class, and then complete the reuse in the child class; Then add additional function codes to form new functions.
Use "super. Property name" to access parent class properties
Code example:
class A{ int value=10; public void method(){ System.out.println("Parent class method body..."); } } class B extends A{ int value=20; public void method(){ super.method();//Parent class method body //You can add subclass additional function codes, such as: System.out.println("Subclass extra code..."); } public void print(){ int value=30; System.out.println(value);//Instance variable System.out.println(this.value);//Instance variables of this class System.out.println(super.value);//Parent instance variable } } public class Test{ public static void main(String[] args){ B b=new B(); b.method(); b.print(); } }
this: the reference of the current object
super: reference of parent class object
Object creation in inheritance
In the creation of an object with inheritance relationship, building a subclass object will first build a parent object.
The common content of the parent class and the unique content of the child class are superimposed to form a complete child class object.
In the construction process, the construction method of the parent class is executed first, and then the construction method of the child class is executed.
super calls the parent class constructor
super(): it means calling the parameterless constructor of the parent class. If no writing is displayed, it implicitly exists in the first line of the subclass constructor.
Super (argument): it means to call the parameterized constructor of the parent class.
The difference between this and super
This refers to the current object reference and calls the properties, methods and constructor of this class (including inheritance).
super refers to the parent class object reference and calls the properties, methods and construction methods of the parent class.
When this or super is used in the construction method, it is required to be in the first line. When this() or this (argument) is used in subclass construction, that is, super() or super (argument) cannot be written at the same time, the constructor pointed to by this() will complete the call of super().
class A{ public A(){ System.out.println("A-Nonparametric structure"); } public A(int value){ System.out.println("A-Parametric structure"); } } class B extends A{ public B(){ super();//Call parent class parameterless construction System.out.println("B-Nonparametric structure"); } public B(int value){ this();//Call the parameterless construction of this class System.out.println("B-Parametric structure"); } }
public class Test{ public static void main(String[] args){ new B(10); } }
Operation results:
A-nonparametric structure
B-nonparametric structure
B-parametric structure
polymorphic
Concept: parent class references point to subclass objects, resulting in a variety of forms.
Animal a=new Dog();
When they have a direct or indirect inheritance relationship, the parent class reference can point to the child class object.
The parent class reference can only call the properties and methods declared by the parent class, not the properties and methods unique to the child class.
Method overrides in polymorphism:
If the method in the parent class is overridden in the subclass, will the method in the parent class or the subclass take precedence when calling this method with a parent type reference?
If the subclass overrides the method in the parent class, the overridden method in the subclass is executed; otherwise, the method in the parent class is executed.
Polymorphic applications:
- Use the parent class as the method parameter to realize polymorphism, which makes the type of method parameter broader.
- Use the parent class as the method return value to realize polymorphism, so that the method can return different subclass objects.
Upward transformation (packing)
The real subclass object is saved in the parent class reference, which becomes the upward transformation (that is, the core concept of polymorphism).
Note: properties and methods declared in the parent class can be called.
class Animal(){ public void eat(){ System.out.println("Animals are eating..."); } } class Dog extends Animal{ public void eat(){ System.out.println("The dog is eating bones..."); } } public class Test{ public static void main(String[] args){ Animal a = new Dog(); } }
Downward transformation (unpacking)
The premise of downward transformation is upward transformation
The real subclass object in the parent class reference is strongly converted back to the type of the subclass itself, which becomes a downward transformation.
Note: only by converting back to the real type of the subclass can the properties and methods unique to the subclass be called.
class Animal(){ public void eat(){ System.out.println("Animals are eating..."); } } class Dog extends Animal{ public void eat(){ System.out.println("The dog is eating bones..."); } } public class Test{ public static void main(String[] args){ Animal a = new Dog(); Dog dog=(Dog)a; } }
Type conversion exception
During downward transformation, if the subclass object type in the parent class reference does not match the target type, a type conversion exception java.lang.ClassCastException will occur
class Animal(){ public void eat(){ System.out.println("Animals are eating..."); } } class Dog extends Animal{ public void eat(){ System.out.println("The dog is eating bones..."); } } class Cat extends Animal{ public void eat(){ System.out.println("The cat is eating fish..."); } } public class Test{ public static void main(String[] args){ Animal a = new Cat(); Dog dog=(Dog)a; } }
instanceof keyword
Before downward transformation, you should judge the real type of the object in the reference to ensure the correctness of type transformation.
Syntax: reference instanceof type / / a boolean result is returned
public class Test{ public static void main(String[] args){ Animal a = new Cat(); if(a instanceof Dog){ Dog dog=(Dog)a; dog.eat(); }else if(a instanceof Cat){ Cat cat=(Cat)a; cat.eat(); } } }
summary
Encapsulation: private property, get/set
Inheritance: subclass parent class extends
Polymorphism: an object whose parent class references a new subclass