Code&Data Insights

[ Java Programming ] Interface 본문

Computer Science/Java Programming

[ Java Programming ] Interface

paka_corn 2022. 4. 17. 11:32

[ Interface ] 

:  보안을 위해 interface를 사용한다. 

( 특정한 디테일을 숨기고, object의 중요한 디테일만을 보여준다 ) 

 

- 인터페이스는 인터페이스로만 상속받을 수 있으며, 클래스와 달리 다중상속을 받는 것이 가능하다

- 추상( abstract) 클래스와 같이 인터페이스는 objects를 생성하는데 쓰일 수 없다. 

- interface 메소드는 body가 존재하지 X, body는 implement class에서 작성되어진다. 

- On implementation of an interface, you must override all of its methods

- Interface methods are by default, abstract and public 

- Interface attributes are by default public, static and final

- An interface cannot contain a constructor ( as it cannot be used to create objects )

 

 

< Comparable Interface > 

- the Comparable interface is in java.lang package so is automatically available to any program.

 

- only one method must be implemented

  : public int compareTo(Object other);

-> The method compareTo must be return

      - A negative number if the calling object "comes before" the parameter other

      - A zero if the calling object "equals" the parameter other 

      - A positive number if the calling object "comes after" the parameter other

(The relationship "comes after" is just the reverse of "comes before"

 

 

- If the parameter other is not of the same type as the class being defined, then a ClassCastException should be thrown. 

 

(ex) Sorting Arrays of Comparable

Comments