[Java] Overload / Override / Inheritance

Make a note of what you learned about Java overloading, overriding, and inheritance.

Overload

Overloading is ** writing multiple methods and constructors with the same name with different argument types and numbers. ** **

Method with overload

The same method name, but the return value may change depending on the number of arguments given. This defines a plus method that does the addition.

keisan.Java


//Sum method to add

public class sum {
    public int sum(int a) {
        return a;
    }
    public int sum(int a, int b) {
        return a + b;
    }
    public int sum(int a, int b, int c) {
        return a + b + c;
    }
}

Inherit the class.

Inheritance is the inheritance of any class by another class.

This time, Keisan.Java is inherited by Task.java, and arguments are given to perform the calculation.

Task.java


public class Task extends Keisan {

     public void taskJikkou() {
       System.out.println("【Execution result】");
       System.out.println("a is"Sum(1)); //result:a is1
       System.out.println("a +b is"Sum(5, 10)); //result: a +b is 15
       System.out.println("a + b +c is"Sum(100, 150, 200)); //result: a + b +c is 450
       }
} 

Create an instance in Main.java and execute it.

Main.java


 public class Main {
 
     public static void main(String[] args) {
        Task task = new Task(); //Create an instance
        task.taskJikkou(); //Task.Call the java taskJikkou method.
     }
 }
【Execution result】
a is 1
a +b is 15
a + b +c is 450

This time I was doing calculations, but it seems that I can express various things by using these. I'm excited that there are many uses, such as "driving" a car and "kodo" of an RPG.

Recommended Posts

[Java] Overload / Override / Inheritance
[Java] Overload and override
java (override)
[Java] Inheritance
Java inheritance
[Java] Overload
Java inheritance
java (inheritance)
[Java] Class inheritance
About java inheritance
Summarize Java inheritance (Java Silver 8)
About inheritance (Java Silver)
[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation]
[Java] Implicit inheritance memo
Java learning memo (inheritance)
Advanced inheritance abstract, interface -java
JAVA learning history interface inheritance
Java starting from beginner, override
Java starting from beginner, inheritance
[Java] What is class inheritance?
java (inheritance of is-a principle)
Muscle Java Object Oriented Day 2 ~ Inheritance ~
Inheritance
Inheritance
Overload
Java
Java overload constructor starting from beginner
Java
[Java] Inheritance and structure of HttpServlet class
Let's be lazy with inheritance and override
[Easy-to-understand explanation! ] How to use Java overload