728x90
반응형
1. 추상 클래스(abstract class)는 Java에서 다음과 같은 특징을 가진 클래스입니다.
1) abstract 키워드를 사용하여 선언합니다.
2) 하나 이상의 추상 메소드를 포함할 수 있습니다. 추상 메소드는 선언만 있고 구현은 없는 메소드입니다.
3) 인스턴스화할 수 없습니다. 즉, new 키워드로 객체를 직접 생성할 수 없습니다.
4) 일반 메소드, 생성자, 필드(멤버 변수)도 포함할 수 있습니다.
5) 상속을 통해 사용되며, 상속 키워드로 extends를 사용합니다.
6) 추상 클래스를 상속받는 자식 클래스는 모든 추상 메소드를 반드시 구현(오버라이딩) 해야 합니다.
7) 다중 상속은 불가능합니다.
2. 추상 클래스의 주요 용도
1) 관련된 클래스들 간의 공통 인터페이스나 동작을 정의합니다.
2) 코드 재사용성을 높이고 관련 클래스들의 일관성을 유지합니다.
3) 템플릿 메서드 패턴과 같은 디자인 패턴을 구현하는 데 사용됩니다.
3. 예시
package entety; public abstract class Product { protected String productId; protected String name; protected double price; public Product() { } public Product(String productId, String name, double price) { this.productId = productId; this.name = name; this.price = price; } |
// 상품 정보를 출력하는 추상 메서드 public abstract void displayInfo(); // 할인된 가격을 계산하는 추상 메서드 public abstract double calculateDiscountedPrice(); } |
package entety; public abstract class Product { protected String productId; protected String name; protected double price; public Product() { } public Product(String productId, String name, double price) { this.productId = productId; this.name = name; this.price = price; } // 상품 정보를 출력하는 추상 메서드 public abstract void displayInfo(); // 할인된 가격을 계산하는 추상 메서드 public abstract double calculateDiscountedPrice(); } |
package entety; public class Electronics extends Product { private String brand; private int warrantyPeriod; // 무상 보증 개월 단위 // 디폴트 생성자 public Electronics() { } // 매개변수가 있는 생성자 public Electronics(String productId, String name, double price, String brand, int warrantyPeriod) { super(productId, name, price); this.brand = brand; this.warrantyPeriod = warrantyPeriod; } |
@Override public void displayInfo() { // 다음 정보를 순서대로 출력: // 1. "상품 ID: [productId]" // 2. "상품 이름: [name]" // 3. "가격: [price]원" // 4. "브랜드: [brand]" // 5. "보증 기간: [warrantyPeriod]개월" // 6. "할인된 가격: [calculateDiscountedPrice()]원" // 7. 빈 줄 출력 System.out.println("상품 ID: " + productId); System.out.println("상품 이름: " + name); System.out.println("가격: " + price + "원"); System.out.println("브랜드: " + brand); System.out.println("보증 기간: " + warrantyPeriod + "개월"); // double discountedPrice = calculateDiscountedPrice() System.out.println("할인된 가격: " + calculateDiscountedPrice() + "원"); System.out.println( ); } @Override public double calculateDiscountedPrice() { // price에서 10% 할인된 가격을 계산하여 리턴하시오. double discountedPrice = price * 0.9; return discountedPrice; // 아래와 같이도 가능합니다. // double discountedPrice = calculateDiscountedPrice() // return price * 0.9; } |
package entety; public class Electronics extends Product { private String brand; private int warrantyPeriod; // 무상 보증 개월 단위 // 디폴트 생성자 public Electronics() { } // 매개변수가 있는 생성자 public Electronics(String productId, String name, double price, String brand, int warrantyPeriod) { super(productId, name, price); this.brand = brand; this.warrantyPeriod = warrantyPeriod; } @Override public void displayInfo() { // 다음 정보를 순서대로 출력: // 1. "상품 ID: [productId]" // 2. "상품 이름: [name]" // 3. "가격: [price]원" // 4. "브랜드: [brand]" // 5. "보증 기간: [warrantyPeriod]개월" // 6. "할인된 가격: [calculateDiscountedPrice()]원" // 7. 빈 줄 출력 System.out.println("상품 ID: " + productId); System.out.println("상품 이름: " + name); System.out.println("가격: " + price + "원"); System.out.println("브랜드: " + brand); System.out.println("보증 기간: " + warrantyPeriod + "개월"); System.out.println("할인된 가격: " + calculateDiscountedPrice() + "원"); System.out.println( ); } @Override public double calculateDiscountedPrice() { // price에서 10% 할인된 가격을 계산하여 리턴하시오. double discountedPrice = price * 0.9; return discountedPrice; } } |
package entety; public class Clothing extends Product { private String size; private String material; // 디폴트 생성자 public Clothing() { } // 매개변수가 있는 생성자 public Clothing(String productId, String name, double price, String size, String material) { super(productId, name, price); this.size = size; this.material = material; } |
@Override public void displayInfo() { // 다음 정보를 순서대로 출력: // 1. "상품 ID: [productId]" // 2. "상품 이름: [name]" // 3. "가격: [price]원" // 4. "사이즈: [size]" // 5. "소재: [material]" // 6. "할인된 가격: [calculateDiscountedPrice()]원" // 7. 빈 줄 출력 System.out.println("상품 ID: " + productId); System.out.println("상품 이름: " + name); System.out.println("가격: " + price + "원"); System.out.println("사이즈: " + size); System.out.println("소재: " + material); System.out.println("할인된 가격: " + calculateDiscountedPrice() + "원"); System.out.println( ); } @Override public double calculateDiscountedPrice() { // price에서 5% 할인된 가격을 계산하여 리턴하시오. double discountedPrice = price * 0.95; return discountedPrice; } |
package entety; public class Clothing extends Product { private String size; private String material; // 디폴트 생성자 public Clothing() { } // 매개변수가 있는 생성자 public Clothing(String productId, String name, double price, String size, String material) { super(productId, name, price); this.size = size; this.material = material; } @Override public void displayInfo() { // 다음 정보를 순서대로 출력: // 1. "상품 ID: [productId]" // 2. "상품 이름: [name]" // 3. "가격: [price]원" // 4. "사이즈: [size]" // 5. "소재: [material]" // 6. "할인된 가격: [calculateDiscountedPrice()]원" // 7. 빈 줄 출력 System.out.println("상품 ID: " + productId); System.out.println("상품 이름: " + name); System.out.println("가격: " + price + "원"); System.out.println("사이즈: " + size); System.out.println("소재: " + material); System.out.println("할인된 가격: " + calculateDiscountedPrice() + "원"); System.out.println( ); } @Override public double calculateDiscountedPrice() { // price에서 5% 할인된 가격을 계산하여 리턴하시오. double discountedPrice = price * 0.95; return discountedPrice; } } |
import entety.Clothing; import entety.Electronics; public class Main { public static void main(String[] args) { // Electronics 객체 생성 (디폴트 생성자 사용) Electronics electronicProduct = new Electronics(); // Clothing 객체 생성 (디폴트 생성자 사용) Clothing clothingProduct = new Clothing(); // 각 객체의 displayInfo() 메서드 호출하여 정보 출력 electronicProduct.displayInfo(); clothingProduct.displayInfo(); // 매개변수가 있는 생성자를 사용하여 Electronics 객체 생성 // productId: "E001", name: "Smart TV", price: 1200000, brand: "삼성전자", warrantyPeriod: 24 Electronics specificElectronicProduct = new Electronics("E001", "Smart TV", 1200000, "삼성전자", 24); specificElectronicProduct.displayInfo(); // 매개변수가 있는 생성자를 사용하여 Clothing 객체 생성 // productId: "C001", name: "T-Shirt", price: 20000, size: "L", material: "폴리에스테르" Clothing specificClothingProduct = new Clothing("C001", "T-Shirt", 20000, "L", "폴리에스테르"); specificClothingProduct.displayInfo(); } } |
728x90
반응형
'JAVA > 이론' 카테고리의 다른 글
34. [ JAVA ] 문자열 생성하는 방법 (1) | 2024.12.17 |
---|---|
33. [ JAVA ] 상속 : interface (0) | 2024.12.16 |
30. [ JAVA ] 상속 : 직속 (0) | 2024.12.16 |
29. [ JAVA ] 상속 : extends (0) | 2024.12.16 |
26. [ JAVA ] 메소드 오버로딩 (Method Overloading) (1) | 2024.12.12 |