**
Person 타입 → Person 호출.실행 = 정적바인딩 ( C++ )
Person 타입 → Person 호출, new Student() 실행 → Student 호출 = 동적바인딩
public abstract class Shape { // 필드 private String type; // 생성자 public Shape(String type) { super(); this.type = type; } public String getType() { return type; } public void setType(String type) { this.type = type; } public abstract double getArea(); // Shape을 상속 받는 객체들이 호출할 때 사용하는 메소드 // 사용되지는 않는다. -> 추상 메소드로 바꿔준다. ( 본문이 없는 메소드 ) }
public class Circle extends Shape{ //필드 private double radius; //생성자 public Circle(String type, double radius) { super(type); this.radius = radius; } // Shape 클래스는 추상 클래스 이므로, 반드시 double getArea() 메소드를 오버라이드 해야 함 @Override public double getArea() { return Math.PI * Math.pow(radius, 2); } }
public class Main { public static void main(String[] args) { // Shape 클래스타입의 객체는 존재할 수 없는 객체이다. // abstract 처리해서 객체의 생성을 막을 수 있다. /* Shape s1 = new Shape("도형"); // 추상 클래스 객체는 못 만듬 System.out.println(s1.getType()); System.out.println(s1.getArea()); */ Shape s2 = new Circle("원", 1); System.out.println(s2.getType()); System.out.println(s2.getArea()); } }