7-1.μμ(μμ)
μμμ μ΄λ―Έ μ κ°λ°λ ν΄λμ€λ₯Ό μ¬μ¬μ©ν΄μ μλ‘μ΄ ν΄λμ€λ₯Ό λ§λ€κΈ° λλ¬Έμ μ€λ³΅λλ μ½λλ₯Ό μ€μ¬ μ€λ€.
μλ₯Ό λ€μ΄, field1, field2, method1(), method2()λ₯Ό κ°μ§λ ν΄λμ€λ₯Ό μμ±νλ€κ³ κ°μ ν΄λ³΄μ. field1κ³Ό method1()μ κ°μ§κ³ μλ ν΄λμ€κ° μλ€λ©΄, 4κ°λ₯Ό λͺ¨λ μ²μλΆν° μμ±νλ κ²λ³΄λ€λ ν΄λμ€λ₯Ό μμνκ³ , field2μ method2()λ§ μΆκ° μμ±νλ κ²μ΄ λ³΄λ€ ν¨μ¨μ μ΄κ³ κ°λ° μκ°μ μ μ½ν΄μ€λ€.
ν΄λμ€ μμ
νλ‘κ·Έλ¨μμλ μμμ΄ λΆλͺ¨λ₯Ό μ ννλ€. μμ ν΄λμ€λ₯Ό μ μΈν λ μ΄λ€ λΆλͺ¨ ν΄λμ€λ₯Ό μμλ°μ κ²μΈμ§ κ²°μ νκ³ , μ νλ λΆλͺ¨ ν΄λμ€λ λ€μκ³Ό κ°μ΄ extends λ€μ κΈ°μ νλ€.
class μμν΄λμ€ extends λΆλͺ¨ν΄λμ€ {
//νλ
//μμ±μ
//λ©μλ
}
*μλ°μμ μμμ νΉμ±
1. μ¬λ¬ κ°μ λΆλͺ¨ ν΄λμ€λ₯Ό μμν μ μλ€. κ·Έλ¬λ―λ‘ extends λ€μλ λ¨ νλμ λΆλͺ¨ ν΄λμ€λ§ μμΌ νλ€.
2. λΆλͺ¨ ν΄λμ€μμ private μ κ·Ό μ νμ κ°λ νλμ λ©μλλ μμ λμμμ μ μΈλλ€. κ·Έλ¦¬κ³ λΆλͺ¨ ν΄λμ€μ μμ ν΄λμ€κ° λ€λ₯Έ ν¨ν€μ§μ μ‘΄μ¬νλ€λ©΄ default μ κ·Ό μ νμ κ°λ νλμ λ©μλλ μμ λμμμ μ μΈλλ€.
λΆλͺ¨ μμ±μ νΈμΆ
νμ€μμ λΆλͺ¨ μλ μμμ΄ μμ μ μλ―μ΄ μλ°μμλ μμ κ°μ²΄λ₯Ό μμ±νλ©΄, λΆλͺ¨ κ°μ²΄κ° λ¨Όμ μμ±λκ³ κ·Έλ€μμ μμ κ°μ²΄κ° μμ±λλ€.
λΆλͺ¨ μμ±μλ μμ μμ±μμ μ¨μ΄ μλ€. λΆλͺ¨ μμ±μλ μμ μμ±μμ 맨 첫 μ€μμ νΈμΆλλ€.
ex)
λΆλͺ¨ ν΄λμ€
public class People {
public String name;
public String ssn;
public People(String name, String ssn) {
this.name = name;
this.ssn = ssn;
}
}
μμ ν΄λμ€
public class Student extends People {
public int studentNo;
public Student(String name, String ssn, int studentNo) {
super(name, ssn); --> λΆλͺ¨ μμ±μ νΈμΆ
this.studentNo = studentNo;
}
}
μμ κ°μ²΄ μ΄μ©
pubic class StudentExample {
public static void main(String[] args) {
Student student = new Student("νκΈΈλ", "12345", 1);
System.out.println("name: " + student.name);
System.out.println("ssn: " + student.ssn);
System.out.println("studentNo: " + student.studentNo);
}
}
*λΆλͺ¨ μμ±μλ₯Ό νΈμΆνμ§ μμΌλ©΄ "Implicit super constructor λΆλͺ¨ν΄λμ€ is undefined. Must explicitly invoke another constructor"λΌλ μ»΄νμΌ μλ¬κ° λ°μ
λ©μλ μ¬μ μ (overiding)
λΆλͺ¨ ν΄λμ€μ λͺ¨λ λ©μλκ° μμ ν΄λμ€μ λ§κ² μ€κ³λμ΄ μλ€λ©΄ κ°μ₯ μ΄μμ μΈ μμμ΄μ§λ§, μ΄λ€ λ©μλλ μμ ν΄λμ€κ° μ¬μ©νκΈ°μ μ ν©νμ§ μμ μλ μλ€. μ΄ κ²½μ° μμλ μΌλΆ λ©μλλ μμ ν΄λμ€μμ λ€μ μμ ν΄μ μ¬μ©ν΄μΌ νλ€. μλ°λ μ΄λ° κ²½μ°λ₯Ό λ©μλ μ¬μ μ μ¦, μ€λ²λΌμ΄λ©μ΄λΌκ³ νλ€.
*λ©μλ μ¬μ μ λ°©λ²
- λΆλͺ¨μ λ©μλμ λμΌν μκ·Έλμ²(λ¦¬ν΄ νμ , λ©μλ μ΄λ¦, λ§€κ° λ³μ λͺ©λ‘)λ₯Ό κ°μ ΈμΌ νλ€.
- μ κ·Ό μ νμ λ κ°νκ² μ¬μ μν μ μλ€.
- μλ‘μ΄ μμΈ(Exception)λ₯Ό throwsν μ μλ€.
λΆλͺ¨ λ©μλ νΈμΆ
μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό μ¬μ μνκ² λλ©΄, λΆλͺ¨ ν΄λμ€μ λ©μλλ μ¨κ²¨μ§κ³ μ¬μ μλ μμ λ©μλλ§ μ¬μ©λλ€. κ·Έλ¬λ μμ ν΄λμ€ λ΄λΆμμ μ¬μ μλ λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό νΈμΆν΄μΌ νλ μν©μ΄ λ°μνλ€λ©΄ λͺ μμ μΌλ‘ super ν€μλλ₯Ό λΆμ¬μ λΆλͺ¨ λ©μλλ₯Ό νΈμΆν μ μλ€.
ex)
super.λΆλͺ¨λ©μλ();
final ν΄λμ€μ final λ©μλ
final ν€μλλ ν΄λμ€, νλ, λ©μλλ₯Ό μ μΈν λ μ¬μ©ν μ μλλ°, ν΄λΉ μ μΈμ΄ μ΅μ’ μνμ΄κ³ κ²°μ½ μμ λ μ μμμ λ»νλ€.
ν΄λμ€μ λ©μλλ₯Ό μ μΈν λ final ν€μλκ° μ§μ λλ©΄ μμκ³Ό κ΄λ ¨μ΄ μλ€λ μλ―Έ
μμν μ μλ final ν΄λμ€
ν΄λμ€λ₯Ό μ μΈ ν λ final ν€μλλ₯Ό class μμ λΆμ΄λ©΄ μ΄ ν΄λμ€λ μ΅μ’ μ μΈ ν΄λμ€μ΄λ―λ‘ μμν μ μλ ν΄λμ€κ° λλ€.
ex)
public final class ν΄λμ€ {...} -> μμ λΆκ°
μ¬μ μν μ μλ final λ©μλ
λ©μλλ₯Ό μ μΈν λ final ν€μλλ₯Ό λΆμ΄λ©΄ μ΄ λ©μλλ μ΅μ’ μ μΈ λ©μλμ΄λ―λ‘ μ¬μ μν μ μλ λ©μλκ° λλ€.
ex)
public final 리ν΄νμ λ©μλ(λ§€κ°λ³μ) {...} -> μ€λ²λΌμ΄λ© λΆκ°
protected μ κ·Ό μ νμ
protectedλ publicκ³Ό default μ κ·Ό μ νμ μ€κ°μ―€μ ν΄λΉνλ€. κ°μ ν¨ν€μ§μμλ defaultμ κ°μ΄ μ κ·Ό μ νμ΄ μμ§λ§ λ€λ₯Έ ν¨ν€μ§μμλ μμ ν΄λμ€λ§ μ κ·Όμ νμ©νλ€.
λ€λ₯Έ ν¨ν€μ§ μμ λ
ex)
import sec01.exam07.pack1.A;
public class D extends A {
public D(){
super(); --> μ΄λ κ² ν΄μ μ κ·Ό κ°λ₯
this.field = "value";
this.method();
}
}
'κ°λ° μ§μ, μ€ν¬λ¦½νΈ > Java λ¬Έλ²' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
7-3μμ(μΆμ ν΄λμ€) (0) | 2023.10.09 |
---|---|
7-2μμ(νμ λ³νκ³Ό λ€νμ±) (0) | 2023.10.09 |
6-6ν΄λμ€(ν¨ν€μ§μ μ κ·Ό μ νμ) (1) | 2023.10.03 |
6-5ν΄λμ€(μΈμ€ν΄μ€ λ©€λ²μ μ μ λ©€λ²) (0) | 2023.10.02 |
6-4ν΄λμ€(λ©μλ) (1) | 2023.10.02 |
λκΈ