Animal.java
public class Animal {
public void move() {
System.out.println("동물이 움직임.");
}
public void eating() {
System.out.println("동물이 음식을 먹음.");
}
}
Human.java
public class Human extends Animal {
@Override
public void move() {
System.out.println("사람이 두 발로 움직임.");
}
public void readBook() {
System.out.println("사람이 책을 읽음.");
}
}
Tiger.java
public class Tiger extends Animal {
@Override
public void move() {
System.out.println("호랑이가 움직임.");
}
public void hunt() {
System.out.println("호랑이가 사냥을 함.");
}
}
main.java
public class Main {
public static void main(String[] args) {
Animal human = new Human();
Animal tiger = new Tiger();
human.move(); //사람이 두발로 움직임.
tiger.move(); //호랑이가 움직임.
}
}
if(grade == "VIP") {
}
else if (grade == "GOLD") {
}
else if (grade == "SILVER") {
}
//////
public void forVIPFunc(VipCustomer c) {}
public void forGoldFunc(GoldCustomer c) {}
public void forSilverFunc(Customer c){} ///객체가 추가 될때마다 그 객체 타입에 맞는 메소드를 계속 만들어주어야함... 또는 오버로딩메서드를 추가되는 객체 타입에 맞게 계속 추가..
.
.
.
Animal human1 = new Human();
Tiger tiger = new Tiger();
Human human2 = null;
Animal animal1 = null;
human2 = tiger; //에러, human과 tiger 객체는 서로 상속관계가 아니기 때문에.
animal1 = human1; //Up-Casting
human2 = (Human)animal1; //Down-Casting 으로 (Human) 자손타입을 명시해줘야 함.
검사하고자 하는 참조변수 instanceof 타입(클래스명) => 연산결과가 맞으면 true, 틀리면 false 를 반환
class BindingTest {
public static void main(String[] args) {
Parent p = new Child();
Child c = new Child();
System.out.println("p.x = " + p.x); //p.x = 100
p.method(); //Child Method
System.out.println("c.x = " + c.x); //c.x = 200
c.method(); //Child Method
}
}
public class Parent {
int x = 100;
void method() {
System.out.println("Parent Method");
}
}
public class Child extends Parent{
int x = 200;
@Override
void method() {
System.out.println("Child Method");
}
}