MapleStory Finger Point
본문 바로가기
Back-end/Java

[Java] 혼자 공부하는 자바 385p 풀어보기

by 디팔⸜( ◜࿁◝ )⸝︎︎ 2022. 9. 20.

3번 문제

Interface 구현 및 추상메소드를 구현 객체에 재정의 연습해보기.

package com.example.myjavaproject;

public class main {
    private static void printSound(Soundable soundable) {
        System.out.println(soundable.sound());
    }


    public static void main(String[] args) {
        printSound(new Cat());
        printSound(new Dog());
    }
}

    interface Soundable {
        String sound();
    }


    class Cat implements Soundable {
        public String sound() {
            return "야옹";
        }
    }

    class Dog implements Soundable {
        public String sound() {
            return "멍멍";
        }
    }

댓글