추상클래스와 인터페이스, 다형성 등 헷갈리면서도 중요한 개념 숙지
CH5. 추상클래스와 인터페이스의 등장
- 다형성을 보장한다는 의미는 무엇인가
개념:
다형성은 동일한 메서드 호출에 대해 다양한 구현이 가능한 것을 의미합니다. 사람 클래스의 추상 메서드를 다양한 하위 클래스에서 구현함으로써 다형성을 보장합니다.
다형성을 보장한다는 것은 객체 지향 프로그래밍에서 유연하고 확장 가능한 코드를 작성하는 데 중요한 요소입니다.
예시:
interface Activity { - void perform();
- }
- class Student implements Activity {
- public void perform() {
- System.out.println("Student is studying");
- }
- }
- class Teacher implements Activity {
- public void perform() {
- System.out.println("Teacher is teaching");
- }
- }
- public class Main {
- public static void main(String[] args) {
- Activity person1 = new Student();
- Activity person2 = new Teacher();
- person1.perform(); // 다형성을 통해 Student의 perform 메서드 호출
- person2.perform(); // 다형성을 통해 Teacher의 perform 메서드 호출
- }
- }
java
Copy code
`interface Activity { void perform(); }
class Student implements Activity { public void perform() { System.out.println("Student is studying"); } }
class Teacher implements Activity { public void perform() { System.out.println("Teacher is teaching"); } }
public class Main { public static void main(String[] args) { Activity person1 = new Student(); Activity person2 = new Teacher();
person1.perform(); // 다형성을 통해 Student의 perform 메서드 호출- person2.perform(); // 다형성을 통해 Teacher의 perform 메서드 호출
- }
}`- 추상클래스와 다형성
- abstract class Person {
- abstract void speak();
- }
- class Student extends Person {
- void speak() {
- System.out.println("Student speaks");
- }
- }
- class Teacher extends Person {
- void speak() {
- System.out.println("Teacher speaks");
- }
- }
- public class Main {
- public static void main(String[] args) {
- Person person1 = new Student();
- Person person2 = new Teacher();
- person1.speak(); // 다형성을 통해 Student의 speak 메서드 호출
- person2.speak(); // 다형성을 통해 Teacher의 speak 메서드 호출
- }
- }
- 인터페이스의 등장
- interface Work {
- void doWork();
- }
- class Student implements Work {
- public void doWork() {
- System.out.println("Student is studying");
- }
- }
- class Teacher implements Work {
- public void doWork() {
- System.out.println("Teacher is teaching");
- }
- }
- public class Main {
- public static void main(String[] args) {
- Work person1 = new Student();
- Work person2 = new Teacher();
- person1.doWork(); // 다형성을 통해 Student의 doWork 메서드 호출
- person2.doWork(); // 다형성을 통해 Teacher의 doWork 메서드 호출
- }
- }
- 인터페이스와 다형성
- interface Activity {
- void perform();
- }
- class Student implements Activity {
- public void perform() {
- System.out.println("Student is studying");
- }
- }
- class Teacher implements Activity {
- public void perform() {
- System.out.println("Teacher is teaching");
- }
- }
- public class Main {
- public static void main(String[] args) {
- Activity person1 = new Student();
- Activity person2 = new Teacher();
- person1.perform(); // 다형성을 통해 Student의 perform 메서드 호출
- person2.perform(); // 다형성을 통해 Teacher의 perform 메서드 호출
- }
- }
- 인터페이스와 다중상속
- interface Eat {
- void eat();
- }
- interface Sleep {
- void sleep();
- }
- class Human implements Eat, Sleep {
- public void eat() {
- System.out.println("Human is eating");
- }
- public void sleep() {
- System.out.println("Human is sleeping");
- }
- }
- public class Main {
- public static void main(String[] args) {
- Human human = new Human();
- human.eat(); // Eat 인터페이스의 eat 메서드 호출
- human.sleep(); // Sleep 인터페이스의 sleep 메서드 호출
- }
- }
- 추상클래스와 인터페이스 비교
- abstract class Shape {
- abstract void draw();
- }
- interface Color {
- void setColor(String color);
- }
- class Circle extends Shape implements Color {
- public void draw() {
- System.out.println("Circle is drawn");
- }
- public void setColor(String color) {
- System.out.println("Circle color is set to " + color);
- }
- }
- public class Main {
- public static void main(String[] args) {
- Circle circle = new Circle();
- circle.draw(); // Shape 추상 클래스의 draw 메서드 호출
- circle.setColor("Red"); // Color 인터페이스의 setColor 메서드 호출
- }
- }
CH6. 자바 최상위 클래스 Object
- Object클래스를 이용하여 객체 생성하기
- Object object1 = new String("Hello");
- Object object2 = new Integer(10);
- /*
- 위의 예시에서는 Object 클래스를 이용하여 String과 Integer 클래스의 객체를 생성하고 있습니다.
- Object 클래스의 참조 변수(object1, object2)는
- 각각 String과 Integer 클래스의 객체를 참조할 수 있습니다.
- 이렇게 Object 클래스를 이용하여 객체를 생성할 경우,
- 해당 객체가 Object 클래스의 메서드를 모두 상속받게 됩니다.
- */
- Object클래스를 활용한 다형성 적용
- public class Main {
- public static void main(String[] args) {
- Object obj1 = new String("Hello");
- Object obj2 = new Integer(10);
- // Object 클래스의 참조 변수로 다양한 클래스의 객체를 참조할 수 있음
- System.out.println(obj1); // "Hello" 출력
- System.out.println(obj2); // "10" 출력
- // Object 클래스의 다형성을 이용하여 배열에 여러 타입의 객체를 저장할 수 있음
- Object[] objects = new Object[3];
- objects[0] = "String";
- objects[1] = 10;
- objects[2] = new Double(3.14);
- for (Object obj : objects) {
- System.out.println(obj);
- }
- }
- }
- Object클래스의 toString() 메서드
- class Person {
- private String name;
- private int age;
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- @Override
- public String toString() {
- return "Person{name='" + name + "', age=" + age + "}";
- }
- }
- public class Main {
- public static void main(String[] args) {
- Person person = new Person("John", 30);
- System.out.println(person.toString()); // "Person{name='John', age=30}" 출력
- }
- }
- /*
- 위의 예시에서는 Person 클래스에 toString() 메서드를 오버라이딩하여 객체의 상태를 문자열로 표현하고 있습니다. 이렇게 오버라이딩된 toString() 메서드를 호출하면 객체의 상태를 효과적으로 나타낼 수 있습니다.
- */
'Backend > Backend 관련 학습 내용' 카테고리의 다른 글
자바 API 활용 및 API 만들기 (2) | 2024.11.29 |
---|---|
객체지향 프로그래밍 설계하기 & 상속관계에서 객체생성 및 Override (0) | 2024.11.29 |
Static과 JVM 모델 이해 (0) | 2024.11.29 |
객체지향 프로그래밍(OOP) (0) | 2024.11.29 |
인텔리제이 단축키 (0) | 2024.11.29 |