์ค๋ฒ๋ผ์ด๋ฉ
์ค๋ฒ๋ผ์ด๋ฉ์ ์ฌ์ ์ ์๋ฏธ๋ ์ด๋ป๊ฒ ๋ ๊น?
override : ~์์ ๋ฎ์ด์ฐ๋ค(overwrite)
์กฐ์ํด๋์ค๋ก๋ถํฐ ์์๋ฐ์ ๋ฉ์๋์ ๋ด์ฉ์ ๋ณ๊ฒฝํ๋ ๊ฒ
์์๋ฐ์ ๋ฉ์๋๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๊ธฐ๋ ํ์ง๋ง, ์์ ํด๋์ค ์์ ์ ๋ง๊ฒ ๋ณ๊ฒฝํด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
๊ทธ๋ด๋ ์ค๋ฒ๋ผ์ด๋ฉ์ด ์ฐ์ธ๋ค.
2์ฐจ์ x,y ์ขํ์ ๋ํ ํด๋์ค Point์ 3์ฐจ์ x, y, z์ขํ์ ๋ํ ํด๋์ค Point3D์ ์์ ์ฝ๋๋ฅผ ๋ณด์.
class Point {
int x;
int y;
String getLocation() {
return "x :" + x + ", y :"+ y;
}
}
class Point3D extends Point { // Point ์์ ๋ฐ์
int z;
String getLocation() { // ์ค๋ฒ๋ผ์ด๋ฉ
return "x :" + x + ", y :"+ y + ", z :"+z;
}
}
ํด๋์ค Point๋ x, y ์ขํ๋ฅผ ๋ฐํํ๋๋ฐ 3D Point๋ x, y, z ์ธ ๊ฐ์ ์ ์ด ํ์ํ๊ธฐ ๋๋ฌธ์ ํด๋์ค Point์ getLocation()๋ x, y๋ง ๋ฐํํ๊ธฐ ๋๋ฌธ์ Point3D์ ๋ง๊ฒ ์ค๋ฒ๋ผ์ด๋ฉ์ ํด์คฌ๋ค.
๊ทธ๋ผ ์ค๋ฒ๋ผ์ด๋ฉ์ ์กฐ๊ฑด์ ์ด๋ป๊ฒ ๋ ๊น?
์ค๋ฒ๋ผ์ด๋ฉ์ ์กฐ๊ฑด
๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ ์กฐ๊ฑด
์์ ํด๋์ค์์ ์ค๋ฒ๋ผ์ด๋ฉํ๋ ๋ฉ์๋๋ ์กฐ์ ํด๋์ค์ ๋ฉ์๋์
- ์ด๋ฆ์ด ๊ฐ์์ผ ํ๋ค.
- ๋งค๊ฐ๋ณ์๊ฐ ๊ฐ์์ผ ํ๋ค.
- ๋ฐํํ์ ์ด ๊ฐ์์ผ ํ๋ค.
์ด๋ฆ, ๋งค๊ฐ๋ณ์, ๋ฐํํ์
์ธ๊ฐ์ง๊ฐ ๊ฐ์์ผ ํ๋ค.
์์ ์์ ์ฝ๋์์๋
์ด๋ฆ : getLocation
๋งค๊ฐ๋ณ์ : ์์
๋ฐํํ์ : String
์ผ๋ก ๋์ผํ๋ค.
์ค๋ฒ๋ผ์ด๋ฉ ์ ์ฝ
์กฐ์ ํด๋์ค์ ๋ฉ์๋๋ฅผ ์์ ํด๋์ค์์ ์ค๋ฒ๋ผ์ด๋ฉํ ๋
1. ์ ๊ทผ ์ ์ด์๋ฅผ ์กฐ์ ํด๋์ค์ ๋ฉ์๋๋ณด๋ค ์ข์ ๋ฒ์๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
2. ์์ธ๋ ์กฐ์ ํด๋์ค์ ๋ฉ์๋๋ณด๋ค ๋ง์ด ์ธ๊ธํ ์ ์๋ค.
3. ์ธ์คํด์ค๋ฉ์๋๋ฅผ static ๋ฉ์๋๋ก ๋๋ ๊ทธ ๋ฐ๋๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
3๊ฐ์ง ์ ์ฝ์ด ์๋ค.
๊ฐ๊ฐ ์์ธํ ์ดํด๋ณด์.
1๏ธโฃ ์ ๊ทผ ์ ์ด์๋ ์กฐ์ ํด๋์ค์ ๋ฉ์๋๋ณด๋ค ์ข์ ๋ฒ์๋ก ๋ณ๊ฒฝ ํ ์ ์๋ค.
class Parent {
public void method1() {
System.out.println("Parent์ method1");
}
protected void method2() {
System.out.println("Parent์ method2");
}
}
class Child extends Parent {
// public -> protected๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
// ์ปดํ์ผ ์๋ฌ ๋ฐ์
@Override
protected void method1() {
System.out.println("Child์ method1");
}
// ์ ๊ทผ ์ ์ด์๋ฅผ ๋์ ๋ฒ์๋ก ๋ณ๊ฒฝํ๋ ๊ฒ์ ํ์ฉ๋๋ค.
// protected -> public์ผ๋ก ๋ณ๊ฒฝ ๊ฐ๋ฅํฉ๋๋ค.
@Override
public void method2() {
System.out.println("Child์ method2");
}
}
์ ๊ทผ ์ ์ด์๋ ๋์ ๋ฒ์๋ก ๋ณ๊ฒฝํ๋ ๊ฒ์ ํ์ฉ๋์ง๋ง, ์ข์ ๋ฒ์๋ก๋ ๋ณ๊ฒฝํ ์ ์๋ค.
public > protected > (default)> private
public์ด ๊ฐ์ฅ ๋์ ๋ฒ์์ด๊ณ , private์ผ๋ก ๊ฐ์๋ก ์ข์ ๋ฒ์์ด๋ค.
2๏ธโฃ ์กฐ์ ํด๋์ค์ ๋ฉ์๋๋ณด๋ค ๋ง์ ์์ ์์ธ๋ฅผ ์ ์ธํ ์ ์๋ค.
class Parent {
public void doSomething() throws IOException {
// ...
}
}
class Child extends Parent {
// ์ปดํ์ผ ์๋ฌ ๋ฐ์!
// ์์ ํด๋์ค์์ ๋ ๋ง์ ์์ธ๋ฅผ ์ ์ธํ ์ ์๋ค.
public void doSomething() throws IOException, SQLException {
// ...
}
}
์กฐ์ ํด๋์ค์์ IOException๋ง ์์ธ๋ฅผ ์ ์ธํ๊ณ , ์์ ํด๋์ค์์ IOException, SQLException์ ์ ์ธํ๋ค.
์กฐ์๊ณผ ๋์ผํ ์์ธ๋ง์ ๊ฐ์ ธ์ฌ ์ ์๊ธฐ ๋๋ฌธ์ ์ ์ฝ๋์์๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
โ ๊ทผ๋ฐ SQLException๋ ์ฐ๊ณ ์ถ์๋ฐ ๊ทธ๋ผ ์ด๋ป๊ฒ ํด โ
class Child extends Parent {
public void doSomething() throws IOException {
try {
// ...
} catch (SQLException e) {
// SQLException ์ฒ๋ฆฌ
}
}
}
๊ทธ๋ด ๋๋ try-catch๋ฌธ์ ์ด์ฉํ์ฌ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ ์ ์๋ค.
3๏ธโฃ ์ธ์คํด์ค๋ฉ์๋๋ฅผ static ๋ฉ์๋๋ก ๋๋ ๊ทธ ๋ฐ๋๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
์ธ์คํด์ค ๋ฉ์๋ → static ๋ฉ์๋๋ก ๋ณ๊ฒฝํ๋ ค๊ณ ์๋ํ๋ ์์ ์ฝ๋
class Parent {
// ์ธ์คํด์ค ๋ฉ์๋
public void instanceMethod() {
System.out.println("๋ถ๋ชจ ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋");
}
}
class Child extends Parent {
// static ๋ฉ์๋๋ก ๋ณ๊ฒฝํ๋ ค๊ณ ์๋
// ์ปดํ์ผ ์๋ฌ ๋ฐ์!
public static void instanceMethod() {
System.out.println("์์ ํด๋์ค์ static ๋ฉ์๋");
}
}
instance ๋ฉ์๋๋ฅผ static ๋ฉ์๋๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
static ๋ฉ์๋→ ์ธ์คํด์ค ๋ฉ์๋๋ก ๋ณ๊ฒฝํ๋ ค๊ณ ์๋ํ๋ ์์ ์ฝ๋
class Parent {
// static ๋ฉ์๋
public static void staticMethod() {
System.out.println("๋ถ๋ชจ ํด๋์ค์ static ๋ฉ์๋");
}
}
class Child extends Parent {
// ์ธ์คํด์ค ๋ฉ์๋๋ก ๋ณ๊ฒฝํ๋ ค๊ณ ์๋
// ์ปดํ์ผ ์๋ฌ ๋ฐ์!
public void staticMethod() {
System.out.println("์์ ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋");
}
}
static ๋ฉ์๋๋ฅผ instance ๋ฉ์๋๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
import java.io.*;
class Parent {
public static void staticMethod() {
System.out.println("๋ถ๋ชจ ํด๋์ค์ static ๋ฉ์๋");
}
public void instanceMethod() {
System.out.println("๋ถ๋ชจ ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋");
}
}
class Child extends Parent {
// ์ค๋ฒ๋ผ์ด๋ฉ์ด ์๋๋ผ ์๋ก์ด static ๋ฉ์๋๋ฅผ ์ ์ํ ๊ฒ์ด๋ค.
public static void staticMethod() {
System.out.println("์์ ํด๋์ค์ static ๋ฉ์๋");
}
// ์ค๋ฒ๋ผ์ด๋ฉ์ด๋ค.
@Override
public void instanceMethod() {
System.out.println("์์ ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋");
}
}
public class Main {
public static void main(String[] args) throws IOException {
Parent.staticMethod(); // "๋ถ๋ชจ ํด๋์ค์ static ๋ฉ์๋"
Child.staticMethod(); // "์์ ํด๋์ค์ static ๋ฉ์๋"
Parent parent = new Parent();
parent.instanceMethod(); // "๋ถ๋ชจ ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋"
Child child = new Child();
child.instanceMethod(); // "์์ ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋"
}
}
/* ์ถ๋ ฅ
๋ถ๋ชจ ํด๋์ค์ static ๋ฉ์๋
์์ ํด๋์ค์ static ๋ฉ์๋
๋ถ๋ชจ ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋
์์ ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋
*/
ํธ์ถํ ๋์
static์ ์ด๋ค๋ฉด ํด๋์ค์ด๋ฆ.๋ฉ์๋์ด๋ฆ()
์ด๋ผ๊ณ ์จ์ผ ํ๋ค.
์ค๋ฒ๋ผ์ด๋ฉ์ ํ๋ค๋ฉด ์ฐธ์กฐ๋ณ์.๋ฉ์๋์ด๋ฆ()
์ด๋ผ๊ณ ์จ์ผ ํ๋ค.
static
- static์ ํด๋์ค์ ์ธ์คํด์คํ ์์ด ์ด๋์๋ ํธ์ถ ํ ์ ์๊ธฐ ๋๋ฌธ์ ์ค๋ฒ๋ผ์ด๋ฉ ํ ํ์๊ฐ ์๋ค.
→ ๊ฐ์ฒด ์ธ์คํด์ค์๋ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ํ๋ค.
- static์ ํด๋์ค ๋ ๋ฒจ์์ ๋์ํ๋ค.
์ธ์คํด์ค
- ์ธ์คํด์ค ๋ฉ์๋๋ ๊ฐ์ฒด ๋ ๋ฒจ์์ ๋์
์ค๋ฒ๋ผ์ด๋ฉ์ ํ ๋์ static์ ์จ์ ์ ๋๋ค.
์ค๋ฒ๋ก๋ฉ vs ์ค๋ฒ๋ผ์ด๋ฉ
์ค๋ฒ๋ก๋ฉ(overloading) ๊ธฐ์กด์ ์๋ ์๋ก์ด ๋ฉ์๋๋ฅผ ์ ์ํ๋ ๊ฒ(new)
์ค๋ฒ๋ผ์ด๋ฉ(overriding) ์์๋ฐ์ ๋ฉ์๋์ ๋ด์ฉ์ ๋ณ๊ฒฝํ๋ ๊ฒ(change, modify)
class Parent {
void parentMethod() {}
}
class Child extends Parent {
void parentMethod() {} // ์ค๋ฒ๋ผ์ด๋ฉ
void parentMethod(int i) {} // ์ค๋ฒ๋ก๋ฉ
void childMethod() {}
void childMethod(int i) {} // ์ค๋ฒ๋ก๋ฉ
}
Animal ์์ ์ฝ๋
class Animal {
// ์ค๋ฒ๋ก๋ฉ: makeSound ๋ฉ์๋๋ฅผ ์ฌ๋ฌ ๋ฒ์ ์ผ๋ก ์ ์
public void makeSound() {
System.out.println("๋๋ฌผ์ด ์๋ฆฌ๋ฅผ ๋ด๊ณ ์์ต๋๋ค.");
}
// ์ค๋ฒ๋ก๋ฉ: makeSound ๋ฉ์๋๋ฅผ ์ฌ๋ฌ ๋ฒ์ ์ผ๋ก ์ ์
public void makeSound(String sound) {
System.out.println("๋๋ฌผ์ด " + sound + " ์๋ฆฌ๋ฅผ ๋ด๊ณ ์์ต๋๋ค.");
}
}
class Dog extends Animal {
// ์ค๋ฒ๋ผ์ด๋ฉ: ์์ ํด๋์ค์ makeSound ๋ฉ์๋๋ฅผ ์ฌ์ ์
@Override
public void makeSound() {
System.out.println("๋ฉ๋ฉ!");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
animal.makeSound(); // "๋๋ฌผ์ด ์๋ฆฌ๋ฅผ ๋ด๊ณ ์์ต๋๋ค."
animal.makeSound("๋ผ๋ฃฉ"); // "๋๋ฌผ์ด ๋ผ๋ฃฉ ์๋ฆฌ๋ฅผ ๋ด๊ณ ์์ต๋๋ค."
Dog dog = new Dog();
dog.makeSound(); // "๋ฉ๋ฉ!"
// dog.makeSound("๋ผ๋ฃฉ"); // ์ปดํ์ผ ์๋ฌ: Dog ํด๋์ค์๋ makeSound(String sound) ๋ฉ์๋๊ฐ ์์
}
}
super
์์ ํด๋์ค์์ ์กฐ์ ํด๋์ค๋ก๋ถํฐ ์์๋ฐ์ ๋ฉค๋ฒ๋ฅผ ์ฐธ์กฐํ๋๋ฐ ์ฌ์ฉ๋๋ ์ฐธ์กฐ ๋ณ์์ด๋ค.
this๋ฅผ ์ฌ์ฉํ๋ฏ์ด ์์๋ฐ์ ๋ฉค๋ฒ์ ์์ ์ ๋ฉค๋ฒ์ ์ด๋ฆ์ด ๊ฐ์ ๋๋ super๋ฅผ ๋ถ์ฌ์ ๊ตฌ๋ณํ ์ ์๋ค.
class Parent {
int x=10;
}
class Child extends Parent {
void method() {
System.out.println("x=" + x);
System.out.println("this.x=" + this.x);
System.out.println("super.x=" + super.x);
}
}
class SuperTest {
public static void main(String args[]) {
Child c = new Child();
c.method();
}
}
/*
x=10
this.x=10
super.x=10
*/
class Parent {
int x=10;
}
class Child extends Parent {
int x=20;
void method() {
System.out.println("x=" + x);
System.out.println("this.x=" + this.x);
System.out.println("super.x=" + super.x);
}
}
class SuperTest {
public static void main(String args[]) {
Child c = new Child();
c.method();
}
}
/*
x=20
this.x=20
super.x=10
*/
๊ฐ์ ์ด๋ฆ์ ๋ฉค๋ฒ๋ณ์๊ฐ Parent์๋ ์๊ณ , Child์๋ ์์ ๋๋ super.x์ this.x๋ ์๋ก ๋ค๋ฅธ ๊ฐ์ ์ฐธ์กฐํ๊ฒ ๋๋ค.
super.x → ์กฐ์ ํด๋์ค๋ก๋ถํฐ ์์๋ฐ์ ๋ฉค๋ฒ๋ณ์ x
this.x → ์์ ํด๋์ค์ ์ ์ธ๋ ๋ฉค๋ฒ๋ณ์ x
super() - ์กฐ์ ํด๋์ค์ ์์ฑ์
this()์ ๋ง์ฐฌ๊ฐ์ง๋ก super() ์ญ์ ์์ฑ์์ด๋ค.
this()→ ๊ฐ์ ํด๋์ค์ ๋ค๋ฅธ ์์ฑ์๋ฅผ ํธ์ถํ๋ ๋ฐ ์ฌ์ฉ
super() → ์กฐ์ ํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถํ๋ ๋ฐ ์ฌ์ฉ
Object ํด๋์ค๋ฅผ ์ ์ธํ ๋ชจ๋ ํด๋์ค์ ์์ฑ์ ์ฒซ ์ค์ ์์ฑ์,this() ๋๋ super(),๋ฅผ ํธ์ถํด์ผ ํ๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์ปดํ์ผ๋ฌ๊ฐ ์๋์ ์ผ๋ก super();๋ฅผ ์์ฑ์์ ์ฒซ ์ค์ ์ฝ์ ํ๋ค.
class Parent {
public Parent() {
System.out.println("๋ถ๋ชจ ํด๋์ค์ ์์ฑ์");
}
}
class Child extends Parent {
public Child() {
// ์ปดํ์ผ๋ฌ๊ฐ ์๋์ผ๋ก super();๋ฅผ ์์ฑ์์ ์ฒซ ์ค์ ์ฝ์
// ๋ฐ๋ผ์ ๋ถ๋ชจ ํด๋์ค์ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ํธ์ถ๋จ
System.out.println("์์ ํด๋์ค์ ์์ฑ์");
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
}
}
Child ํด๋์ค์ ์์ฑ์ ์ ์๋ฅผ ํ์ง ์์ผ๋ฉด super();์ด ์๋์ผ๋ก ์์ฑ๋๋ค.
์ธ์คํด์ค๋ฅผ ์์ฑํ ๋๋ ํด๋์ค๋ฅผ ์ ํํ๋ ๊ฒ๋งํผ ์์ฑ์๋ฅผ ์ ํํ๋ ๊ฒ๋ ์ค์ํ๋ค
ํด๋์ค์ ์์ฑ์์ ์ญํ ๋ฐ ๊ด๊ณ
1๏ธโฃ ํด๋์ค - ์ด๋ค ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๊ฒ์ธ๊ฐ?
2๏ธโฃ ์์ฑ์ - ์ ํํ ํด๋์ค์ ์ด๋ค ์์ฑ์๋ฅผ ์ด์ฉํด์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๊ฒ์ธ๊ฐ?
// Car ํด๋์ค ์ ์
public class Car {
private String model;
private String color;
private int speed;
// ์์ฑ์ - ๋ชจ๋ธ๊ณผ ์์์ ์ธ์๋ก ๋ฐ๋ ์์ฑ์
public Car(String model, String color) {
this.model = model;
this.color = color;
this.speed = 0; // ์ด๊ธฐ ์๋๋ 0์ผ๋ก ์ค์
}
// ์์ฑ์ - ๋ชจ๋ธ, ์์, ์ด๊ธฐ ์๋๋ฅผ ์ธ์๋ก ๋ฐ๋ ์์ฑ์
public Car(String model, String color, int speed) {
this.model = model;
this.color = color;
this.speed = speed;
}
// Getter์ Setter ๋ฉ์๋ ์๋ต
}
// ๋ฉ์ธ ํด๋์ค
public class Main {
public static void main(String[] args) {
// Car ํด๋์ค์ ์์ฑ์๋ฅผ ์ ํํ์ฌ ์ธ์คํด์ค ์์ฑ
Car car1 = new Car("Toyota", "Red");
Car car2 = new Car("Tesla", "Black", 100);
// ์์ฑ๋ ์ธ์คํด์ค์ ์ ๋ณด ์ถ๋ ฅ
System.out.println("Car 1: Model - " + car1.getModel() + ", Color - " + car1.getColor() + ", Speed - " + car1.getSpeed());
System.out.println("Car 2: Model - " + car2.getModel() + ", Color - " + car2.getColor() + ", Speed - " + car2.getSpeed());
}
}
์ฌ๊ธฐ์ ์ด๋ค ์์ฑ์๋ฅผ ์์ฑํ ์ง ์ ์ ํ์ ํด์ผ ํ๋ค.
speed๊ฐ ํ์ํ๋ค๋ฉด ๋๋ฒ์งธ ์์ฑ์ ๋ฉ์๋๋ฅผ ์ด์ฉํด์ผ ํ๋ค.
class Point {
int x = 10;
int y = 20;
Point(int x, int y){
this.x = x; // ์ปดํ์ผ๋ฌ์ ์ํด ์์ super() ์๋ ์์ฑ -> Object()๋ฅผ ์๋ฏธํ๋ค.
this.y = y;
}
}
class Point3D extend Point {
int z = 30;
Point3D() {
this(100, 200, 300);
}
Point3D(int x, int y, int z) {
super(x,y);
this.z = z;
}
}
class PointTest2 {
public static void main(String args[]) {
Point3D p3 = new Point3D();
System.out.println("p.3x=" + p3.x);
System.out.println("p.3y=" + p3.y);
System.out.println("p.3z=" + p3.z);
}
}
/*
p3.x=100
p3.y=200
p3.z=300
*/
'๐น๐๐๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์ ์ด์ - static, final, abstract (0) | 2024.05.07 |
---|---|
[Java] package์ import (0) | 2024.05.04 |
[Java] ์ค๋ฒ๋ก๋ฉ(overloading) (1) | 2024.04.18 |
[Java] ์์ฑ์ / this (1) | 2024.04.17 |
[Java] ๋ณ์์ ์ด๊ธฐํ & ํด๋์ค์ ์ธ์คํด์ค์ ์ด๊ธฐํ ์์ (0) | 2024.04.16 |