목차는 다음과 같다.
1. abstract class
2. interface
3. generic
클래스에 구현부가 없는 메서드가 있으므로 객체를 생성할 수 없음.
하지만 상위 클래스 타입으로써 자식을 참조할 수는 있다.
// Vehicle v = new Vehicle(); // abstract 클래스는 객체 생성 X
Vehicle v = new DiselSUV(); // 자식을 참조하는 것은 문제 X
형태
모든 멤버변수는 public static final
, 생략가능
모든 메서드는 public abstract
, 생략 가능
public interface Fly {
void howToFly(); //어떻게 나는지
void howFastFly(); // 얼마나 빨리 나는지
}
public class Airplane extends Transportation implements Fly{
@Override
public void howToFly() {
System.out.println("By Engine!");
}
@Override
public void howFastFly() {
System.out.println("500km/h");
}
}
abstract
메서드 사용interface DefautMethodInterface{
void abstractMethod();
default void defaultMethod(){
sysout(" 기본 메서드");
}
}
클래스와 인터페이스, 메소드를 정의할 때 type을 파라미터로 사용할 수 있도록 함
List list = new ArrayList();
list.add("hello");
String str = (String) list.get(0); // type 변환 필요
List<String> list = new ArrayList<String>();
list.add("hello");
String str = list.get(0); //type 변환하지 않음
예시
public class Box<T>{
private T t;
public T get() {return t;}
public void set(T t) { this.x = t;}
}
public class BoxTest{
public static void main(String[] args){
Box<String> box1 = new Box<String>();
box1.set("Hello");
String str = box1.get();
Box<Integer> box2 = new Box<Integer>();
box2.set(6);
String str = box2.get();
}
}