0. Introduction

<aside> 💡 과정 목표

  1. 인터페이스 사용의 잘못된 예시인 상수 인터페이스에 대해 알아봅니다.
  2. 상수 인터페이스에 대해 왜 잘못된 사용이라고 하는지에 대해 이해하고 이를 해결하는 방법에 대해 알아봅니다.

</aside>

1. 인터페이스의 용도

interface Shape {
    
    void printInfo();
    
    double getArea();
    
}

class Circle implements Shape {

		private int radius;
		
		Circle(int radius) {
				this.radius = radius;
		}
		
		@Override
		public void printInfo() {
				System.out.println("radius : " + radius);
		}
		
		@Override
		public double getArea() {
				return Math.PI * Math.pow(radius, 2);
		}
		
}

public class ShapeTest {
		
		public static void main(String[] args) {
				Shape shape = new Circle(10);
				
				double area = shape.getArea();
				System.out.println(area);
		}
}

2. 상수 인터페이스

public interface PhysicalConstants {
		
		static final double AVOGADROS_NUMBER = 6.022_140_857e23;
		
		static final double BOLTZMANN_CONSTANT = 1.380_648_52e-23;
		
		...
}
package item18;

public class ConstantInterfaceExample{

    public static void main(String[] args) {
        double number = item22.PhysicalConstants.AVOGADROS_NUMBER;
        double boltzmann = item22.PhysicalConstants.BOLTZMANN_CONSTANT;
    }

}
package item18;

import item22.PhysicalConstants;

public class InheritanceExample implements PhysicalConstants {

    public static void main(String[] args) {
        double number = AVOGADROS_NUMBER;
        double boltzmann = BOLTZMANN_CONSTANT;
    }

}