μμμ κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°(OOP)μ μ€μν κ°λ μ€ νλμ λλ€. μμμ μ½λμ μ¬μ¬μ©μ±μ λμ΄κ³ ν΄λμ€ κ°μ κ³μΈ΅ ꡬ쑰λ₯Ό μ μνλ λ° μ¬μ©λ©λλ€. κΈ°μ‘΄ ν΄λμ€μ νΉμ±μ μ΄μ΄λ°μ μλ‘μ΄ ν΄λμ€λ₯Ό λ§λ€ μ μκ² ν΄μ£Όλ©°, μ½λμ μΌκ΄μ±μ μ μ§νκ³ μ μ§λ³΄μλ₯Ό μ©μ΄νκ² λ§λλλ€.
μμμ 4κ°μ§ νΉμ§
- μλ°λ λ¨μΌ μμλ§μ μ§μν©λλ€.
- μ΅μμ μ‘°μμ Object ν΄λμ€μ λλ€.
- μμ ν΄λμ€λ μ‘°μμΌλ‘λΆν° λͺ¨λ λ©€λ²λ₯Ό μμλ°μ΅λλ€.
- μμ ν΄λμ€μ λ©€λ²λ νμ μ‘°μλ³΄λ€ κ°κ±°λ λ λ§μ λ©€λ²λ₯Ό κ°μ΅λλ€.
λ©μλ μ€λ²λΌμ΄λ©(Method Overriding)
λ©μλ μ€λ²λΌμ΄λ©(Method Overriding)μ μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό μ¬μ μνλ κ°λ
μ
λλ€. μ¦, λΆλͺ¨ ν΄λμ€μμ μ΄λ―Έ μ μλ λ©μλλ₯Ό μμ ν΄λμ€μμ κ°μ μκ·Έλμ²(λ©μλ μ΄λ¦, 맀κ°λ³μ λͺ©λ‘, λ°ν νμ
)λ‘ λ€μ μ μνμ¬ μ¬μ©νλ κ²μ
λλ€. μ΄λ κ² νλ©΄ μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό μλ‘ κ΅¬ννκ±°λ κΈ°λ₯μ νμ₯ν μ μμ΅λλ€.
μ€λ²λΌμ΄λ©μ μν λͺ κ°μ§ κ·μΉ
- μ€λ²λΌμ΄λ©νλ €λ λ©μλλ λΆλͺ¨ ν΄λμ€μμ μ μΈλμ΄μΌ ν©λλ€.
- μ€λ²λΌμ΄λ©νλ €λ λ©μλμ μκ·Έλμ²(μ΄λ¦, 맀κ°λ³μ λͺ©λ‘, λ°ν νμ )λ λΆλͺ¨ ν΄λμ€μ λ©μλμ λμΌν΄μΌ ν©λλ€.
- μ κ·Ό μ μ΄μλ λΆλͺ¨ ν΄λμ€μ λ©μλλ³΄λ€ μ’μ λ²μλ‘ λ³κ²½ν μ μμ΅λλ€.
- μμΈλ λΆλͺ¨ ν΄λμ€μ λ©μλλ³΄λ€ λ μ κ±°λ κ°μ μμ μμΈλ₯Ό λμ§ μ μμ΅λλ€.
super & super()
superλ μλ°μμ ν€μλλ‘ μ¬μ©λλ©°, μ£Όλ‘ μμ κ΄κ³μμ λΆλͺ¨ ν΄λμ€μ λ©€λ²μ μ κ·Όνλ λ°μ μ¬μ©λ©λλ€.
1) λΆλͺ¨ ν΄λμ€μ λ©€λ² νΈμΆ: μμ ν΄λμ€μμ κ°μ μ΄λ¦μ λ©€λ²κ° λΆλͺ¨ ν΄λμ€μλ μ‘΄μ¬ν λ, super ν€μλλ₯Ό μ¬μ©νμ¬ λΆλͺ¨ ν΄λμ€μ λ©€λ²λ₯Ό μ°Έμ‘°ν μ μμ΅λλ€.
2) λΆλͺ¨ ν΄λμ€μ μμ±μ νΈμΆ: μμ ν΄λμ€μ μμ±μμμ λΆλͺ¨ ν΄λμ€μ μμ±μλ₯Ό νΈμΆν λ super()λ₯Ό μ¬μ©ν©λλ€. μ΄κ²μ μ£Όλ‘ μμ ν΄λμ€μ μμ±μμμ λΆλͺ¨ ν΄λμ€μ μ΄κΈ°νλ₯Ό μννκΈ° μν΄ μ¬μ©λ©λλ€.
μμ μ½λ μμ ( μμ, λ©μλ μ€λ²λΌμ΄λ©, super, super() )
public class _02_Inheritance {
public static void main(String[] args) {
Car car = new Car();;
car.setYear(2022);
car.display();
Motorcycle motorcycle = new Motorcycle();
motorcycle.setModel("Sports");
motorcycle.setPrice(300000);
motorcycle.display();
}
}
// Parent class
class Vehicle {
protected String brand;
public Vehicle(String brand) {
this.brand = brand;
}
public void display() {
System.out.print("Brand: " + brand + ", ");
}
}
// Child class inheriting from Vehicle
class Car extends Vehicle {
private int year;
public Car() {
super("KIA"); // super() : invoke the constructor of parent
}
public void setYear(int year) {
this.year = year;
}
@Override
public void display() {
super.display(); // super : call parent class
System.out.println("Year: " + year);
}
}
// Child class inheriting from Vehicle
class Motorcycle extends Vehicle {
private String model;
private int price;
public Motorcycle() {
super("Harley-Davidson");
}
public void setModel(String model) {
this.model = model;
}
public void setPrice(int price) {
this.price = price;
}
// Method overriding
@Override
public void display() {
super.display();
System.out.print("Model: " + model + ", ");
System.out.print("Price: " + price);
}
}
2λ² μ§Έ μμ μ½λ
public static void main(String[] args) {
Dog d = new Dog(3, "Female");
d.printName();
}
}
class Animal_Park {
String name = "Animal";
int age;
public Animal_Park(int age) {
this.age = age;
}
public void printName() {}
}
class Dog extends Animal_Park {
String name = "Dog";
String sex;
public Dog(int age, String sex) {
super(age);
this.sex = sex;
}
@Override
public void printName() {
// λΆλͺ¨ ν΄λμ€μ λ©€λ²μ μ κ·Ό
System.out.println(super.name + " : " + name);
System.out.println("Age : " + age);
System.out.println("Sex : " + sex);
}
}
Inheritance: OOP concept where a class inherits properties and behaviors from another class, promoting code reuse and establishing a hierarchy.
Method Overriding: OOP feature allowing a subclass to provide a specific implementation for a method already defined in its superclass.
super: Keyword in Java used to refer to superclass members, accessing methods and fields from the parent class in the subclass.
super(): Used to invoke the superclass constructor in the subclass, often as the first statement in the subclass constructor.
'Programming > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
17 Abstraction (0) | 2024.01.20 |
---|---|
16 Composition relationship (0) | 2024.01.20 |
14 Encapsulation (feat. Access modifier) (0) | 2024.01.20 |
13 Method (0) | 2024.01.20 |
12 Constructor (0) | 2024.01.19 |
λκΈ