class Animal {
String name;
public void setName(String inputedName) {
this.name = inputedName;
}
}
public class Sample {
public static void main(String[] args) {
Animal cat = new Animal();
cat.setName("boby"); // 메소드 호출
Animal dog = new Animal();
dog.setName("happy");
System.out.println(cat.name);
System.out.println(dog.name);
}
}
// 출처 : https://wikidocs.net/214
class Animal {
String name;
void setName(String name) {
this.name = name;
}
}
class Dog extends Animal {
}
public class Sample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.setName("poppy");
System.out.println(dog.name); // poppy 출력
}
}
// 출처 : https://wikidocs.net/280
class Dog extends Animal {
void sleep() {
System.out.println(this.name+" zzz");
}
}
// 출처 : https://wikidocs.net/280
Animal dog = new Dog() : 우에서 좌로 읽음
상위 클래스가 가진 메소드를 하위클래스에서 재정의
class Animal {
String name;
void setName(String name) {
this.name = name;
}
}
class Dog extends Animal {
void sleep() {
System.out.println(this.name + " zzz");
}
}
class HouseDog extends Dog {
void sleep() {
System.out.println(this.name + " zzz in house");
}
}
public class Sample {
public static void main(String[] args) {
HouseDog houseDog = new HouseDog();
houseDog.setName("happy");
houseDog.sleep(); // happy zzz in house 출력
}
}
출처 : https://wikidocs.net/280
같은 이름의 메서드를 여러개 가지면 매개변수의 유형이가 개수를 다르게 하여 차이를 두는 것
class HouseDog extends Dog {
void sleep() {
System.out.println(this.name + " zzz in house");
}
void sleep(int hour) {
System.out.println(this.name + " zzz in house for " + hour + " hours");
}
}
출처 : https://wikidocs.net/280