μ΄ κΈμ μνμ½λ©μ 'Java' κ°μ’λ₯Ό λ°νμΌλ‘ μ 리ν λ΄μ©μ λλ€.
μ°½μμ μΈ μμ
μμμ μμ ν΄λμ€μ κΈ°λ₯μ νμ ν΄λμ€μκ² λ¬Όλ €μ£Όλ κΈ°λ₯μ λλ€. κ·Έλ λ€λ©΄ νμ ν΄λμ€λ μμ ν΄λμ€μ λ©μλλ₯Ό μ£Όμ΄μ§ κ·Έλλ‘ μ¬μ©ν΄μΌ ν κΉμ? λ§μ½ κ·ΈλμΌ νλ€λ©΄ μ μ½μ΄ μλΉν κ²μ λλ€. μ΄λ° μ μ½μ λ²μ΄λλ €λ©΄ νμ ν΄λμ€κ° λΆλͺ¨ ν΄λμ€μ κΈ°λ³Έμ μΈ λμλ°©λ²μ λ³κ²½ν μ μμ΄μΌ ν©λλ€. μ΄λ° λ§₯λ½μμ λμ λ κΈ°λ₯μ΄ λ©μλ μ€λ²λΌμ΄λ©(overriding)μ λλ€.
package org.opentutorials.javatutorials.overriding.example1;
class Calculator {
int left, right;
public void setOprands(int left, int right) {
this.left = left;
this.right = right;
}
public void sum() {
System.out.println(this.left + this.right);
}
public void avg() {
System.out.println((this.left + this.right) / 2);
}
}
class SubstractionableCalculator extends Calculator {
public void sum() {
System.out.println("μ€ν κ²°κ³Όλ " +(this.left + this.right)+"μ
λλ€.");
}
public void substract() {
System.out.println(this.left - this.right);
}
}
public class CalculatorDemo {
public static void main(String[] args) {
SubstractionableCalculator c1 = new SubstractionableCalculator();
c1.setOprands(10, 20);
c1.sum();
c1.avg();
c1.substract();
}
}
// μ€ν κ²°κ³Όλ 30μ
λλ€.
// 15
// -10
λ©μλ sumμ΄ SubstractionableCalculatorμ μΆκ° λμμ΅λλ€. μ€νκ²°κ³Όλ c1.sumμ΄ μμ ν΄λμ€μ λ©μλκ° μλλΌ νμ ν΄λμ€μ λ©μλ sumμ μ€ννκ³ μλ€λ κ²μ 보μ¬μ€λλ€. νμ ν΄λμ€ μ μ₯μμ λΆλͺ¨ ν΄λμ€λ λ§νμλ©΄ κΈ°λ³Έμ μΈ λμ λ°©λ²μ μ μν κ²μ΄λΌκ³ μκ°ν μ μμ΅λλ€. νμ ν΄λμ€μμ μμ ν΄λμ€μ λμΌν λ©μλλ₯Ό μ μνλ©΄ λΆλͺ¨ ν΄λμ€λ‘λΆν° λ¬Όλ € λ°μ κΈ°λ³Έ λμ λ°©λ²μ λ³κ²½νλ ν¨κ³Όλ₯Ό κ°κ² λ©λλ€. κΈ°λ³Έλμμ νλκ² μ μ©λκ³ , μμΈμ μΈ λμμ λ λμ μ°μ μμλ₯Ό κ°κ²νκ³ μμ΅λλ€. μ΄κ²μ 곡νμμ μΌλ°μ μΌλ‘ λ°κ²¬λλ κ·μΉμ λλ€. μ΄κ²μ λ©μλ μ€λ²λΌμ΄λ©(overriding)μ΄λΌκ³ ν©λλ€!
μ€λ²λΌμ΄λ©μ 쑰건
class Calculator {
int left, right;
public void setOprands(int left, int right) {
this.left = left;
this.right = right;
}
public void sum() {
System.out.println(this.left + this.right);
}
public void avg() {
System.out.println((this.left + this.right) / 2);
}
}
class SubstractionableCalculator extends Calculator {
public void sum() {
System.out.println("μ€ν κ²°κ³Όλ " +(this.left + this.right)+"μ
λλ€.");
}
public int avg() {
return (this.left + this.right)/2;
}
public void substract() {
System.out.println(this.left - this.right);
}
}
public class CalculatorDemo {
public static void main(String[] args) {
SubstractionableCalculator c1 = new SubstractionableCalculator();
c1.setOprands(10, 20);
c1.sum();
c1.avg();
c1.substract();
}
}
./Playground/SubstractionableCalculator.java:9: error: avg() in SubstractionableCalculator cannot override avg() in Calculator
public int avg() {
^
return type int is not compatible with void
1 error
overridingμ νκΈ° μν΄μλ λ©μλμ λ¦¬ν΄ νμμ΄ κ°μμΌ ν©λλ€. μ¦ ν΄λμ€ Calculatorμ λ©μλ avgλ λ¦¬ν΄ νμ μ΄ voidμ λλ€. κ·Έλ°λ° μ΄κ²μ μμν ν΄λμ€ SubstractionableCalculatorμ λ¦¬ν΄ νμ μ intμ λλ€. μ€λ²λΌμ΄λ©μ νκΈ° μν΄μλ μλμ 쑰건μ μΆ©μ‘±μμΌμΌ ν©λλ€.
- λ©μλμ μ΄λ¦
- λ©μλ 맀κ°λ³μμ μ«μμ λ°μ΄ν° νμ κ·Έλ¦¬κ³ μμ
- λ©μλμ λ¦¬ν΄ νμ
μμ κ°μ΄ λ©μλμ ννλ₯Ό μ μνλ μ¬νλ€μ ν΅νΈμ΄μ λ©μλμ μλͺ (signature)λΌκ³ ν©λλ€. μ¦ μμ μλ¬λ λ©μλλ€ κ°μ μλͺ μ΄ λ¬λΌμ λ°μν λ¬Έμ μ λλ€. μλμ κ°μ΄ μμ ν΄λμ€μ μ½λλ₯Ό λ³κ²½ν΄μ μ΄ λ¬Έμ λ₯Ό ν΄κ²°ν΄λ΄ μλ€.
class Calculator {
int left, right;
public void setOprands(int left, int right) {
this.left = left;
this.right = right;
}
public void sum() {
System.out.println(this.left + this.right);
}
public int avg() {
return (this.left + this.right)/2;
}
}
class SubstractionableCalculator extends Calculator {
public void sum() {
System.out.println("μ€ν κ²°κ³Όλ " +(this.left + this.right)+"μ
λλ€.");
}
public int avg() {
return (this.left + this.right)/2;
}
public void substract() {
System.out.println(this.left - this.right);
}
}
public class CalculatorDemo {
public static void main(String[] args) {
SubstractionableCalculator c1 = new SubstractionableCalculator();
c1.setOprands(10, 20);
c1.sum();
c1.avg();
c1.substract();
}
}
// output
// μ€ν κ²°κ³Όλ 30μ
λλ€.
// -10
μμμ λΆλͺ¨μ μμ μμ΄ μμμ μμ μΌλ‘ λ³κ²½νλ κ² λͺ©μ μΈλ°, μ΄ μμ λ μ’μ μμ λ μλλλ€.
μμ ν΄λμ€μ νμ ν΄λμ€μ μλͺ μ΄ κ°κΈ° λλ¬Έμ λ©μλ μ€λ²λΌμ΄λ©μ ν μ μμμ΅λλ€. κ·Έλ°λ° μμ μ½λλ₯Ό 보면 μ€λ³΅μ΄ λ°μνμ΅λλ€. λ©μλ avgμ λΆλͺ¨μ μμ ν΄λμ€κ° κ°μ λ‘μ§μ κ°μ§κ³ μμ΅λλ€. μ€λ³΅μ μ κ±° λμ΄μΌ ν©λλ€. μμ±μμ λ§μ°¬κ°μ§λ‘ superλ₯Ό μ¬μ©νλ©΄ μ΄ λ¬Έμ λ₯Ό ν΄κ²° ν μ μμ΅λλ€.
class Calculator {
int left, right;
public void setOprands(int left, int right) {
this.left = left;
this.right = right;
}
public void sum() {
System.out.println(this.left + this.right);
}
public int avg() {
return ((this.left + this.right) / 2);
}
}
class SubstractionableCalculator extends Calculator {
public void sum() {
System.out.println("μ€ν κ²°κ³Όλ " +(this.left + this.right)+"μ
λλ€.");
}
public int avg() {
return super.avg();
}
public void substract() {
System.out.println(this.left - this.right);
}
}
public class CalculatorDemo {
public static void main(String[] args) {
SubstractionableCalculator c1 = new SubstractionableCalculator();
c1.setOprands(10, 20);
c1.sum();
System.out.println("μ€ν κ²°κ³Όλ" + c1.avg());
c1.substract();
}
}
μ ν΄λμ€μ λ©μλ avgμμ μμ ν΄λμ€μ λ©μλλ₯Ό νΈμΆνκΈ° μν΄μ superλ₯Ό μ¬μ©νκ³ μμ΅λλ€. λλΆμ μ½λμ μ€λ³΅μ μ κ±° ν μ μμμ΅λλ€. μ΄λ κ²ν΄μ λΆλͺ¨ ν΄λμ€μ κΈ°λ₯μ λ³κ²½ ν μ μλ λ°©λ²μΈ λ©μλ μ€λ²λΌμ΄λ©μ λν΄μ μμλ΄€μ΅λλ€~!
'Programming > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] overloading μ΄λ? μ€λ²λ‘λ© λ» (0) | 2022.11.01 |
---|---|
[Java] μμκ³Ό μμ±μ (0) | 2022.10.25 |
[Java] μμ (0) | 2022.10.24 |
[Java] μ΄κΈ°νμ μμ±μ (0) | 2022.10.24 |