Three features of Java

Object oriented is the core idea of java. It has three characteristics: encapsulation, inheritance and polymorphism.

1 package

1.1 concept

Object-oriented encapsulation can combine the attributes and methods of the object to provide unified external access. Encapsulation is to separate the user and designer of the object. The designer can design the content that can be operated externally and that can only be operated internally. Users can only use the designed content, but they can't see how the designer realizes it. Just as a computer is composed of cpu, memory and various peripherals, for computer users, they can only use these components, but they can't see the internal structure of these components.

1.2 benefits

In programming, classes encapsulate data and methods and protect the operation of data and methods.

1.3 permission modifier

(1) When developers encapsulate, they need to use modifiers to control access to properties and methods
Public: refers to public attributes and methods modified with public. Any other class can access them. This is the most open permission. Private: refers to private attributes and methods decorated with private. They can only be used in the current class, and there is no way to use them in other classes. Protected: refers to protected attributes and methods modified with protected. They can be used in all classes in this package and in subclasses other than this package.
Default: there is no modifier in front of the method and attribute. The attribute and method of the default modifier can be accessed by other classes in the same package, and the classes, including subclasses, in other packages are not allowed to be accessed.

public class Person { 
	//Define properties 
	private String name; 
	private boolean sex; 
	private int age; 
	private String hobby; 
	//Define common methods 
	public void display(){ 
	//Define a private method 
	private String getSex(){ 
		return sex?"female":"male"; 
	//Define a protected method 
	protected void study(){
	//Define a default method 
	void filter(){

(2) Modifier permission table

(3) Attention

  • In general, attributes are usually decorated with private, so that private modified attributes can be accessed externally. Usually, a set of get/set methods is defined for a property, which is called the accessor of the property.
  • If an attribute has only a get method but no set method, it is called a read-only attribute; If the attribute has only set method and no get method, it is called write only attribute;
  • The accessor must be public.

2 succession

The inherited class is called the parent class (base class, superclass). An inherited class is called a subclass.

2.1 characteristics of inheritance

Subclasses inherit the characteristics and behavior of the parent class, so that subclasses have various properties and methods of the parent class. In the inheritance relationship, the parent class is more general and the child class is more specific.
(1) The subclass has all the properties and methods of the parent class (except construction methods, which can only be called and cannot be inherited);
(2) Subclasses can also have their own unique properties and methods;
(3) Subclasses can override methods already owned in the parent class (normal methods).

2.2 advantages of inheritance

Inheritance can effectively realize code reuse and avoid repeated code( When two classes have the same characteristics and behaviors, the same parts can be extracted and put into one class as the parent class, and the other two classes inherit the parent class).

2.3 syntax of inheritance

[modifier] class name extends * * parent class name{
//Structure of class

In java, the extends keyword is used to indicate that one class inherits another class. Define some general methods and properties in the parent class, and the child class automatically inherits the properties and methods of the parent class (except private ones).
Tax note
(1) Constructors can be called by subclasses, but they cannot be inherited by subclasses.
(2) In java, a class can only have one direct parent (single inheritance), but it can indirectly inherit multiple parents.
(3) A parent class can have many subclasses.

3 polymorphism

3.1 understanding polymorphism

In object-oriented programming, it is to assign the object of the subclass to the parent class. When calling the method with the reference of the parent class, the object of the subclass specifically executes the method. There are two forms of polymorphism in Java: one is inheritance (multiple classes rewrite the same method), and the other is to override the same method in the connection by implementing the connection.

3.2 advantages of polymorphism

(1) Polymorphism can enhance the scalability and maintainability of programs.
(2) Make the code more concise and the structure clearer.

3.3 use of polymorphism

Polymorphic syntax:

Parent class name parent class object name = new child class name ([parameter list]);

Use of polymorphism:
(1) General practice: write a method that only accepts the parent class as a parameter. The written code only deals with the parent class. When calling this method, instantiate objects of different subclasses (new object).
(2) Specific method: the subclass overrides the method of the parent class. The parent class type is used as the type of the parameter, and the parent class and child class objects are passed in as parameters. When running, the method is dynamically determined according to the actual object type created.

For example, now we need to calculate the area of multiple images, including circles, rectangles, etc., but the calculation method of each figure is different, and we can use inheritance to realize it.

(1) Define a Shape class

public class Shape { 
/*** Method for calculating the area of a drawing */ 
	public double area(){ 
		System.out.println("Methods in the parent class"); 
		return 0; 

(2) Define two graphic classes, Cycle and Square, inherit the Shape class, and override the area () method in the Shape class

/*** circular */ 
public class Cycle extends Sharp{ 
	private double r;//radius 
	public static final double PI=3.14;//π 
	public double getR() { 
		return r; 
	public void setR(double r) { 
		this.r = r; 
	public Cycle(double r) { 
	/*** Override the method of calculating area in the parent class */ 
	public double area(){ 
		double area=PI*r*r; 
		return area; 
public class Square extends Sharp{ 
	private double chang; 
	private double kuan; 
	public double getChang() { 
		return chang; 
	public void setChang(double chang) { 
		this.chang = chang; 
	public double getKuan() {
		return kuan; 
	public void setKuan(double kuan) { 
		this.kuan = kuan; 
	public Square(double chang, double kuan) { 
		this.chang = chang; 
		this.kuan = kuan; 
	//Override the method of calculating area in the parent class 
	@Override public double area() { 
		double area=chang*kuan; 
		return area; 

(3) Writing test classes

public class Test { 
/*Polymorphism: assign the reference (memory address) of the subclass to the parent object. When using the parent object to call the method, it actually calls the method of the parent method rewritten or inherited by the subclass. */ 
//Calculate the area of the drawing 
public static void main(String[] args) { 
	//Create the object of the subclass and assign the object of the subclass to the parent class (polymorphic) 
	Sharp sharp=new Cycle(5); 
	double area=sharp.area();
	Sharp sharp2=new Square(4, 5); 
	double area1=sharp2.area(); 

4 rewriting and overloading of methods

4.1 method rewriting

During the operation period, the properties in the parent class can be redefined in the subclass, and the functions different from those of the parent class can be obtained by rewriting the methods in the parent class.

Rewrite condition

  • Method name must be the same;
  • The modifier range of access rights cannot be narrowed when rewriting, but can only be equal or expanded;
  • The return types of methods must be consistent;
  • The parameter list of the method must be the same, including quantity and data type. The parameter name is not required;
  • Methods decorated with final cannot be overridden.

For example: Method in parent class

public void eat(String nameq){ 
    System.out.println(name+"I'm eating"); 

Overridden methods in subclasses

//Method override 
@Override //Used to check whether the method is rewritten correctly 
public void eat(String name){ 
   System.out.println(getName()+"Eating mice"); 

4.2 overloading of methods

Concept: in the same class, a method name can define many methods as long as the parameters are different. This is called method overloading.
Benefits: enhance the flexibility of the method; It is conducive to the convenience of method invocation.

Overload conditions:

  • The method name is the same;
  • The parameters of methods are different, including: (different quantity types);
  • In the same number of parameters, different parameter types have different orders in the method parameter list.
  • Note that method overloading has nothing to do with the return value type of the method and the modifier of the method, nor with the parameter name in the parameter list, that is, the return value of the method and the access modifier can be different.
/*** Method overload */ 
public class Sum { 
	//Calculates the sum of two integers 
	public int add(int a,int b){
		return a+b; 
	public double add(double a,double b){ 
		return a+b; 
	public long add(long a,long b){ 
		return a+b; 

Tags: Java

Posted on Fri, 03 Sep 2021 23:32:31 -0400 by tomsace