
안녕하세요.
현재 자바 1-17 객체지향 퀴즈 강의를 듣고 있습니다.
코드를 작성하고 Main.java를 실행하는데 사진과 같은 에러가 발생하여 문의드립니다!
감사합니다.
전체 화면 캡처 및 에러 메세지

작성한 코드
(1) Main.java
public class Main {
public static void main(String[] args) {
Human child = new Child("나", 20);
Human parent = new Parent("엄마", 50);
Human grandParent = new GrandParent("할아버지", 70);
Human[] humans = {grandParent, parent, child};// 배열에 담기
for (Human human : humans) {//for-each문 적용
System.out.println(human.name + ", 나이 : "+ child.age + ", 속도 : "+ child.speed + ", 현재 위치 : "+ human.getLocation());
}
System.out.println("<활동 시작>");
for (Human human : humans) {//for-each문 적용
if (human instanceof Walkable) {// human이 걸을 수 있는 친구의 인스턴스이면, 걸어갈거야
((Walkable) human).walk(1,1);//캐스팅, Walkable의 인스턴스 타입으로 인식됨
System.out.println("- - - - - - - -");
}
if (human instanceof Runnable) {
((Runnable) human).run(2,2);
System.out.println("- - - - - - - -");
}
if (human instanceof Swimmable) {
((Swimmable) human).swim(3, -1);
System.out.println("- - - - - - - - ");
}
}
}
}
(2) Human.java
public class Human {
String name;
int age;
int speed;
int x,y;
public Human(String name, int age, int speed, int x, int y) {// 초기화할 수 있는 constructor 생성 alt+insert
this.name = name;
this.age = age;
this.speed = speed;
this.x = x;
this.y = y;
}
public Human(String name, int age, int speed) {// 위치 기본값 0
this(name, age, speed, 0,0);
}
public String getLocation() {// 위치 정보
return "( " + x + ", " + y + ")";
}
protected void printWhoAmI() {
System.out.println("My name is "+name+ "." + age + "aged.");
}
}
(3) Walkable.java
public interface Walkable {
void walk(int x, int y);
}
(4) Runnable.java
public interface Runnable {
void run(int x, int y);
}
(5) Swimmable.java
public interface Swimmable {
void swim(int x, int y);
}
(6) GrandParent.java
public class GrandParent extends Human implements Walkable{
public GrandParent(String name, int age) {//조부모의 기본 속도 : 1 -> parameter로 받을 필요가 없다.
super(name, age, 1);
}
@Override
public void walk(int x, int y) {
printWhoAmI();
System.out.println("walk speed : " + speed);
this.x = x;
this.y = y;
System.out.println("Walked to " + getLocation());
}
}
(7) Parent.java
public class Parent extends Human implements Walkable, Runnable{
public Parent(String name, int age) {//기본 속도 : 3 지정되어 있으므로
super(name, age, 3);
}
@Override
public void walk(int x, int y) {
printWhoAmI();
System.out.println("walk speed : " + speed);
this.x = x;
this.y = y;
System.out.println("Walked to " + getLocation());
}
@Override
public void run(int x, int y) {
printWhoAmI();
System.out.println("run speed : " + (speed+2));
this.x = x;
this.y = y;
System.out.println("Ran to " + getLocation());
}
}
(8) Child.java
public class Child extends Human implements Walkable, Runnable, Swimmable{
public Child(String name, int age) {// 기본 속도 : 5
super(name, age, 5);
}
@Override
public void walk(int x, int y) {
printWhoAmI();
System.out.println("walk speed : " + speed);
this.x = x;
this.y = y;
System.out.println("Walked to " + getLocation());
}
@Override
public void run(int x, int y) {
printWhoAmI();
System.out.println("run speed : " + (speed+2));
this.x = x;
this.y = y;
System.out.println("Ran to " + getLocation());
}
@Override
public void swim(int x, int y) {
printWhoAmI();
System.out.println("swim speed : " + (speed+1));
this.x = x;
this.y = y;
System.out.println("swum to " + getLocation());
}
}
