์๋
! ์ด์ ์์
์์ ์ฐ๋ฆฌ๋ ์ด๋ฏธ ์์์ ๊ฐ๋
์ ๋ํด ๊ฐ๋ตํ๊ฒ ์๊ฒ ๋์์ต๋๋ค. ์ค๋ ์ฐ๋ฆฌ๋ ์ด ์ฃผ์ ๋ฅผ ๋ค์ ๋ค๋ฃฐ ๊ฒ์
๋๋ค. ํ์ง๋ง ๋๋ฌด ๊น์ง๋ ์์ต๋๋ค. ์์ผ๋ก๋ ์ด์ ๋ํ ์์ธํ ๊ฐ์๋ฅผ ํ๋๋ก ํ๊ฒ ์ต๋๋ค. ์ค๋ ์ฐ๋ฆฌ๋ ๋ช ๊ฐ์ง ์ค์ฉ์ ์ธ ์๋ฅผ ๊ฐ๋จํ ์ดํด๋ณด๊ณ Java์ ํฅ๋ฏธ๋ก์ด ์ฐ์ฐ์๋ฅผ ์๊ฒ ๋ ๊ฒ์
๋๋ค.
๊ณ์น
๊ทธ๋ ๋ค๋ฉด ์์์ด๋ ๋ฌด์์ ๋๊น? ์์์ ๊ธฐ์กด ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ ํด๋์ค๋ฅผ ์ ์ธํ ์ ์๋ ํ๋ก๊ทธ๋๋ฐ ๋ฉ์ปค๋์ฆ(Java ํฌํจ)์ ๋๋ค. ๊ทธ๋ฐ ๋ค์ ํ์ ํด๋์ค๋ ๋ถ๋ชจ ํด๋์ค์ ํ๋ ๋ฐ ๋ฉ์๋์ ๋ํ ์ก์ธ์ค ๊ถํ์ ์ป์ต๋๋ค. ์ ์ด๊ฒ์ด ํ์ํ ๊น์? ์, ํ๋ก๊ทธ๋จ์์ ํธ๋ญ, RaceCar, Sedan, Pickup ๋ฑ ์ฌ๋ฌ ์๋์ฐจ ํด๋์ค๋ฅผ ์์ฑํด์ผ ํ๋ค๊ณ ์์ํด ๋ณด์ญ์์ค. ์ฝ๋๋ฅผ ์์ฑํ๊ธฐ ์ ์๋ ์ด ๋ชจ๋ ํด๋์ค๊ฐ ๊ณตํต์ ์ด ๋ง๋ค๋ ๊ฒ์ ํ์คํ ์ ์ ์์ต๋๋ค. ๋ชจ๋ ์๋์ฐจ์๋ ๋ชจ๋ธ์ด ์์ต๋๋ค. ์ด๋ฆ, ์ ์กฐ ์ฐ๋, ์์ง ํฌ๊ธฐ, ์ต๋ ์๋ ๋ฑ (๋ชจ๋ ๋ฐํด์ ๊ธฐํ ๋ถํ์ด ๊ณตํต์ ์ด๋ผ๋ ์ฌ์ค์ ๋งํ ๊ฒ๋ ์์ต๋๋ค). ์ด ์ํฉ์์ ๋ค์์ ์ํํ ์ ์์ต๋๋ค.- ๊ฐ ํด๋์ค์์ ์ด๋ฌํ ํ๋๋ฅผ ๋ง๋ญ๋๋ค(์ ์๋์ฐจ ํด๋์ค๋ฅผ ๋ง๋ค ๋ ๊ฐ๊ฐ ์ถ๊ฐ).
- ๋ชจ๋ ์๋์ฐจ์ ๊ณตํต๋๋ ํ๋๋ฅผ
Car
์์ ํด๋์ค๋ก ๊ฐ์ ธ์จ ๋ค์ extends ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ํด๋์ค์์ ํน์ ์ ํ์ ์๋์ฐจ์ ๋ํ ๋ชจ๋ ํด๋์ค๋ฅผ ํ์์ํต๋๋คCar
.
public class Car {
private String model;
private int maxSpeed;
private int yearOfManufacture;
public Car(String model, int maxSpeed, int yearOfManufacture) {
this.model = model;
this.maxSpeed = maxSpeed;
this.yearOfManufacture = yearOfManufacture;
}
}
public class Truck extends Car {
public Truck(String model, int maxSpeed, int yearOfManufacture) {
super(model, maxSpeed, yearOfManufacture);
}
}
public class Sedan extends Car {
public Sedan(String model, int maxSpeed, int yearOfManufacture) {
super(model, maxSpeed, yearOfManufacture);
}
}
์ต์ํ ์ฐ๋ฆฌ๋ ๋ถํ์ํ ์ฝ๋ ์ค๋ณต์ ํผํฉ๋๋ค(๊ทธ๋ฆฌ๊ณ ํ๋ก๊ทธ๋จ์ ์์ฑํ ๋ ํญ์ ๊ทธ๋ ๊ฒ ํ๋ ค๊ณ ๋
ธ๋ ฅํด์ผ ํฉ๋๋ค). ๋ํ ๋ชจ๋ ์๋์ฐจ์ ๊ณตํต๋ ๋ชจ๋ ํ๋๊ฐ ํ๋์ ํด๋์ค๋ก ํตํฉ๋ ๊ฐ๋จํ๊ณ ์ดํดํ๊ธฐ ์ฌ์ด ํด๋์ค ๊ตฌ์กฐ๊ฐ ์์ต๋๋ค. ํธ๋ญ์ ๋ค๋ฅธ ์๋์ฐจ์๋ ์๋ ํน์ ํ๋๊ฐ ์๋ ๊ฒฝ์ฐ ํด๋์ค์์ ์ ์ธํ ์ ์์ต๋๋ค Truck
. ๋ฐฉ๋ฒ๋ ๋ง์ฐฌ๊ฐ์ง์
๋๋ค. ๋ชจ๋ ์๋์ฐจ์๋ ๋ฉ์๋๋ก ์ค๋ช
ํ ์ ์๋ ํน์ ๊ณตํต ๋์์ด ์์ต๋๋ค(์: ์๋์ฐจ ์๋, ๊ฐ์/์ ๋ ๋ฑ). ์ด๋ฌํ ๊ณตํต ๋ฉ์๋๋ ์์ ํด๋์ค๋ก ํตํฉ๋ ์ ์์ผ๋ฉฐ ๊ฐ Car
ํน์ ์ ํ์ ์๋์ฐจ๋ ํ์ ํด๋์ค์์ ๊ณ ์ ํ ์์
์ ์ ์ํ ์ ์์ต๋๋ค. .
public class Car {
public void gas() {
// Accelerate
}
public void brake() {
// Brake
}
}
public class F1Car extends Car {
public void pitStop() {
// Only race cars make pit stops
}
public static void main(String[] args) {
F1Car formula1Car = new F1Car();
formula1Car.gas();
formula1Car.pitStop();
formula1Car.brake();
}
}
ํด๋์ค ์ ๋ชจ๋ ์๋์ฐจ ๊ณตํต ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ต๋๋ค Car
. F1Car
๊ทธ๋ฌ๋ "Formula 1" ๊ฒฝ์ฃผ์ฉ ์๋์ฐจ๋ฅผ ๋ํ๋ด๋ ํด๋์ค๋ฅผ ๋ณด์ญ์์ค . ํผํธ ์คํ(๊ธด๊ธ ์๋์ฐจ ์ ๋น๋ฅผ ์ํ ์ ๋ฅ์ฅ)์ ๋ ์ด์ค์์๋ง ์ํ๋๋ฏ๋ก ๊ด๋ จ ํ์ ํด๋์ค์ ์ด ํน์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ต๋๋ค.
instanceof ์ฐ์ฐ์
Java์๋ ๊ฐ์ฒด๊ฐ ํน์ ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋์๋์ง ํ์ธํ๊ธฐ ์ํ ํน์ ์ฐ์ฐ์ instanceof ๊ฐ ์์ต๋๋ค . ๊ฒ์ฌ ๊ฒฐ๊ณผ์ ๋ฐ๋ผ true ๋๋ false๋ฅผ ๋ฐํํฉ๋๋ค . ์๋์ฐจ ์์ ์ ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ป๊ฒ ์๋ํ๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค.
public class Truck extends Car {
public static void main(String[] args) {
Truck truck = new Truck();
System.out.println(truck instanceof Car);
}
}
์ถ๋ ฅ: true ๊ฐ์ฒด๊ฐ ์๊ณ ๋ชจ๋ ํธ๋ญ์ด ์๋์ฐจ์ด๋ฏ๋ก ์ฐ์ฐ์ ๋ true ๋ฅผinstanceof
๋ฐํํฉ๋๋ค . ํด๋์ค ๋ ํด๋์ค ์์ ํ์๋ฉ๋๋ค . ๋ชจ๋ ํธ๋ญ์ ๊ณตํต ์์์ธ ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋ฉ๋๋ค . ์ฐ์ฐ์๊ฐ ์ด๋ป๊ฒ ์ฌ์ฉ๋๋์ง ์์ธํ ์ดํด๋ณด์ญ์์ค . ๋ฉ์๋("ํด๋์ค์ ๊ฐ์ฒด ์ธ์คํด์ค")๊ฐ ์๋๋ผ ์ฐ์ฐ์์ด๊ธฐ ๋๋ฌธ์ ๋ง์นจํ ์์ด ์์ฑํฉ๋๋ค. ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์๋ํด ๋ณด๊ฒ ์ต๋๋ค. Truck
Truck
Car
Car
instanceof
public static void main(String[] args) {
Car car = new Car();
System.out.println(car instanceof Truck);
}
์ถ๋ ฅ: false ํด๋์ค Car
(๋ฐ car ๊ฐ์ฒด)๊ฐ ํด๋์ค์์ ํ์๋์ง ์์ต๋๋ค Truck
. ๋ชจ๋ ํธ๋ญ์ ์๋์ฐจ์ด์ง๋ง ๋ชจ๋ ์๋์ฐจ๊ฐ ํธ๋ญ์ ์๋๋๋ค. Car
๊ฐ์ฒด๋ ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ง ์์ต๋๋ค Truck
. ๋ ๋ค๋ฅธ ์:
public static void main(String[] args) {
Car car = new Car();
Truck truck = new Truck();
System.out.println(car instanceof Object && truck instanceof Object);
}
์ถ๋ ฅ: True ์ฌ๊ธฐ์ ๋
ผ๋ฆฌ๋ ๊ฐ๋จํฉ๋๋ค. ์ฌ์ฉ์๊ฐ ์์ฑํ ํด๋์ค๋ฅผ ํฌํจํ์ฌ Java์ ๋ชจ๋ ํด๋์ค๋ ํด๋์ค์์ ํ์๋ฉ๋๋ค Object
("extends Object"๋ผ๊ณ ์์ฑํ์ง ์๋๋ผ๋ ์ด๋ฏธ ์์๋์ด ์์). ์ด๊ฒ์ด ์ธ์ ์ด๋ป๊ฒ ์ ์ฉํ ๊น์? ์ฐ์ฐ์ ๋ ๋ฉ์๋๋ฅผ instanceof
์ฌ์ ์ํ ๋ ๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค equals()
. equals
์๋ฅผ ๋ค์ด, ํด๋์ค ์์ ๋ฉ์๋๊ฐ ๊ตฌํ๋๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค String
.
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
a String
๋ฅผ ์ ๋ฌ๋ ๊ฐ์ฒด์ ๋น๊ตํ๊ธฐ ์ ์ ๋ฉ์๋๋ ๊ฐ์ฒด๊ฐ ๋ฌธ์์ด์ธ์ง ์ฌ๋ถ๋ฅผ ํ
์คํธํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์์์ผ ๋ ๊ฐ์ฒด์ ์์ฑ์ ๋น๊ตํ๊ธฐ ์์ํฉ๋๋ค. ์ด ํ
์คํธ๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๊ฐ ๋ฐ ๊ธธ์ด ํ๋๊ฐ ์๋ ๊ฐ์ฒด๋ฅผ ๋ฉ์๋์ ์ ๋ฌํ๊ณ ๋ฌธ์์ด๊ณผ ๋น๊ตํ ์ ์์ต๋๋ค. ๋ฌผ๋ก ์ด๋ ์๋ชป๋ ๊ฒ์
๋๋ค.
GO TO FULL VERSION