Java

Abstract vs Interface

고인돌개발자 2021. 4. 24. 23:00

학습목표 : Abstract vs Interface 에 대한 관계를 이해한다.

 

궁금했다.  왜 Interface 는 abstract 를 표기하지 않을까?? 혹시...  

 

1. Abstract (추상화)

   * 특징  
      - 클래스 (abstract class A{})
                   a) (  = Interface ) 추상 클래스는 객체로 생성하여 사용이 불가하다. 
                   b) ( != Interface ) 추상 클래스는 다른 클래스에서 상속(extends) 받아서만 사용 가능하다.
                   c) (  = Interface ) 추상 클래스 의 추상 메서드는 선언만 가능하다.
                                          (구현 불가 - 구현은 상속된 클래스에서만 사용)

                   d) ( != Interface ) 추상 클래스는 일반 메서드의 선언과 구현이 가능하다. (Interface 와 다른 점)

                   e) ( != Interface ) 추상 클래스는 상속하여 사용하기 때문에 당연히 다중상속은 불가하다.

  

      - 메소드 ( public abstract int getB();)

                  a) ( != Interface ) 추상 메서드는 반드시 abstract 표기를 한다.

                  b) (  = Interface ) 추상 메서드는 상속된 클래스에서만 구현(@Override)으로 사용이 가능하다.

                  c) ( != Interface ) 추상 메서드가 있는 클래스는 반드시 추상형으로 표기하여 클래스를 생성하여야 한다.

                   * 실제 메서드가 추상형으로 선언되었기 때문에 클래스가 추상형으로 선언 되어야만 한다.

 

예제)

package abstrtact.lifecoding;

public class AbstractMain {

	public static void main(String[] args) {
		System.out.println("========= AbstractMain ==============");
		
		
		NormalC normalc = new NormalC();
		normalc.normalM();
		System.out.println(normalc.abstractM());		
	}
}


 abstract class AbstractC{
	
	public abstract int abstractM(); // 구현되지 않은 정의만 됨.
	
	//public abstract int c() { // Abstract methods do not specify a body
	public  int getWhat() { // Abstract methods do not specify a body
		int a=10;
		return a;
	}
	
	
	public void normalM() {
		System.out.println("A-d function");
	}		
}

class NormalC extends AbstractC{

	@Override
	public int abstractM() { // abstract 는 반드시 구현해야 함. 	
		return 10;
	}	
}

1. Interface (엄격한 추상화)

   * 특징  
      - 클래스 (interface class A{})
                   a) (  = abstract ) Interface 클래스는 객체로 생성하여 사용이 불가하다. 
                   b) (  = abstract ) interface 클래스는 다른 클래스에서 상속(implements) 받아서만 사용 가능하다.
                   c) (  = abstract ) interface 클래스 의 추상 메소드는 선언만 가능하다.
                                         (구현 불가 - 구현은 상속된 클래스에서만 사용)

                   d) ( != abstract ) interface 클래스는 public 정적 상수 와 public 추상 메서드만 가질 수 있다.

                   e) ( != abstract ) interface 클래스는 다중상속이 가능하다.

  

     - 메소드 (위 궁금증에 대한 대답)

                  a) interface 클래스의 메서드는 abstract 를 명시하지 않아도 abstract 로 인식한다.
                    'int age' = 10;  is same this  'public static int age = 10; '

     - 변수 (아.. 이건 몰랐네)

                 a)  interface 클래스의 변수는 static 을 명시하지 않아도 static 으로 인식한다.

 

예제) 

package book.oopforsprings.lec04._interface;

public class InterfaceMain {

	public static void main(String[] args) {
		
		// Static area 
		System.out.printf("Your age is %s \n", Normal.age);
		System.out.printf("Your age is %s \n", Normal2.age2);
		
		Normal normal = new UseInterface();
			   normal.doHi();
		
		Normal2 normal2 = new UseInterface();
		        normal2.doHi2();
		
	}

}


// Normal interfacle class
interface Normal {
	
	int age = 10; // this is same public static int age = 10;
	
	void doHi(); // this is same public abstract void doHi();
	
}

//Mark static and abstracts 
interface Normal2 {
	
	public static int age2 = 20;
	
	public abstract void doHi2();
	
}

class UseInterface implements Normal, Normal2{

	@Override
	public void doHi() {
		System.out.println("HI ~~");		
	}

	@Override
	public void doHi2() {
		System.out.println("HI 2 ~~");	
		
	}
	
}