์ถ์ํด๋์ค์ ์ธํฐํ์ด์ค๊ฐ ๋ฌด์์ธ์ง์๊ณ ์ด๋ป๊ฒ ์ฐ์ด๋์ง๋ ์์ง๋ง, ๋์ ๋น๊ตํด์ ์ค๋ช ํ๋ผ๊ณ ํ๋ฉด ๋ง์ ๋ง๋ฌธ์ด ๋งํ๊ฒ ๋๋ค. ์ด๋ฒ์ ๊ทธ๋์ ๊ทธ ๋์ ๊ฐ๋ ์ ์ค๋ช ํด์ฃผ๊ณ , ๊ณตํต์ ๊ณผ ์ฐจ์ด์ ์ ์ค๋ช ํ๋ ค๊ณ ํ๋ค. ๊ฐ๋จํ๊ฒ ๋น์ ๋ฅผ ํ์๋ฉด ์ถ์ํด๋์ค='๋ณต์ ' ์ด๊ณ , ์ธํฐํ์ด์ค='์ค๊ณ๋'๋ผ๊ณ ํฉ๋๋ค. ์ ๊ทธ๋ฐ์ง ํ๋ฒ ์์๋ณผ๊น์?
public abstract class ํด๋์ค์ด๋ฆ{
...๋ณ์์ ์ธ ๋ฐ ์ผ๋ฐ๋ฉ์๋...
abstract ๋ฆฌํดํ์
๋ฉ์๋์ด๋ฆ();
//์ค๊ดํธ ์์ด ๊ตฌํํ์ง ์๋ ์ํ
}โ ์ถ์ํด๋์ค์ ๋คํ์ฑ
์ถ์ํด๋์ค๋ "๋คํ์ฑ"์ ๋ณด์ฅํ๊ธฐ ์ํด ๋ํ๋ ๊ฐ๋ ์ ๋๋ค. ์์ํด๋์ค์์ ๋ฐ๋์ ๋ฉ์๋๋ฅผ ์ฌ์ ์ํด์ผ ํด์ผํ๋ฏ๋ก ๋คํ์ฑ ๋ณด์ฅ๋ฉ๋๋ค.
โ ์ถ์ํ
๊ณตํต๋ ํ๋(๋ฉ์๋), ํ๋(๋ณ์)๋ฅผ ๋ฌถ์ด ํ๋์ ํด๋์ค๋ฅผ ๋ง๋๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
โ ์ถ์ ๋ฉ์๋
๋ฉ์๋ ์ ์ธ๋ง ๋์ด์๊ณ ๊ตฌํ๋ถ๊ฐ ์๋ ๋ฉ์๋
public abstract class Parent {
private int num;
public void print(){ //์ผ๋ฐ ํด๋์ค๊ฐ ์กด์ฌ
System.out.println("print() ํธ์ถ!");
}
public abstract int sum(int num1, int num2);
//{}๋ก ๊ตฌํ์ด ์๋ ์ถ์๋ฉ์๋๊ฐ ์กด์ฌ
}
public class Child extends Parent{
//์ค๋ฒ๋ผ์ด๋ฉ Generate-> overide method
// ์ถ์๋ฉ์๋๋ฅผ ๋ฐ๋์ ์ค๋ฒ๋ผ์ด๋ฉ ํด์ผ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
@Override
public int sum(int num1, int num2) {
return num1 + num2;
}
}
public class Abstract1 {
public static void main(String[] args) {
// Parent parent = new Parent(); //๊ฐ์ฒด๋ฅผ ์์ฑ ํ ์ ์์
Child child = new Child();
child.print();
int result = child.sum(10,5);
System.out.println(result);
}
}
์ ์: ๊ฐ๋ฐ์ฝ๋์ ๊ฐ์ฒด๊ฐ ์๋ก ํต์ ํ๋ ์ ์ ์ญํ
์๋ฐ๋ ๋ค์ค ์์์ ์ง์ํ์ง ์์ผ๋ฏ๋ก ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํด์ ๋ค์ค ์์์ ๊ตฌํํฉ๋๋ค.
๋ค๋ฅธ ํด๋์ค๋ฅผ ์์ฑํ ๋ ๊ธฐ๋ณธ์ด ๋๋ ํ์ ์ ๊ณตํ๋ฉด์ ๋ค๋ฅธ ํด๋์ค ์ฌ์ด์ ๋งค๊ฐ ์ญํ ์ ๋ด๋นํ๋ ์ผ์ข ์ ์ถ์ ํด๋์ค์ ๋น์ทํฉ๋๋ค.
์ธํฐํ์ด์ค๋ ์ถ์ ๋ฉ์๋์ ์์๋ง์ ํฌํจํ ์ ์์์ง๋ง java 8๋ถํฐ ์ธ์คํด์ค ๋ฉ์๋, static ๋ฉ์๋๋ ํฌํจํ ์ ์์ต๋๋ค.
์ธํฐํ์ด์ค ์ ์ธ
์ ๊ทผ์ ์ด์ interface ์ธํฐํ์ด์ค์ด๋ฆ {
ํ์
์์์ด๋ฆ = ๊ฐ;
//public static final ํ์
๋ณ์๋ช
์ผ๋ก JVM์ด ์๋์ผ๋ก ๋ณํ
public ๋ฐํํ ๋ฉ์๋๋ช
(๋งค๊ฐ๋ณ์1, ๋งค๊ฐ๋ณ์2 ..);
//abstract๋ฅผ ์์ฑํ์ง ์์๋ JVM์์ ์๋์ผ๋ก abstract๋ฅผ ๋ถ์ฌ์ค
}
์ธํฐํ์ด์ค ๊ตฌํ
์ ๊ทผ์ ์ด์ class ํด๋์ค์ด๋ฆ implements ์ธํฐํ์ด์ค์ด๋ฆ{
...
}
์์๊ณผ ์ธํฐํ์ด์ค ๊ตฌํ์ ๋์์ ํ๋ ค๋ฉด
์ ๊ทผ์ ์ด์ class ํด๋์ค์ด๋ฆ extends ๋ถ๋ชจํด๋์ค์ด๋ฆ implements ์ธํฐํ์ด์ค์ด๋ฆ{
...
}
์์๊ณผ ๋ค์ค ์ธํฐํ์ด์ค ๊ตฌํ์ ๋์์ ํ๋ ค๋ฉด
์ ๊ทผ์ ์ด์ class ํด๋์ค์ด๋ฆ extends ๋ถ๋ชจํด๋์ค์ด๋ฆ implements ์ธํฐํ์ด์ค์ด๋ฆ1, ์ธํฐํ์ด์ค2....{
...
}
```
โ API(Application Programming Interface)
์์ฉ ํ๋ก๊ทธ๋จ์์ ์ฌ์ฉํ ์ ์๋๋ก ์ด์์ฒด์ ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๊ฐ ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ ์ ์ดํ ์ ์๊ฒ ๋ง๋ ์ธํฐํ์ด์ค๋ฅผ ๋ปํฉ๋๋ค.
โ ์์(final)
- ๋ณํ ์ ์๋ ์ํ๋ฅผ ๋ํ ๋ ๋๋ค.
- ๋ชจ๋ ๋๋ฌธ์๋ก ์๋๋ค ex) final PI
- ํด๋์ค, ๋ณ์, ๋ฉ์๋์์ ์ฌ์ฉํ ์ ์๋ ํค์๋
1)ํด๋์ค์์ ์ฌ์ฉ
- ์์์ ํ ์ ์๊ฒ๋จ
2)๋ณ์์์ ์ฌ์ฉ
- ๊ฐ์ ์ฌํ ๋นํ ์ ์์
3)๋ฉ์๋์์ ์ฌ์ฉ
- ์ค๋ฒ๋ผ์ด๋ฉ์ ํ ์ ์์
public interface Drawable {
int man = 100; //public statiic final int MAX =100;
//์ถ์ ๋ฉ์๋
public void drawPrint(String msg); //publiv anstract void drawPrint(String msg)
public void setColor(String color);
//์ธ์คํด์ค ๋ฉ์๋
public default void prinitInfo(){
System.out.println("Drawable ์ธํฐํ์ด์ค์ ์ธ์คํด์ค ๋ฉ์๋ prubtInfo()!");
}
//static ๋ฉ์๋
public static void staticMethod(){
System.out.println("Drawable ์ธํฐํ์ด์ค์ static ๋ฉ์๋ prubtInfo()!");
}
}
public interface Movable {
public void move(int x, int y);
public default void printMove(){
System.out.println("Movable ์ธํฐํ์ด์ค์ printMove()");
}
}
public class Circle implements Drawable{
double radius = 10;
@Override
public void drawPrint(String msg) {
System.out.println("๋ฉ์ธ์ง: " + msg);
}
@Override
public void setColor(String color) {
System.out.println(color + "์์์ผ๋ก ์ค์ ํจ");
}
public double calcCircle(double radius){
return radius * radius * Math.PI;
}
}
public class Rectangle implements Drawable{
@Override
public void drawPrint(String msg) {
System.out.println(msg + "ํํ๋ก ์ง์ฌ๊ฐํ์ ๊ทธ๋ฆฝ๋๋ค.");
}
@Override
public void setColor(String color) {
System.out.println("์ง์ฌ๊ฐํ์ ์ ์ "+ color + "์์์ผ๋ก ๊ทธ๋ฆฝ๋๋ค");
}
public int calcRectangle(int x, int y){
return x * y;
}
}
public class Printer implements Movable{
String tools = "ํ๋ฆฐํฐ";
@Override
public void move(int x, int y) {
System.out.println(tools + "๋ก x:" + x + ", y:"+ y + "์ขํ ๊ทธ๋ฆผ์ ๊ทธ๋ฆฝ๋๋ค");
}
public void test(){
System.out.println(tools + "๋ก ํ
์คํธ ์ถ๋ ฅํฉ๋๋ค");
}
}
public class Shape implements Drawable, Movable{
@Override
public void drawPrint(String msg) {
System.out.println("๋ฉ์ธ์ง: "+ msg);
System.out.println("Drawable์ drawPrint() ํธ์ถ");
}
@Override
public void setColor(String color) {
System.out.println("์์: "+ color);
System.out.println("Drawable์ setColor() ํธ์ถ");
}
@Override
public void move(int x, int y) {
System.out.println("x: "+ x +", y: "+ y);
System.out.println("Movable์ move() ํธ์ถ");
}
}
public class Interface1 {
public static void main(String[] args) {
//๊ทธ๋ฆผ ๊ทธ๋ ค์ฃผ๋ ํ๋ก๊ทธ๋จ
// Drawable drawable = new Drawable; //์ธํฐํ์ด์ค๋ ํด๋์ค๊ฐ ์๋๋ผ์ ๊ฐ์ฒด๋ฅผ ๋ชป๋ง๋ ๋ค.
Circle circle = new Circle();
circle.drawPrint("์์ ๋์ด๋ฅผ ๊ตฌํด๋ผ");
circle.setColor("๋
ธ๋");
circle.prinitInfo();
Drawable.staticMethod(); //static๋ฉ์๋๋ ์ธํฐํ์ด์ค์ ์ด๋ฆ์ผ๋ก ์ ๊ทผํ์ฌ ์ฌ์ฉํ๋ค.
Rectangle rectangle = new Rectangle();
rectangle.drawPrint("4 * 7");
rectangle.setColor("ํ๋");
System.out.println(rectangle.calcRectangle(4,7));
Printer ptinter = new Printer();
ptinter.move(10,5);
ptinter.test();
//๋ค์ค ์ธํฐํ์ด์ค ๊ตฌํ
Shape shape = new Shape();
shape.drawPrint("shape ๋ฉ์ธ์ง");
shape.setColor("ํํฌ");
shape.move(2,6);
//-------------------------------------
Drawable drawable = new Circle();
drawable.drawPrint("drawable");
drawable.setColor("๊ฒ์ ");
//drawable.calCircle ==>์๋จ ์
์บ์คํ
์ด๋ ๊ฐ์์๋ฆฌ
//------------------------------------
Drawable shape2 = new Shape(); //Drawable ํ์ ๊ฐ์ ธ์ด
shape2.drawPrint("drawable");
shape2.setColor("๋
น์");
//shape2.move //Movable ์ธํฐํ์ด์ค๊บผ๋ผ์ ๋ชป์
Movable shape3 = new Shape(); //Movable ํ์ ๊ฐ์ ธ์ด
shape3.move(2,6);
//shape3.drawPrint("drawable"); //Drawable ํ์
์์๋ง ์ฌ์ฉ๊ฐ๋ฅ
}
}
์ถ์ํด๋์ค๋ ์์์ ๋ฐ์์ ๊ธฐ๋ฅ์ ํ์ฅํ๋ ๊ฒ์ด๊ณ , ์ธํฐํ์ด์ค๋ ์ค๊ณ๋์ฒ๋ผ ์ผ์ ํ ๊ท์น์ ๊ฐ์ง ํด๋์ค๋ฅผ ๋ง๋ค๋ ค๊ณ ํ๋ ๊ฒ ์ ๋๋ค.
์ธํฐํ์ด์ค๋ ํด๋์ค๊ฐ ์๋๋ค.
์์๊ณผ ๋ฌ๋ฆฌ ์ธํฐํ์ด์ค๋ ํ๋์ ํด๋์ค๊ฐ ๋ ์ด์์ ์ธํฐํ์ด์ค๋ฅผ ๋์์ ๊ตฌํํฉ๋๋ค.