Table of Contents
1. Meaning
2. Why use method overriding
3. How to use method overriding
3.1 Basic Syntax
3.2 Specific analysis
3.3 Some tips on method rewriting
Home Common Problem Complete mastery of method overriding in Java

Complete mastery of method overriding in Java

Jul 29, 2022 pm 03:22 PM
java

This article brings you relevant knowledge about java. After the subclass inherits the parent class, you can write a method in the subclass with the same name and the same parameters as the parent class, so as to realize the function of the parent class. We call the overwriting of methods with the same name and parameters in a class called method rewriting. Let’s take a look at it together. I hope it will be helpful to everyone.

Complete mastery of method overriding in Java

Recommended study: "java video tutorial"

1. Meaning

After a subclass inherits a parent class , you can write a method in the subclass with the same name and the same parameters as the parent class, thereby overriding the method with the same name and the same parameters in the parent class. We call this process override of the method

2. Why use method overriding

2.1 When the method of the parent class cannot meet the needs of the subclass, the method needs to be rewritten in the subclass

2.2 Question and analysis

For example, there is a parent class Peple and a subclass Chinese. There is a say() method in the parent class that outputs people talking. However, when I want the subclass to call it, it outputs Chinese people talking. It is very difficult. Obviously calling the method directly is not possible, so you need to rewrite the say method in the subclass

2.3 Sample code

People class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

public class Peple {

    private String name;

    private String sex;

    private int age;

    private int weight;

 

    public Peple() {

    }

 

    public Peple(String name, String sex, int age, int weight) {

        this.name = name;

        this.sex = sex;

        this.age = age;

        this.weight=weight;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getSex() {

        return sex;

    }

 

    public void setSex(String sex) {

        this.sex = sex;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

    public int getWeight() {

        return weight;

    }

 

    public void setWeight(int weight) {

        this.weight = weight;

    }

    public void say(){

        System.out.println("人在说话");

    }

 

}

Copy after login

Chinese class

1

2

3

4

5

6

7

8

public class Chinese extends Peple{

    public Chinese() {

    }

    @Override

    public void say() {

        System.out.println("中国人在说话");

    }

}

Copy after login

Test03 Class

1

2

3

4

5

6

7

public class Test03 {

    public static void main(String[] args) {

        Chinese c=new Chinese();

        c.say();

        //当进行方法重写时,调用的是子类的say()方法

    }

}

Copy after login

2.4 Sample code running screenshot

3. How to use method overriding

3.1 Basic Syntax

1

2

3

4

5

6

@Override

权限修饰符 返回值类型 方法名(形参列表){

    //子类重写的方法的权限修饰符的访问权限必须大于等于父类的,但是父类不能是private

    //当父类的返回值类型为基本数据类型或者为void时,子类方法的返回值类型也应该为对应的基本数据类型或者void

   

}

Copy after login

3.2 Specific analysis

3.2.1 The access permissions of methods overridden by subclasses should be greater than or equal to the access permissions of parent class methods

a Sample code

People class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

public class Peple {

    private String name;

    private String sex;

    private int age;

    private int weight;

 

    public Peple() {

    }

 

    public Peple(String name, String sex, int age, int weight) {

        this.name = name;

        this.sex = sex;

        this.age = age;

        this.weight=weight;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getSex() {

        return sex;

    }

 

    public void setSex(String sex) {

        this.sex = sex;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

    public int getWeight() {

        return weight;

    }

 

    public void setWeight(int weight) {

        this.weight = weight;

    }

    //没有写访问权限的话,默认是default访问权限

    void say(){

        System.out.println("人在说话");

    }

 

}

Copy after login

Chinese class

1

2

3

4

5

6

7

8

9

10

public class Chinese extends Peple{

    public Chinese(){

    }

    //父类say方法的访问权限为default,子类say方法的访问权限为public,

    // 符合子类方法访问权限大于等于父类方法访问权限的要求

    @Override

    public void say() {

        System.out.println("中国人在说话");

    }

}

Copy after login

Test03 class

1

2

3

4

5

6

public class Test03 {

    public static void main(String[] args) {

        Chinese c=new Chinese();

        c.say();

    }

}

Copy after login

b Sample code running screenshot

3.2.2 When the return value type of the parent class method is a basic data type, the return value type of the method overridden by the subclass is also the corresponding basic data type

a Sample code

People class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

public class Peple {

    private String name;

    private String sex;

    private int age;

    private int weight;

 

    public Peple() {

    }

 

    public Peple(String name, String sex, int age, int weight) {

        this.name = name;

        this.sex = sex;

        this.age = age;

        this.weight=weight;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getSex() {

        return sex;

    }

 

    public void setSex(String sex) {

        this.sex = sex;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

    public int getWeight() {

        return weight;

    }

 

    public void setWeight(int weight) {

        this.weight = weight;

    }

    public double add(int a,int b){

       return a+b;

    }

 

}

Copy after login

Chinese class

1

2

3

4

5

6

7

8

public class Chinese extends Peple{

    public Chinese(){

    }

    @Override

    public double add(int a,int b) {

       return a+b+1;

    }

}

Copy after login

Test03 class

1

2

3

4

5

6

public class Test03 {

    public static void main(String[] args) {

        Chinese c=new Chinese();

        System.out.println("求和之和再加上1的结果为: "+c.add(2, 3));

    }

}

Copy after login

b Sample code running screenshot

3.2.3 When the return value type of the parent class method is void, the return value type of the method overridden by the subclass is also void

a Sample code

People class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

public class Peple {

    private String name;

    private String sex;

    private int age;

    private int weight;

 

    public Peple() {

    }

 

    public Peple(String name, String sex, int age, int weight) {

        this.name = name;

        this.sex = sex;

        this.age = age;

        this.weight=weight;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getSex() {

        return sex;

    }

 

    public void setSex(String sex) {

        this.sex = sex;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

    public int getWeight() {

        return weight;

    }

 

    public void setWeight(int weight) {

        this.weight = weight;

    }

    public void eat(){

        System.out.println("人的主食一般是熟食");

    }

 

}

Copy after login

Chinese class

1

2

3

4

5

6

7

8

public class Chinese extends Peple{

    public Chinese(){

    }

    @Override

    public void eat() {

        System.out.println("中国人的主食是大米或者小麦");

    }

}

Copy after login
Copy after login

Test03 class

1

2

3

4

5

6

public class Test03 {

    public static void main(String[] args) {

        Chinese c=new Chinese();

        c.eat();

    }

}

Copy after login
Copy after login

b Sample code running screenshot

3.2.4 When the parent When a class method is statically modified, subclasses cannot override the method

a Error example code

People class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

public class Peple {

    private String name;

    private String sex;

    private int age;

    private int weight;

 

    public Peple() {

    }

 

    public Peple(String name, String sex, int age, int weight) {

        this.name = name;

        this.sex = sex;

        this.age = age;

        this.weight=weight;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getSex() {

        return sex;

    }

 

    public void setSex(String sex) {

        this.sex = sex;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

    public int getWeight() {

        return weight;

    }

 

    public void setWeight(int weight) {

        this.weight = weight;

    }

    public static void eat(){

        System.out.println("人的主食一般是熟食");

    }

 

}

Copy after login

Chinese class

1

2

3

4

5

6

7

8

public class Chinese extends Peple{

    public Chinese(){

    }

    @Override

    public void eat() {

        System.out.println("中国人的主食是大米或者小麦");

    }

}

Copy after login
Copy after login

Test03 Class

1

2

3

4

5

6

public class Test03 {

    public static void main(String[] args) {

        Chinese c=new Chinese();

        c.eat();

    }

}

Copy after login
Copy after login

b Sample code running screenshot

Error message given by idea when compiling

Given after forced running Error message

3.3 Some tips on method rewriting

3.3.1 Copying method

steps

1. First directly copy (Ctrl C) the method in the parent class that needs to be overridden by the subclass

2. Paste (Ctrl V) into the subclass

3 .Modify the functions in the subclass to facilitate the realization of requirements that cannot be achieved by the parent class

Operation screenshot display

#3.3 .2 Compiler prompt method

Steps

1. First, write an English @ symbol

in the subclass body, not within the method. 2. Select the prompt Overide/implement methods in...

3. Double-click to pop up an override method selection list pop-up window

4. Select the corresponding method that needs to be overridden according to the prompts

5. After clicking the OK button, an overridden method of the method you selected will be generated in the subclass

6. Remove the automatically generated first line in the generated overridden method, and then add it to the method body according to your needs Write the appropriate statement

Operation screenshot display

##3.3.3 Shortcut key method

Steps

1. Move the mouse to the location where the overridden method should be generated

2. Press the Alt key and Insert key on the keyboard at the same time,

3. In the pop-up box, select Override Methods

4. After double-clicking, an interface will pop up. In the interface, select the method that needs to be overridden by the subclass

5. Click the OK button The required rewrite method will be generated later

6. Remove the automatically generated first line in the rewrite method, and then write the appropriate statement in its method body

Operation screenshot display

# Recommended learning: "

java video tutorial

The above is the detailed content of Complete mastery of method overriding in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.