public class Animal {
public void printInfo() {
System.out.println("This is Animal");
}
public void printInfo(String name) {
System.out.println("This is Animal whose name is " + name);
}
}
public class ExeOverloading {
public static void main(String[] args) {
Animal animal = new Animal();
animal.printInfo();
animal.printInfo("Jack");
}
}
결과
This is Animal
This is Animal whose name is Jack
public class Cat extends Animal{
@Override
public void printInfo() {
System.out.println("This is Cat");
}
}
public class ExeOverriding {
public static void main(String[] args) {
Animal animal = new Animal();
Cat cat = new Cat();
animal.printInfo();
cat.printInfo();
}
}
결과
This is Animal
This is Cat
항목 | Overloading | Overriding |
---|---|---|
메서드명 | 같다 | 같다 |
매개변수, 타입 | 다르다 | 같다 |
리턴타입 | 상관없음 | 같다 |