728x90
์์ (Inheritance)
1. 5000์ค์ง๋ฆฌ A๋ผ๋ ์ฝ๋๊ฐ ์๋ค.
2. ๋๋ A๋ผ๋ ์ฝ๋์ 3์ค์ ๋ ๋ ์ถ๊ฐ/๋ณ๊ฒฝํด์ B๋ผ๋ ์ฝ๋๋ฅผ ์ถ๊ฐํด์ผํ๋๋ฐ....
3. ๋๊ฐ์ 5000์ค์ง๋ฆฌ ์ฝ๋๋ฅผ ๋ ๋ณต๋ถํด์ผํ๋ ๋๋ฌด ๊ธธ๊ณ ๋ญ๋น๋ค.
4. ์์์ ์ฌ์ฉํ๋ฉด 5000์ค ์ง๋ฆฌ ์ค๋ณต๋๋ ์ฝ๋๋ฅผ ๋ณต๋ถ์ ์ํด๋ ๋๊ณ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์๊ฒ ํ๋ค.
ํ์ค๋ก ์ ๋ฆฌํ์๋ฉด ๋ฉ์ธ๊ฐ์ฒด์ ํ๋(๋ณ์)์ ๋ฉ์๋๋ฅผ ๋ค๋ฅธ ๊ฐ์ฒด๋ ์ฌ์ฉํ ์ ์๊ฒ ๋ง๋ค์ด์ฃผ๋๊ฒ์ด ์์์ด๋ค.
์ฝ๋์ค๋ณต์ ๋ง์์์์ผ๋ฉฐ ํ์ฅ์ฑ์ด ์ข์์ง๊ฒ ๋๋ค.
์ฐ์ต ์๋ฌธ
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
|
package main;
import cls.ChildClass;
import cls.ParentClass;
public class MainClass {
public static void main(String[] args) {
ChildClass cc =new ChildClass();
cc.method();
ParentClass pc =new ChildClass();
pc.method();
ChildClass c = (ChildClass)pc;
c.func();
}
}
|
cs |
๋ถ๋ชจํด๋์ค ParentClass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package cls;
public class ParentClass {
private String name; // ๋ฉค๋ฒ๋ณ์๋ ๊ฑฐ์ private
protected int number;
public ParentClass() {
System.out.println("ParentClass ParentClass()");
}
public void method() {// ๋ฉ์๋๋ public์ผ๋ก ํด๋๊ธฐ
System.out.println("ParentClass method");
}
}
|
cs |
์์ํด๋์ค ChildClass
extends๋ฅผ ํ์ฉํด ๋ถ๋ชจํด๋์ค์ ํ๋(๊ฐ์ฒด)์ ๋ฉ์๋๋ฅผ ๋ฐ์์จ๋ค.
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
|
package cls;
public class ChildClass extends ParentClass {
public ChildClass(){
System.out.println("ChildClass ChildClass ()");
}
public void func() {
number = 12;
//super.method();
//this.method();
//๋ถ๋ชจ๋ ์์ํจ์ ๋๋ค ํ๋ ค๋ฉด ์ด๋ ๊ฒ ํธ์ถํ ์ ์๋ค.
}
//over ride ->์์๋ฐ์ ํด๋์ค์์ ๋ฉ์๋๋ฅผ ๊ณ ์ณ ๊ธฐ์
ํ๋๊ฒ
public void method() {// ๋ฉ์๋๋ public์ผ๋ก ํด๋๊ธฐ
super.method();//super๋ฅผ ํตํด์ ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค.
//System.out.println("Child method ๊ณ ์ณค๋ค");
}
}
|
cs |
'๐ ์๋ฐ Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
HashMap๊ณผ TreeMap (0) | 2021.02.03 |
---|---|
LinkedList ๊ฐ๋จ ์ ๋ฆฌ & ์์ฝ (0) | 2021.02.03 |
์๋ฐ(JAVA)- ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋จ์ด๋? (0) | 2021.02.01 |
์๋ฐ(JAVA) - ์ ๊ทผ ์ง์ ์ (ํน์ ์ ๊ทผ ์ ์ด์) (0) | 2021.01.27 |
์๋ฐ(JAVA) - this ์ ์๋ฏธ์ ์ฌ์ฉ๋ฒ (0) | 2021.01.27 |
๋๊ธ