<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)부모를 이용하여 객체 생성
-다른 부모에 있는 메소드는 불러올 수 없다
-내 클래스에서만 만든 메소드도 불러올 수 없다
'JAVA > 14_interface' 카테고리의 다른 글
<<2.여러 클래스로 나누어 작성>> + 객체 생성 주의사항 (0) | 2021.02.03 |
---|---|
<<1.하나의 페이지에서의 interface구현>> (0) | 2021.02.03 |