싱글톤 패턴

<aside> 💡

싱글톤 패턴: 애플리케이션 전체에서 객체를 단 하나만 생성해서 사용 (생성자 private, 정적 메소드 생성해서 정적필드 값 리턴)

</aside>

class Singleton {

    private static Singleton singleton = new Singleton();
    private Singleton() {}

    public static Singleton getInstance() {
        return singleton;
    }

}

상속

<aside> 💡

상속: 잘 개발된 클래스를 재사용하여 새로운 클래스 생성 (중복되는 코드 줄이고 개발시간 단축)

</aside>

class Parent {
    int field1;
    void method1() {
        System.out.println("나는 부모클래스의 method1이야");
    }
    public Parent () {
        System.out.println("1");
    }
}
class Child extends Parent{     //Child 클래스는 Parent 클래스에게 상속을 요청
    int field2;
    Child() {
        super();
    }
    void method2() {
        System.out.println("나는 자식클래스의 method2이야");
    }
}


public class InherMain {
    public static void main(String[] args) {
        Parent parent = new Parent();
        Child child = new Child();
        System.out.println(parent.field1);
        parent.method1();
        //parent.method2();   //불가능

        System.out.println(child.field1 + " " + child.field2);
        child.method1();
        child.method2();

    }
}