Technical Interview 에서 받았던 질문 中 1
OOP(객체지향 프로그래밍)에 대해 설명해주세요.
Object-Oriented Programming
객체지향 프로그래밍은 프로그램을 객체라는 독립적인 단위로 나누어 설계하는 방법이다. 객체는 데이터를 포함하고, 그 데이터를 처리하는 메서드를 함께 가지고 있다. 이를 통해 코드의 재사용성과 유지보수성을 높일 수 있다.
OOP의 주요 특징
1. 캡슐화 Encapsulation
객체 내부의 데이터를 외부에서 직접 접근하지 못하도록 하고, 메서드를 통해 접근하도록 하는 방식이다. 자바에서는 private 키워드를 사용해 필드를 숨기고, getter와 setter로 접근하는 것이 예시이다.
2. 상속 Inheritance
기존 클래스의 속성과 메서드를 물려받아 새로운 클래스를 만드는 방법으로, 코드의 재사용성을 높일 수 있다. 자바에서는 extends를 사용하여 상속을 구현한다.
3. 다형성 Polymorphism
같은 메서드 이름이지만 객체에 따라 다른 방식으로 동작할 수 있게 하는 개념이다. 예를 들어, Animal 클래스의 sound() 메서드를 Dog와 Cat 클래스에서 각각 다르게 오버라이딩하는 방식이다.
4. 추상화 Abstraction
복잡한 세부 사항은 감추고 필요한 기능만을 외부에 제공하는 방식으로, 자바의 abstract 클래스나 interface가 이에 해당된다.
(이하 내용은 예시 코드)
예시
야구를 예시로 들면 아래와 같다.
1. 캡슐화
class Player {
private String name;
private String position;
// Constructor to set the player's name and position
public Player(String name, String position) {
this.name = name;
this.position = position;
}
// Method to get the player's name
public String getName() {
return name;
}
// Method to get the player's position
public String getPosition() {
return position;
}
// Method to change the player's position
public void setPosition(String position) {
this.position = position;
}
}
public class BaseballGame {
public static void main(String[] args) {
Player player = new Player("John", "Pitcher");
// Access to fields is restricted, only accessible through methods
System.out.println(player.getName()); // John
System.out.println(player.getPosition()); // Pitcher
player.setPosition("Catcher"); // Changing position
System.out.println(player.getPosition()); // Catcher
}
}
2. 상속
class Player {
protected String name;
public Player(String name) {
this.name = name;
}
// General play method for all players
public void play() {
System.out.println(name + " is playing baseball.");
}
}
class Pitcher extends Player {
public Pitcher(String name) {
super(name);
}
// Overriding play method for pitchers
@Override
public void play() {
System.out.println(name + " is pitching the ball.");
}
}
class Catcher extends Player {
public Catcher(String name) {
super(name);
}
// Overriding play method for catchers
@Override
public void play() {
System.out.println(name + " is catching the ball.");
}
}
public class BaseballGame {
public static void main(String[] args) {
Player pitcher = new Pitcher("John");
Player catcher = new Catcher("Mike");
pitcher.play(); // John is pitching the ball.
catcher.play(); // Mike is catching the ball.
}
}
3. 다형성
Player 객체의 play() 메소드를 Player 객체를 상속받은 투수, 포수, 야수 클래스에서 각각 다른 play()를 하도록 한다.
// Parents
class Player {
public void play() {
System.out.println("Playing baseball");
}
}
// Child1
class Pitcher extends Player {
@Override
public void play() {
System.out.println("Throwing the ball");
}
}
// Child2
class Catcher extends Player {
@Override
public void play() {
System.out.println("Catching the ball thrown by the pitcher");
}
}
// Child3
class Fielder extends Player {
@Override
public void play() {
System.out.println("Fielding the ball in the outfield");
}
}
public class BaseballGame {
public static void main(String[] args) {
Player pitcher = new Pitcher();
Player catcher = new Catcher();
Player fielder = new Fielder();
pitcher.play(); // "Throwing the ball"
catcher.play(); // "Catching the ball thrown by the pitcher"
fielder.play(); // "Fielding the ball in the outfield"
}
}
4. 추상화
abstract class Player {
protected String name;
public Player(String name) {
this.name = name;
}
// Abstract method that must be implemented by subclasses
public abstract void play();
}
class Pitcher extends Player {
public Pitcher(String name) {
super(name);
}
// Implementing the abstract method for pitchers
@Override
public void play() {
System.out.println(name + " is pitching the ball.");
}
}
class Catcher extends Player {
public Catcher(String name) {
super(name);
}
// Implementing the abstract method for catchers
@Override
public void play() {
System.out.println(name + " is catching the ball.");
}
}
public class BaseballGame {
public static void main(String[] args) {
Player pitcher = new Pitcher("John");
Player catcher = new Catcher("Mike");
pitcher.play(); // John is pitching the ball.
catcher.play(); // Mike is catching the ball.
}
}
'JAVA > JAVACORE' 카테고리의 다른 글
[TOOL] Debugging on Eclipse (1) | 2024.10.20 |
---|---|
[COMPARISON] State / Behavior in Object (2) | 2024.10.03 |
[COMPARISON] Object / Class (3) | 2024.10.03 |