추상 클래스

[Java] 추상 클래스(abstract class)
Dec 26, 2023
추상 클래스
추상 클래스(abstract class) 완전하게 구현되어 있지 않은 메소드를 가지고 있는 클래스 구체적, Object
 
꼭 추상 클래스를 써야하나? 꼭 안써도 되지만,
  • 추상 클래스는 new 할 수 없다.
  • 추상 메소드를 강제한다.( 메소드 이름 실수 x로 안전한 오버라이딩 가능)
 
간단 추상 클래스 예제
abstract class Shape{ // 추상 클래스 모양 int x,y; public void translate(int x, int y){ this.x = x; this.y = y; } public abstract void draw(); // 추상 메소드 draw } class Rectangle extends Shape{ int width, height; public void draw(){ System.out.println("사각형 그리기 메소드"); } } class Cirlce extends Shape{ int radius; public void draw(){ System.out.println("원 그리기 메소드"); } } public class AbstractTest { public static void draw(Shape obj){ obj.draw(); } public static void main(String[] args) { draw(new Cirlce()); draw(new Rectangle()); } }
notion image
 
Share article
RSSPowered by inblog