[Java] Overload and override

Summarize overloads and overrides

Overload

Define multiple ** different ** methods with the same name but ** signature **. → Even if the name is the same, ** the method is different **

** * What is signature ** -Method name -Argument list type -Number of arguments -Argument order

** [Things not to consider when overloading] ** ・ Difference in return value -Variable name of the argument -Differences in access modifiers

Example)

int price(int a, double b){
   return a * b;
}

[Overloading pattern]

double price(int a){   //Since it is treated as a separate method, there is no problem even if the return value is changed. Overloaded because the signatures are different
  //processing
}

int price(double a, int b){  //Argument order reversed → overload
  //processing
}

[** It is also possible to overload methods ** with final **]

final void price(){
   //processing
}

void prince(int a){  //The final method can also be overloaded! Because it is treated as another method.
  //processing
}

[Pattern that does not cause overload]

int price(int product, double tax){  //The argument name is not a signature. Does not overload
   //processing
}

override

When you ** inherit ** a parent class ** and declare a ** child class **, you must ** overwrite the members of the parent class with the child class **.

** [Override conditions] ** ・ The signature is the same -** Return type ** must be ** same type ** or ** subclass type ** as a member of parent class -Specify the ** access modifier ** that is ** the same as or ** looser ** than the member of the parent class. -The member of the parent class does not have ** final **

Example)

class Infant{
  protected void talk(){
     System.out.println("Ogyaa");
  }
} 

[Patterns that can be overridden]

class Adult extends Infant {
  public void talk(){   //Loose access modifiers than the parent class talk method
    System.out.println("Hello");
 }
}

[Patterns that cannot be overridden]

class Adult extends Infant {
  void talk(){   //More stringent access modifiers than the parent class talk method
    System.out.println("Hello");
 }
}

class Adult extends Infant {
  void talk(String a){   //Signature different from the talk method of the parent class (overload)
    System.out.println("Hello" + a);
 }
}

Recommended Posts

[Java] Overload and override
[Java] Overload / Override / Inheritance
java (override)
[Java] Overload
XXE and Java
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
Study Java # 2 (\ mark and operator)
[Java] Difference between == and equals
[Java] Stack area and static area
Java starting from beginner, override
[Java] Generics classes and generics methods
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
Java if and switch statements
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
Java methods and method overloads
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
[Java] Upload images and base64
C # and Java Overrides Story
Java abstract methods and classes
Java encapsulation and getters and setters
Kotlin post- and pre-increment and operator overload (comparison with C, Java, C ++)
ClassCastException occurs when migrating from Java7 to Java8 ~ Generics and overload ~
About Java static and non-static methods
I compared PHP and Java constructors
Differences between "beginner" Java and Kotlin
Distributed tracing with OpenCensus and Java
[Java] Difference between Hashmap and HashTable
Java variable declaration, initialization, and types
Java Excel Insertion and Image Extraction
Install Java and Tomcat with Ansible
AWS SDK for Java 1.11.x and 2.x
[Java] Basic types and instruction notes
Java release date and EOL summary
Java and first-class functions-beyond functional interfaces-
About fastqc of Biocontainers and Java
Java for beginners, expressions and operators 1
Java Primer Series (Variables and Types)
Encoding and Decoding example in Java
[Java beginner] About abstraction and interface
Basic data types and reference types (Java)
[Java] Loop processing and multiplication table
Java 15 implementation and VS Code preferences
Java arguments, return values and overloads
OpenJDK 8 java and javac command help
Java reference mechanism (stack and heap)