fee-fi-fo-fum
article thumbnail
λ°˜μ‘ν˜•

 

πŸ‘€Today...


Most powerful is he who has himself in his own power. - Seneca

 

 

β˜•μ˜€λŠ˜μ˜ TIL(Today I Learn)


상속과 포함관계

상속(inheritance)은 기쑴의 클래슀λ₯Ό μž¬ν™œμš©ν•˜μ—¬ μƒˆλ‘œμš΄ 클래슀λ₯Ό μž‘μ„±ν•˜λŠ” μžλ°”μ˜ λ¬Έλ²•μš”μ†Œμ΄λ‹€. ν™•μž₯λ˜μ—ˆλ‹€λŠ” 뜻과 κ°™μœΌλ©° extendsν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. μžλ°”μ˜ 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œλŠ” 단일상속(Single inheritance)λ§Œμ„ ν—ˆμš©ν•œλ‹€. ν•˜μ§€λ§Œ μΈν„°νŽ˜μ΄μŠ€λΌλŠ” λ¬Έλ²•μš”μ†Œλ₯Ό μ‚¬μš©ν•΄μ„œ 닀쀑상속과 λΉ„μŠ·ν•œ 효과λ₯Ό λ‚΄λŠ”κ²ƒμ΄ κ°€λŠ₯ν•˜λ‹€.

public class Main {
    public static void main(String[] args) {
        Bike bike = new Bike();
        Car car = new Car();
        MotorBike motorBike = new MotorBike();
        
        bike.run();
        car.run();
        motorBike.run();
    }
}

class Vehicle {
    void run() {
        System.out.println("Vehicle is running");
    }
}

class Bike extends Vehicle {
    void run() {
        System.out.println("Bike is running");
    }
}

class Car extends Vehicle {
    void run() {
        System.out.println("Car is running");
    }
}

class MotorBike extends Vehicle {
    void run() {
        System.out.println("MotorBike is running");
    }
}

// 좜λ ₯κ°’
Bike is running
Car is running
MotorBike is running

 

포함(Composite) κ΄€κ³„λŠ” μƒμ†μ²˜λŸΌ 클래슀λ₯Ό μž¬μ‚¬μš©ν•  수 μžˆλŠ” λ°©λ²•μœΌλ‘œ, 클래슀의 λ©€λ²„λ‘œ λ‹€λ₯Έ 클래슀 νƒ€μž…μ˜ μ°Έμ‘°λ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λŠ” 것을 μ˜λ―Έν•œλ‹€. μ•„λž˜ 예제둜 ν™•μΈν•˜μž...!

public class Employee {
    int id;
    String name;
    Address address;			//μš”λΆ€λΆ„μ΄ μ°Έμ‘°λ³€μˆ˜λ₯Ό μ„ μ–Έν•΄μ„œ ν¬ν•¨κ΄€κ³„λ‘œ μ‚¬μš©

    public Employee(int id, String name, Address address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    void showInfo() {
        System.out.println(id + " " + name);
        System.out.println(address.city+ " " + address.country);
    }

    public static void main(String[] args) {
        Address address1 = new Address("μ„œμšΈ", "ν•œκ΅­");
        Address address2 = new Address("도쿄", "일본");

        Employee e = new Employee(1, "κΉ€μ½”λ”©", address1);
        Employee e2 = new Employee(2, "박해컀", address2);

        e.showInfo();
        e2.showInfo();
    }
}

class Address {
    String city, country;

    public Address(String city, String country) {
        this.city = city;
        this.country = country;
    }
}

// 좜λ ₯κ°’
1 κΉ€μ½”λ”©
μ„œμšΈ ν•œκ΅­
2 박해컀
도쿄 일본

 

# Object Class

object ν΄λž˜μŠ€λŠ” μžλ°”μ˜ 클래슀 상속 κ³„μΈ΅λ„μ—μ„œ μ΅œμƒμœ„μ— μœ„μΉ˜ν•œ ν΄λž˜μŠ€μ΄λ‹€. μžλ°”μ˜ λͺ¨λ“ ν΄λž˜μŠ€λŠ” Object 클래슀둜 λΆ€ν„° ν™•μž₯이 λœλ‹€. λ‹€λ₯Έ 클래슀둜 λΆ€ν„° μ•„λ¬΄λŸ° 상속을 λ°›μ§€ μ•ŠλŠ” ν΄λž˜μŠ€λŠ” μžλ™μœΌλ‘œ extends Objectλ₯Ό μΆ”κ°€ν•œλ‹€.

class ParentEx {  //  μ»΄νŒŒμΌλŸ¬κ°€ "extends Object" μžλ™ μΆ”κ°€ 
}

class ChildEx extends ParentEx {
}

 

 

Method Overriding

λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©μ€ μƒμœ„ν΄λž˜μŠ€λ‘œ λΆ€ν„° 상속받은 λ©”μ„œλ“œμ™€ λ™μΌν•œ μ΄λ¦„μ˜ λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ ν•˜λŠ”κ²ƒμ„ μ˜λ―Έν•œλ‹€. μƒμœ„ 클래슀의 λ©”μ„œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”© ν•˜λ €λ©΄ λ‹€μŒ 3κ°€μ§€ 쑰건이 μ„±λ¦½λ˜μ•Όν•œλ‹€.

  • λ©”μ„œλ“œ μ„ μ–ΈλΆ€κ°€ μƒμœ„ν΄λž˜μŠ€μ˜ 그것과 μ™„μ „νžˆ μΌμΉ˜ν•΄μ•Ό ν•œλ‹€.
  • μ ‘κ·Όμ œμ–΄μžμ˜ λ²”μœ„κ°€ μƒμœ„ 클래슀의 λ©”μ„œλ“œλ³΄λ‹€ κ°™κ±°λ‚˜ λ„“μ–΄μ•Ό ν•œλ‹€.
  • μ˜ˆμ™ΈλŠ” μƒμœ„ν΄λž˜μŠ€μ˜ λ©”μ„œλ“œλ³΄λ‹€ 많이 μ„ μ–Έν• μˆ˜μ—†λ‹€.
public class Main {
    public static void main(String[] args) {
        Bike bike = new Bike();
        Car car = new Car();
        MotorBike motorBike = new MotorBike();
        
        bike.run();
        car.run();
        motorBike.run();
    }
}

class Vehicle {
    void run() {
        System.out.println("Vehicle is running");
    }
}

class Bike extends Vehicle {
    void run() {
        System.out.println("Bike is running");
    }
}

class Car extends Vehicle {
    void run() {
        System.out.println("Car is running");
    }
}

class MotorBike extends Vehicle {
    void run() {
        System.out.println("MotorBike is running");
    }
}

// 좜λ ₯κ°’
Bike is running
Car is running
MotorBike is running

 

 

super&super()

 

# super()

super()ν‚€μ›Œλ“œλŠ” μƒμœ„ 클래슀의 μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜λŠ” 것을 μ˜λ―Έν•œλ‹€. 그리고 μƒμ„±μžμ•ˆμ—μ„œλ§Œ μ‚¬μš©μ΄ κ°€λŠ₯ν•˜κ³  λ°˜λ“œμ‹œ 첫쀄에 μ™€μ•Όλ§Œ ν•œλ‹€. λ§Œμ•½ super()κ°€ μ—†λŠ” κ²½μš°μ—λŠ” μžμ‹ μƒμ„±μžμ˜ 첫쀄에 μ»΄νŒŒμΌλŸ¬κ°€ μžλ™μœΌλ‘œ super()λ₯Ό μ‚½μž…ν•œλ‹€. μ΄λ•Œ μƒμœ„ν΄λž˜μŠ€μ— κΈ°λ³Έμƒμ„±μžκ°€ μ—†μœΌλ©΄ μ—λŸ¬κ°€ λ°œμƒν•œλ‹€.

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
    }
}

class Human {
    Human() {
        System.out.println("휴먼 클래슀 μƒμ„±μž");
    }
}

class Student extends Human { // Human ν΄λž˜μŠ€λ‘œλΆ€ν„° 상속
    Student() {    
        super(); // Human 클래슀의 μƒμ„±μž 호좜
        System.out.println("학생 클래슀 μƒμ„±μž");
    }
}

// 좜λ ₯κ°’
휴먼 클래슀 μƒμ„±μž
학생 클래슀 μƒμ„±μž

 

# super

super ν‚€μ›Œλ“œλŠ” μƒμœ„ν΄λž˜μŠ€μ˜ 객체λ₯Ό 가리킨닀. λ§Œμ•½ λΆ€λͺ¨μ˜ 멀버 λ³€μˆ˜μ™€ μžμ‹μ˜ λ©€λ²„λ³€μˆ˜μ˜ 이름이 κ°™λ‹€λ©΄ super.λΆ€λͺ¨λ©€λ²„λͺ…을 μ§€μ •ν•˜μ—¬ λΆ€λͺ¨μ˜ λ©€λ²„λ³€μˆ˜λͺ…을 κ°€λ¦¬ν‚¬μˆ˜μžˆλ‹€.

public class Example {
    public static void main(String[] args) {
        SubClass subClassInstance = new SubClass();
        subClassInstance.callNum();
    }
}

class SuperClass {
    int count = 20; // super.count
}

class SubClass extends SuperClass {
    int count = 15; // this.count

    void callNum() {
        System.out.println("count = " + count);
        System.out.println("this.count = " + this.count);
        System.out.println("super.count = " + super.count);
    }
}


// 좜λ ₯κ°’
count = 15
count = 15
count = 20

 

 

μΊ‘μŠν™”μ™€ νŒ¨ν‚€μ§€

μΊ‘μŠν™”λŠ” νŠΉμ •κ°μ²΄ μ•ˆμ— κ΄€λ ¨λœ 속성과 κΈ°λŠ₯을 ν•˜λ‚˜μ˜ 캑슐둜 λ§Œλ“€μ–΄ 데이터λ₯Ό μ™ΈλΆ€λ‘œ λ³΄ν˜Έν•˜λŠ” 것을 λ§ν•œλ‹€. λ°μ΄ν„°λ³΄ν˜Έμ™€ λ‚΄λΆ€μ μœΌλ‘œ μ‚¬μš©λ˜λŠ” 데이터λ₯Ό μ™ΈλΆ€λ…ΈμΆœλ‘œλΆ€ν„° λ°©μ§€ν•˜κΈ° μœ„ν•¨μ΄λ‹€. (data hiding)

 

νŒ¨ν‚€μ§€(package)λŠ” νŠΉμ •ν•œ λͺ©μ μ„ κ³΅μœ ν•˜λŠ” ν΄λž˜μŠ€μ™€ μΈν„°νŽ˜μ΄μŠ€μ˜ λ¬ΆμŒμ„ μ˜λ―Έν•˜κ³  μžλ°”μ—μ„œμ˜ νŒ¨ν‚€μ§€λŠ” 물리적인 ν•˜λ‚˜μ˜ directory이닀. 이 directoryλŠ” 계측ꡬ쑰λ₯Ό κ°€μ§€κ³  있고, κ³„μΈ΅κ°„μ˜ ꡬ뢄은 (.)으둜 ν‘œν˜„ν•œλ‹€. νŒ¨ν‚€μ§€μ˜ μž₯점은 클래슀의 μΆ©λŒμ„ λ°©μ§€ν•΄μ€€λ‹€. 같은 μ΄λ¦„μ˜ 클래슀λ₯Ό κ°€μ§€κ³  μžˆλ”λΌλ„ λ‹€λ₯Έ νŒ¨ν‚€μ§€μ— μ†Œμ†λ˜λ©΄ 이름λͺ…μœΌλ‘œ 좩돌이 λ°œμƒν•˜μ§€ μ•ŠλŠ”λ‹€.

 

νŒ¨ν‚€μ§€μ˜ 선언은 μ•„λž˜ μ½”λ“œμ™€ 같이 μ„ μ–Έν•œλ‹€.

package practicepack.test2; // import 문을 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 경우, λ‹€λ₯Έ νŒ¨ν‚€μ§€ 클래슀 μ‚¬μš©λ°©λ²•

public class PackageImp {
		public static void main(String[] args) {
			practicepack.test.ExampleImport example = new practicepack.test.ExampleImport();
		}
}

ν•˜μ§€λ§Œ μœ„μ™€κ°™μ΄ λ‹€λ₯ΈνŒ¨ν‚€μ§€μ˜ 클래슀λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” μ„ μ–Έμ‹œμ— νŒ¨ν‚€μ§€λͺ…을 λͺ¨λ‘ ν¬ν•¨μ‹œμΌœμ„œ 정보λ₯Ό μ œκ³΅ν•΄μ•Όν•œλ‹€. 이λ₯Ό 쒀더 μ‰½κ²Œ μ‚¬μš©ν•˜λŠ” 방법이 import이닀. μ‚¬μš©λ²•μ€ μ•„λž˜μ™€ κ°™λ‹€.

package practicepack.test;

public class ExampleImp {
		public int a = 10;
		public void print() {
			System.out.println("Import λ¬Έ ν…ŒμŠ€νŠΈ")
}
package practicepack.test2; // import 문을 μ‚¬μš©ν•˜λŠ” 경우

import practicepack.test.ExampleImp // import λ¬Έ μž‘μ„±

public class PackageImp {
		public static void main(String[] args) {
			ExampleImp x = new ExampleImp(); // 이제 νŒ¨ν‚€μ§€ λͺ…을 μƒλž΅ κ°€λŠ₯
		}
}

μ΄λ ‡κ²Œ μ‚¬μš©ν•˜λ©΄ νŒ¨ν‚€μ§€λͺ…을 μƒλž΅ν•˜κ³  ν΄λž˜μŠ€μ— λ°”λ‘œ 접근이 κ°€λŠ₯ν•˜λ‹€.

 

μ ‘κ·Όμ œμ–΄μž

μ ‘κ·Όμ œμ–΄μžλŠ” 멀버 λ˜λŠ” ν΄λž˜μŠ€μ— μ‚¬μš©λ˜μ–΄ μ™ΈλΆ€μ—μ„œ μ ‘κ·Όν•˜μ§€ λͺ»ν•˜λ„둝 μ œν•œν•˜λŠ” 역할을 ν•œλ‹€. μƒλž΅μ‹œμ—λŠ” default둜 μ§€μ •λ˜κ³  클래슀,λ©€λ²„λ³€μˆ˜,λ©”μ†Œλ“œ,μƒμ„±μžμ— 지정이 κ°€λŠ₯ν•˜λ‹€.

프라이빗(private) : 같은 클래슀 λ‚΄λΆ€μ—μ„œλ§Œ μ ‘κ·Όν•  수 μžˆλ‹€.

λ””ν΄νŠΈ(default) : κ°™μ€ νŒ¨ν‚€μ§€λ‚΄μ—μ„œλ§Œ μ ‘근이 κ°€λŠ₯ν•˜λ‹€.

ν”„λ‘œν…ν‹°λ“œ(protected) : κ°™μ€ νŒ¨ν‚€μ§€ λ‚΄μ—μ„œ μ ‘κ·Όν•  μˆ˜ μžˆλ‹€. λ‹€λ₯Έ ν΄λž˜μŠ€μ—μ„œλŠ” μ ‘κ·Όν•  μˆ˜ μ—†μ§€λ§Œ, μƒμ† λ°›μ€ μžμ‹ ν΄λž˜μŠ€μ—μ„œλŠ” μ ‘근이 κ°€λŠ₯ν•˜λ‹€.

퍼블릭(public) : μ ‘κ·Ό μ œν•œμ΄ μ—†λ‹€. λ‹€λ₯Έ νŒ¨ν‚€μ§€, λ‹€λ₯Έ ν΄λž˜μŠ€μ—μ„œ μ ‘κ·Όν•  μˆ˜ μžˆλ‹€.

 

 

getter / setter method

getter /setter λ©”μ„œλ“œλŠ” λŒ€ν‘œμ μœΌλ‘œ private μ ‘κ·Όμžκ°€ ν¬ν•¨λ˜μ–΄μžˆλŠ” 객체의 λ³€μˆ˜μ˜ 데이터값을 μΆ”κ°€ν•˜κ±°λ‚˜ μˆ˜μ •ν•˜κ³  μ‹Άμ„λ•Œ κ°€λŠ₯ν•˜κ²Œ ν•΄μ€€λ‹€. setterλŠ” 데이터값을 λ³€κ²½ν• μˆ˜μžˆκ²Œ ν•΄μ£Όκ³ , getterλŠ” μ„€μ •ν•œ λ³€μˆ˜κ°’μ„ μ½μ–΄μ˜€λŠ”λ° μ‚¬μš©λœλ‹€.

μ•„λž˜ μ½”λ“œλ₯Ό μ°Έκ³ ν•˜μž. 

 public class GetterSetterTest {
    public static void main(String[] args) {
        Worker w = new Worker();
        w.setName("κΉ€μ½”λ”©");
        w.setAge(30);
        w.setId(5);

        String name = w.getName();
        System.out.println("근둜자의 이름은 " + name);
        int age = w.getAge();
        System.out.println("근둜자의 λ‚˜μ΄λŠ” " + age);
        int id = w.getId();
        System.out.println("근둜자의 IDλŠ” " + id);
    }
}

class Worker {
    private String name; // λ³€μˆ˜μ˜ 은닉화. μ™ΈλΆ€λ‘œλΆ€ν„° μ ‘κ·Ό λΆˆκ°€
    private int age;
    private int id;

    public String getName() { // λ©€λ²„λ³€μˆ˜μ˜ κ°’ 
        return name;
    }

    public void setName(String name) { // λ©€λ²„λ³€μˆ˜μ˜ κ°’ λ³€κ²½
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age < 1) return;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

// 좜λ ₯κ°’
근둜자의 이름은 κΉ€μ½”λ”©
근둜자의 λ‚˜μ΄λŠ” 30
근둜자의 IDλŠ” 5

 

 

 


πŸ‘Šν˜Όμžμ„œ ν•΄κ²°ν•˜κΈ°


 

 

 

λ°˜μ‘ν˜•
profile

fee-fi-fo-fum

@hae02y

ν¬μŠ€νŒ…μ΄ μ’‹μ•˜λ‹€λ©΄ "μ’‹μ•„μš”β€οΈ" λ˜λŠ” "κ΅¬λ…πŸ‘πŸ»" ν•΄μ£Όμ„Έμš”!