<Test.java - interface>

1
2
3
4
5
6
7
8
9
package ex04.multiImplements;
 
public interface Test { //인터페이스는 implements 안됨 / 다른 인터페이스는 상속 가능 
    String str = "여러 패키지에 있는 것들을 상속 받습니다.";
    
    public void testView();
    
}
 
cs

-인터페이스는 implements 안됨

-다른 인터페이스는 상속 가능 (extends)


<MultiClass.java>

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
package ex04.multiImplements;
 
import ex01.Abstract.Shape;
import ex03.Interface.IDraw;
 
//<<<<<다중 상속 구현>>>>>-class는 하나만, interface는 여러개 가능 
public class MultiClass extends Shape implements IDraw, Test {
 
    @Override
    public void testView() { // Test interface
        System.out.println("Test interface");
 
    }
 
    @Override
    public void draw() { // IDraw interface
        result = 1.1;
        System.out.println("IDraw interface" + result );
        
    }
 
    @Override
    public double calc(double x) { // Shape abstract class
        System.out.println("Shape abstract class - calc method");
        return 5.5;
    }
 
    @Override
    public void show(String name) { // Shape abstract class
        result = 9.9;
        System.out.println("Shape abstract class - show method" + name);
    }
 
    public void myClass() {
        System.out.println("이건 내가만든 클래스입니다~!");
    }
}
 
cs

-다중상속 구현 클래스

 ->class는 하나만, interface는 여러개 가능


<Main.java>

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
package ex04.multiImplements;
 
import ex01.Abstract.Shape;
 
public class MainEntry {
    public static void main(String[] args) {
 
        Test t = new MultiClass();
        t.testView();
        System.out.println(t.str);
        
        System.out.println("\n\n");
        
        //2)부모를 이용하여 객체 생성 - 다른 부모에있는 메소드는 불러올 수 x
        Shape s = new MultiClass();
        s.view(); //->Shape안에있는 method만 불러올 수 있다, 다른 부모안에있는 메소드는 불러올수 x
        s.show("seoyeeee");
        
        System.out.println("\n\n");
        
        //1)자기 자신으로 객체 생성 
        //->내가 가지고있는 method포함, 모든 부모가 가지고 있는 method사용가능
        MultiClass mc = new MultiClass();
        mc.draw();
        mc.show("yeinnnn");
        mc.testView();
        mc.view();
    }
 
}
 
cs

Test interface
여러 패키지에 있는 것들을 상속 받습니다.



Super class Shape
Shape abstract class - show methodseoyeeee



IDraw interface1.1
Shape abstract class - show methodyeinnnn
Test interface
Super class Shape


(객체 생성 방법)

1)자기 자신으로 객체 생성 

   -내가 가지고있는 method포함, 모든 부모가 가지고 있는 method 사용가능 

 

2)부모를 이용하여 객체 생성

   -다른 부모에 있는 메소드는 불러올 수 없다

   -내 클래스에서만 만든 메소드도 불러올 수 없다 

<IDraw.java - interface>

1
2
3
4
5
6
7
8
9
10
package ex03.Interface;
 
//interface - 고정된 수인 상수와 추상메소드만 가질 수 있다!!! 
public interface IDraw { 
    int su = 1000//static final
    
    public void draw(); // abstract method
 
}
 
cs

<BB.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package ex03.Interface;
 
public class BB implements IDraw { //BB class
 
    char ch = 'K';
    String th = "th78";
    
    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println("BB 클래스에서 인터페이스 IDraw의 메소드 오버라이드 했습니다.");
 
    }
    
    public int add(int x, int y) {
        return x+y;
    }
 
}
 
cs

<Main.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package ex03.Interface;
 
public class MainEntry {
    public static void main(String[] args) {
        //방법1) 자기 자신으로 객체 생성
        BB b = new BB();
        b.draw();
        System.out.println(b.su);
        
        int plus = b.add(1020);
        System.out.println(plus);
        System.out.println(b.add(3,4));
        
        
        //방법2) 부모 인터페이스로 객체 생성
        IDraw bb = new BB();
        bb.draw();
        System.out.println(bb.su);
//        bb.add(1,2); -> 부모 인터페이스로 객체를 생성했기 때문에, 
//        자식클래스에서 생성한 add()에는 접근 x
    }
 
}
 
cs

30
7
BB 클래스에서 인터페이스 IDraw의 메소드 오버라이드 했습니다.
1000


(객체 생성 방법 및 주의할점)

방법1) 자기 자신으로 객체 생성
        BB b = new BB();
        b.draw();

 

방법2) 부모 인터페이스로 객체 생성

        IDraw bb = new BB();

        bb.draw();

        System.out.println(bb.su);

 

        bb.add(1,2); -> 부모 인터페이스로 객체를 생성했기 때문에, 자식클래스에서 생성한 add()에는 접근 x

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