interface

  • 클래스 아님 (객체 생성 안됨)
  • implements
  • 추상메소드, 상수만 가질 수 있음 (abstract, final 생략 가능)
  • 다중 구현

cf) 자바는 단일 상속만 지원

    ->다중 상속은 interface를 가지고 implements(구현) 한다

 

  • abstract, final만 갖는다. 
  • ex) int x = 90; -> static final 생략됨
  • public void show(); -> abstract 생략 가능 -> 무조건 추상메소드임
  • public void view() {     } -> 일반 메소드는 생성 안됨
  • interface D extends B -> interface간 상속에서도 extends 키워드 사용
  • class Multi extends Shape implements B,A  -> interface 다중 상속 가능
  • 단)))) class Multi implements B,A extends Shape  { ->이렇게 쓰면 안됨!! - class명(Multi)바로 옆에는 class인 Shape가 와야함!!

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package ex02.Interface;
 
/* interface
 -클래스 아님(객체 생성 안됨)
 -implements
 -추상메소드, 상수만 가질 수 있다.(abstract, final 생략 가능)
 -다중 구현 
 cf)자바는 단일 상속만 지원
    ->다중 상속은 interface를 가지고 implements(구현) 한다 
 */
 
interface A { //interface - abstract method, final field만 갖는다 
    int x = 90// static final 생략됨
    final int y = 777;
    static final char ch = 'P';
    
    public void show(); //abstract 생략가능함 - 무조건 추상메소드임
    public abstract void disp();
//    public void view() {   } -> 일반 메소드는 생성 안됨
}// A interface end
 
interface B {
    void view(); //abstract 생략됨
// B interface end
 
//<<<<<<<,=>interface 간 상속에서도 extends 키워드 사용한다>>>>>>
interface D extends B {
    void dview();
// D interface end 
 
 
 
class Rect implements D {
 
    @Override
    public void view() { // B interface에 있는 메소드 
        // TODO Auto-generated method stub
        System.out.println("B interface에 있는 메소드 ");
        
    }
 
    @Override
    public void dview() { // D interface에 있는 메소드 
        // TODO Auto-generated method stub
        System.out.println("D interface에 있는 메소드 ");
    }
    
    public int plus(int x, int y) {
        return x+y;
    }
    
// Rect class end
 
class Shape {
    
//Shape class end 
 
//class Multi implements B,A extends Shape  { <<<<<->이렇게 쓰면 안됨!! - class명(Multi)바로 옆에는 class인 Shape가 와야함!!>>>>
class Multi extends Shape implements B,A { //->interface 다중 상속 가능 
 
    @Override
    public void show() {
        // TODO Auto-generated method stub
        
    }
 
    @Override
    public void disp() {
        // TODO Auto-generated method stub
        
    }
 
    @Override
    public void view() {
        // TODO Auto-generated method stub
        
    } 
    
// Multi class end 
 
 
 
public class MainEntry {
    public static void main(String[] args) {
        
        //2)D와 B를 가지고 생성가능(상속관계)
        D d = new Rect();
        B b = new Rect();
//        d.plus();는 안됨 -> 상속받은 상위 interface를 이용하여 객체를 생성했기 떄문 
        
        //1)자기자신으로 객체 생성
        Rect r = new Rect(); 
        r.plus(56);
        
//        A a = new A(); -> A는 클래스가 아니기 때문에 객체 생성 X
 
    }
}
 
cs

+ Recent posts