- λΆλͺ¨ ν΄λμ€(μνΌ, μμ ν΄λμ€)μ μμ ν΄λμ€(μλΈ, νμ ν΄λμ€)κ° μμΌλ©°, μμ ν΄λμ€λ λΆλͺ¨ ν΄λμ€λ₯Ό μ νν΄μ, λΆλͺ¨ ν΄λμ€μ λ³μμ λ©μλλ₯Ό μμλ°μ μΈ μ μμ.
- μλ°μμ λ€μ€ μμμ λ¬Έλ²μ μΌλ‘ λΆκ°ν¨.(λ¨κ³μ μΈ μμμ κ°λ₯)
π μμμ΄ λΆκ°λ₯ν κ²½μ°
- λΆλͺ¨ ν΄λμ€μ private μ κ·Όμ νμ κ°λ νλ λ° λ©μλλ μμμ΄ λ¬Όλ €λ°μ μ μμ.
- λΆλͺ¨μ μμ ν΄λμ€κ° μλ‘ λ€λ₯Έ ν¨ν€μ§μ μλ κ²½μ°, λΆλͺ¨μ default μ κ·Ό μ νμ κ°λ νλ λ° λ©μλλ μμμ΄ λ¬Όλ €λ°μ μ μμ.(default μ κ·Ό μ νμ 'κ°μ νμ΄μ§μ μλ ν΄λμ€'λ§ μ κ·Όμ΄ κ°λ₯ν μ κ·Όμ νμμ΄κΈ° λλ¬Έ)
π μμμ΄ νμν μ΄μ
- μ΄λ―Έ λ§λ ¨λμ΄μλ ν΄λμ€λ₯Ό μ¬μ¬μ©ν΄μ λ§λ€ μ μκΈ° λλ¬Έμ ν¨μ¨μ μ΄κ³ κ°λ° μκ°μ΄ λ¨μΆλ¨.
- μμ
public class ParentBook{
String name; //νλ
int price;
public void Print(){ // λ©μλ
System.out.println("μ±
μ μ΄λ¦κ³Ό κ°κ²© : "+name+" "+price);
}
public class ChildBook extends ParentBook{
ChildBook (String name, int price){ // μμ±μ
this.name = name; // ChildBookμ΄ ParetBookμ νλλ₯Ό μμλ°μμ κ°λ₯ν μ μΈ
this.price = price; // "
}
public static void main (String[] args){
ChildBook Child = new ChildBook("λμ λΌμμ€λ μ§ λ무", 10000);
System.out.print("[ꡬν κ²°κ³Ό 1] ");
Child.Print();
}
- [ꡬν κ²°κ³Ό] μ±
μ μ΄λ¦κ³Ό κ°κ²©: λμ λΌμμ€λ μ§λ무 10000
- ChildBook ν΄λμ€κ° ParentBookμ νλμ λ©μλλ₯Ό μμλ°μ. ChildBook ν΄λμ€ λ΄μ λ°λ‘ νλλ λ©μλκ° μ μΈλμ΄ μμ§ μμλλ°λ, μμ±μμ this.name μ μΈμ΄λ, main λ©μλμ Child.Print() κ° μ»΄νμΌ μλ¬κ° λμ§ μκ² λ¨.
- μ°Έκ³ λΈλ‘κ·Έ : https://chanhuiseok.github.io/posts/java-1/
μμ 1.
public class BusinessMan extends Man{
String company;
public BusinessMan(){
// super(10); //λΆλͺ¨ν΄λμ€μ μμ±μλ₯Ό μ€ννλ λ¬Έλ²
// System.out.println("BusineeMan ν΄λμ€μ μμ±μ μ€ν~");
super("νκΈΈλ");
company = "λ―Έμ§μ ";
}
}
public class Man {
String name;
public Man(String name){
this.name = name;
System.out.println("Man ν΄λμ€μ μμ±μ μ€ν~");
}
}
public class Test1 {
public static void main(String[] args) {
BusinessMan m1 = new BusinessMan();
}
}
μμ 2.
- λ€μ ν΄λμ€λ₯Ό κΈ°λ°μΌλ‘ main() λ©μλκ° μ€νλ μ μλλ‘ νλ‘κ·Έλ¨μ μμ±ν΄λ³΄μμ€.

public class A {
private int x;
private int y;
public A(){
x = 1;
y = 1;
}
public A(int x){
this.x = x;
y = 1;
}
public A(int x, int y){
this.x = x;
this.y = y;
}
public void disp(){
System.out.print(" x = " + x + ", y = " + y);
}
}
class B extends A{
private int x;
private int y;
public B(){
x = 1;
y = 1;
}
public B(int x){
super(x);
this.x = 1;
y = 1;
}
public B(int x, int y){
super(x, y);
this.x = 1;
this.y = 1;
}
public void disp(){
super.disp();
System.out.println(" x = " + x + ", y = " + y);
}
}
public class Work {
public static void main(String[] args) {
B bp = new B();
B bp1 = new B(10);
B bp2 = new B(10, 20);
bp.disp(); // x = 1, y = 1, x = 1, y = 1
bp1.disp(); // x = 10, y = 1, x = 1, y = 1
bp2.disp(); //// 10 = 1, y = 20, x = 1, y = 1
}
}
