import
같은 패키지 파일
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| // Friend.java
package newpkg;
public class Friend {
void print() {
System.out.println("Friend Class");
}
}
// Main.java
package newpkg;
public class Main {
public static void main(String[] args) {
Friend f = new Friend();
f.print();
}
}
// 결과
Friend Class
|
다른 패키지 파일
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
| // bookpkg의 Book.java
package bookpkg;
public class Book {
private int code;
private String title;
private int price;
public Book(){}
public Book(int code, String title, int price){
this.code = code;
this.title = title;
this.price = price;
}
public int getCode() { return code; }
public String getTitle(){ return title; }
public int getPrice(){ return price; }
public void printBookInfo(){
System.out.println("code : " + code);
System.out.println("title : " + title);
System.out.println("price : " + price);
}
}
// usebookpkg의 UseBook.java
package usebookpkg;
import bookpkg.Book; // 다른 패키지 임포트
public class UseBook {
public static void main(String[] args){
Book bk = new Book(101, "java", 20000);
bk.printBookInfo();
}
}
// 결과
code : 101
title : java
price : 20000
|
추상클래스
추상클래스 / 추상메소드
추상클래스는 자식을 위해 존재하는 클래스이며, 인스턴스를 만들 수 없다..
추상클래스는 abstact
키워드를 통해 만들 수 있다.
추상메소드는 다형성을 이용하기 위해 부모 클래스에 메소드 이름은 존재하지만 메소드 내용은 필요하지 않은 경우 추상메소드로 선언할 수 있다.
추상메소드는 헤더 부분만 있고 메소드 내용이 없으므로, {} 없이 세미콜론(;)으로 끝낸다.
1
2
3
4
5
6
7
| public abstract class Person { // 추상클래스
public void show(){
System.out.println('parent show');
}
public abstract void print(); // 추상메소드 {} 부분 없이 세미콜론(;)으로 끝냄.
}
|
추상클래스의 특징
- 일반 클래스처럼 멤버변수, 생성자, 일반 메소드 모두 가질 수 있다.
- 추상 클래스는 객체를 만들 수 없는 클래스이다.
- 추상 클래스의 메소드는 일반 메소드일 수도 있고 추상 메소드일 수도 있다.
- 추상 클래스 내의 메소드는 추상 메소드로 구현하는 것이 바람직하다.
- 추상 메소드는 선언만 있고 구현 내용은 없다.
- 추상 메소드를 갖는 클래스는 반드시 추상 클래스가 되어야한다.
- 추상 메소드는 반드시 자식 클래스에서 오버라이딩 해야한다.
그렇지 않으면 자식 클래스도 추상 클래스가 되어야 한다. - 추상 클래스는 자식 클래스를 만들어 다형성을 이용하도록 한다.
- 추상 메소드에는
private
접근 제어자를 붙이면 자식 클래스에서 접근할 수 없으므로 private
접근 제어자를 사용하지 않는다. - 추상 메소드를 갖지 않아도 추상 클래스로 정의할 수 있다.
- 추상 클래스는 다중 상속을 지원하지 않는다.
인터페이스
인터페이스는 추상 클래스의 일종으로 추상 클래스보다 더 추상화 되어 있다.
interface
키워드를 이용하여 만들 수 있다.
상수, 추상 메소드, 디폴트 메소드, 정적 메소드, 프라이빗 메소드만을 갖는다.
인터페이스 내의 상수는 항상 public static final
로 간주된다.
인터페이스 내의 메소드 앞에 아무것도 없을시 메소드는 public abstract
로 간주된다.
1
2
3
4
5
6
7
| interface 인터페이스명 {
public static final 자료형 상수명 = 값; // 상수
public abstract 반환자료형 메소드명(매개변수 리스트); // 추상 메소드
default 반환자료형 메소드명(매개변수 리스트){ 함수내용 } // 디폴트 메소드
static 반환자료형 메소드명(매개변수 리스트){ return } // 정적 메소드
private 반환자료형 메소드명(매개변수 리스트){ 함수내용 } // 프라이빗 메소드
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| // 아래의 상수는 모두 같다. ( public static final )
public static final int numberOfPerson = 100;
int numberOfPerson = 100;
public int numberOfPerson = 100;
static int numberOfPerson = 100;
final int numberOfPerson = 100;
public static int numberOfPerson = 100;
// 아래의 메소드는 모두 같다. ( public abstract )
public abstract void print();
void print();
public void print();
abstract void print();
|
상속
추상클래스와 다르게 다중 상속이 가능하다.
다중상속을 하였을 때, extends
클래스를 먼저 작성 후, implements
를 작성해야한다.
인터페이스가 인터페이스를 상속하게 되면, extends
를 사용한다.
1
2
3
4
5
| class B extends A { ... } // class B가 class A를 상속
class D implements C { ... } // class D가 interface C를 상속
class G extends E implements F { ... } // class G가 class E와 interface F를 상속
class K extends H implements I, J { ... } // class K가 class H와 interface I, J를 상속
interface L extends I { ... } // interface L이 interface I를 상속
|
default 메소드
Java 8이후에 추가된 기능이다. 기존의 인터페이스 메소드는 상속받는 모든 하위 클래스에 메소드를 오버라이딩 해야했는데, 디폴트 메소드는 구현이 되어있기 때문에 하위 클래스에서 오버라이딩 하지 않아도 된다.
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
| interface A {
int COUNT = 100;
void test();
default void show() {
System.out.println("show");
}
}
class B implements A {
public void test() { // test()는 추상메소드이므로 오버라이딩한다.
System.out.println("test");
}
}
public class Main {
public static void main(String[] args){
Test t = new Test();
t.test();
t.show();
}
}
// 결과
test
show
|
static 메소드
class의 static 메소드와 같이 인터페이스 static 메소드는 클래스.클래스 메소드명으로 접근할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| interface A {
int get();
static int getData(){
return 100;
}
}
public class Main {
public static void main(String[] args){
System.out.println("A.getData() : " + A.getData());
}
}
// 결과
A.getData() : 100
|
private 메소드
같은 인터페이스 내에 있는 디폴트 메소드나 다른 프라이빗 메소드에 의해서만 호출 가능하다.
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
| interface A {
default int getData() {
print();
return get() + 100;
}
private void print() {
System.out.println(get());
}
private int get() {
return 100;
}
}
class B implements A {
void test() {
int x = getData();
System.out.println("x : " + x);
}
}
public class Main {
public static void main(String[] args){
Test t = new Test();
t.test();
}
}
// 결과
100
x : 200
|