Java object-oriented method overloading, rewriting and keywords this, super

preface

  continue to learn Java object-oriented today, and understand what is method overloading, what is method rewriting, and what is the difference? Know the keywords this and super and understand their usage.
  I hope it can be helpful to your study!

Tip: the following is the main content of this article, and the Java series learning will be continuously updated

1, Method overloading

(1) Basic concepts

   in the same class, overloads are formed between methods with the same method name and different parameter lists;

(2) Manifestations of overloading

  method overloading is mainly reflected in different number of parameters, different types of parameters, different order of parameters and the same method name.
    the parameter variable name has nothing to do with the return value type, but it is recommended that the return value type should be the same.

(3) Meaning
  the significance of method overloading is that the caller only needs to remember a method name to call different versions, so as to realize the processing of multiple types of data.

(4) Case

public class Overload {

	//The number of parameters reflected by method overloading is different
	void show() {
		System.out.println("This is an output without parameters");
	}
	void show(int a) {
		System.out.println(a + "Is an integer");
	}
	void show(int a,String c) {
		System.out.println(a + "Is an integer," + c + "Is a string");
	}
	
	//Method overloading reflects different parameter types
	void show(double a) {
		System.out.println(a + "Is a real number");
	}
	void show(String c) {
		System.out.println(c + "Is a string");
	}

	//main method test
	public static void main(String[] args) {
		Overload y = new Overload();
		y.show();
		y.show(12);
		y.show(25.3);
		y.show("abcde");
		y.show(5, "xyz");
	}
}

Go back to the directory

2, Method rewrite

(1) Basic concepts

  when the methods inherited from the parent class are insufficient to meet the needs of the subclass, the inherited methods need to be modified in the subclass, which is called method rewriting / overwriting.

(2) Rewriting principle

  1. The same method name, the same parameter list, the same return value type or return subclass (at least upward transformation).
  2. Access modifier > = parent permission.
  3. Larger exceptions cannot be thrown.

   in the method overridden by the subclass, the "original" method of the parent class can be called through the super keyword.
   static methods (Methods with static modification) in the parent class do not belong to the object level, but the class level, so they cannot be inherited or overridden by subclasses.

(3) Case

//Parent class
public class Animal {
	public void eat(){
		System.out.println("Small animals are eating food~");
	}
}

//Subclass
class Dog extends Animal {
	@Override
	public void eat(){
		System.out.println("The repair dog is eating dog food~");
	}
}

Go back to the directory

3, Keyword this

(1) Key tips

  the keyword this indicates the reference of the current object.
   for construction method, this (construction method parameter); Represents the object currently being constructed;
  for member methods, this show(); Method representing the object currently being called;
  for member variables, this name; A variable representing the object currently being called;
   when decorating an attribute, if the attribute cannot be found in the current class, it can be found in the parent class.

(2) Principle

   when different objects call the same method, this is used inside the method body for recording, so the objects represented by this are different. When accessing member variables, it is resolved to this by default Member variable name, where this It is equivalent to "my" in Chinese, so the use of different classes will have different results.

(3) Use occasion

   a. when the name of the formal parameter variable and the name of the member variable are the same, the variable will be used preferentially in the method body. At this time, you need to use this The method explicitly uses member variables rather than formal parameter variables.
   b. the parameterized construction in this class can be called by using this (argument) in the parameterless construction method; Using this() in a parameterized construct can also call a nonparametric construct in this class; However, they cannot call each other at the same time, otherwise an endless loop will be formed.

(3) Case

public class Boy {

	private String name;
	private int age;
	
	//Nonparametric structure
	Boy(){
		//This calls the parameterized construct in this class to initialize member variables
		this("Zhang San", 19);
	}
	//Parametric structure
	Boy(String name, int age){
		// this();  Call the parameterless construction of this class, which is not applicable at this time

		//this.name refers to the member variable in the Boy class
		this.name = name;
		this.age = age;
	}
	
	//Private method: information to string
	private String toString(){
		String ret = "full name:" + this.name + "Age:" + this.age;
		return ret;
	}

	//Print information
	public void show() {
		//this. The member method name calls the method in this class
		System.out.println(this.toString());
	}
}

Go back to the directory

4, Keyword super

(1) Use occasion

   the keyword super represents a reference to a parent object.
  use super Method can access the member variables and member methods of the parent class.
   the first line in the constructor using super() means to call the constructor of the parent class.

(2) Case

public class Student extends Person {
	private int id;

	public Student() {
		super();//It means to call the parameterless construction method in the parent class, and the compiler will automatically add the code, so it can be omitted
	}

	public Student(String name,int age,String nationality,int id) {
		super(name,age,nationality);//Call the parameterized constructor method in the parent class
		setId(id);
	}

	//Override the show() method in the parent class
	@Override
	public void show() {
		super.show();//Call the show() method in the parent class
		System.out.println("Student ID:" + id);
	}

	public int getId(){
		return this.id;
	}
	public void setId(int id){
		this.id = id;
	}
}

Go back to the directory

summary

Tip: here is a summary of the article:
The above is today's learning content. This paper continues the object-oriented learning of Java, learning method overloading and method rewriting, as well as the usage of two keywords. The learning content will be updated continuously!!!

Keywords: Java Back-end OOP

Added by plus2net on Tue, 11 Jan 2022 06:20:47 +0200