ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java_Basic]클래스의 상속 2 : 오버라이딩(윤성우의 열혈 Java 프로그래밍)
    Java/Basic 2021. 1. 2. 15:34

    상속의 특성

    하위 클래스는 상위 클래스의 모든 특성을 지닌다.

    하위 클래스는 자신의 특성을 더한다.

     

    Life is a journey.  인생은 여행이다.

    노트북은 컴퓨터이다.

     

    IS-A 관계 : A is a B 

    class MobilePhone{
    	protected String number;
    	
    	public MobilePhone(String num){
    		number = num;
    	}
    	public void answer(){
    		System.out.println("Hi~ from " + number);
    	}
    }
    
    class SmartPhone extends MobilePhone{
    	private String androidVer;
    
    	public SmartPhone(String num, String ver){
    		super(num);
    		androidVer = ver;
    	}
    	public void playApp(){
    		System.out.println("App is running in " + androidVer);
    	}
    }
    
    class MobileSmartPhon{
    	public static void main(String[] args){
    		SmartPhone phone = new SmartPhone("010-555-777","Nougat");
    		phone.answer();	//전화를 받는다.
    		phone.playApp();	//앱을 선택하고 실행한다.
    	}
    }

     

    class SmartPhone extends MobilePhone{...}

     

    SmartPhone phone = new SmartPhone("010-555-777","Nougat");

     

    MobilePhone = phone = new SmartPhone("010-555-777","Nougat");

     

    자식클래스로 접근시에는 그 안에 있는걸 다 사용할 수 있지만 

    부모 클래스로 접근시에는 부모클래스에 있는 기능밖에 사용하지 못한다.

     

    예시) 스마트폰인줄 알고 스마트폰을 구입하는 사람이 쓰는 기능과, 모바일 폰인줄 알고 스마트폰을 사는 사람이 사용하는 기능에는 차이가 있다. (접근(구입)은 가능하지만)

    class MobilePhone{
    	protected String number;
    
    	public MobilePhone(String num){
    		number = num;
    	}
    	public void answer(){
    		System.out.println("Hi~ from " + number);
    	}
    }
    
    class SmartPhone extends MobilePhone{
    	private String androidVer;
    
    	public SmartPhone(String num, String ver){
    		super(num);
    		androidVer = ver;
    	}
    	public void playApp(){
    		System.out.println("App is running in " + androidVer);
    	}
    }
    
    class MobileSmartPhoneRef{
    	public static void main(String[] args){
    		SmartPhone ph1 = new SmartPhone("010-555-777","Nougat");
    		MobilePhone ph2 = new SmartPhone("010-999-333","Nougat");
    		ph1.answer();
    		ph1.playApp();
    		System.out.println();
    
    		ph2.answer();
    		//ph2.playApp();	//ph2는 부모클래스의 참조변수이기에 자식 클래스 메소드에 접근하지 못한다.
    	}
    }

    상속 관계의 세 클래스

    class Cake{
    	public void sweet(){...}
    }
    class ChessCake extends Cake{
    	public void milky(){...}
    }
    class StrawberryCheeseCake extends CheeseCake{
    	public void sour(){...}
    }

    Cake cake1 = new StrawberryCheeseCake();

    CheeseCake cake2 = new StrawberryCheeseCake();

     

    Cake형 참조변수로 호출할 수 있는 메소드

    cake1.sweet(); -> Cake에 정의된 메소드 호출

     

    CheeseCake형 참조변수로 호출할 수 있는 메소드 

    cake2.sweet();

    cake2.milky();

     

    CheeseCake ca1 = new CheeseCake();

    Cake ca2 = ca1;  -> 가능

     

    Cake ca3 = new CheeseCake();

    CheeseCake ca4 = ca3;  -> 불가능(형변환 하면 가능)

     

    상속과 배열

    참조관계는 배열까지 이어진다.

    CheeseCake[] cakes = new CheeseCake[10];

     

    Cake[] cakes = new CheeseCake[10];

     

    메소드 오버라이딩

    무효화 시키다, 덮어쓰다

    class Cake{
    	public void yummy(){...}
    }
    class CheeseCake extends Cake{
    	public void yummy(){...}
    }
    class StrawberryCheeseCake extends CheeseCake{
    	public void yummy(){...}
    }

    Cake c1 = new StrawberryCheeseCake();

    CheeseCake c2 = new StrawberryCheeseCake();

    StrawberryCheeseCake c3 = new StrawberryCheeseCake();

     

    c1.yummy;    //StrawberryCheeseCake의 yummy 호출

    c2.yummy;    //StrawberryCheeseCake의 yummy 호출

    c3.yummy;    //StrawberryCheeseCake의 yummy 호출

     

    오버라이딩 된 메소드 호출

    super를 사용하면 된다.

    class Cake{
    	public void yummy(){
    		System.out.println("Yummy Cake");
    	}
    }
    
    class CheeseCake extends Cake{
    	public void yummy(){
    		super.yummy();	//Cake 의 yummy 메소드 호출
    		System.out.println("Yummy Cheese Cake");
    	}
    	public void tasty(){
    		super.yummy();	//Cake 의 yummy 메소드 호출
    		System.out.println("Yummy Tasty Cake");
    	}
    }
    
    class YummyCakeSuper{
    	public static void main(String[] args){
    		CheeseCake cake = new CheeseCake();
    		cake.yummy();
    		cake.tasty();
    	}
    }

     

    instanceof 연산자

    참조 가능성을 묻는다.

    boolean타입으로 반환

    class Box{
    	public void simpleWrap(){
    		System.out.println("Simple Wrapping");
    	}
    }
    
    class PaperBox extends Box{
    	public void paperWrap(){
    		System.out.println("Paper Wrapping");
    	}
    }
    
    class GoldPaperBox extends PaperBox{
    	public void goldWrap(){
    		System.out.println("Gold Wrapping");	
    	}
    }
    
    class Wrapping{
    	public static void main(String[] args){
    		Box box1 = new Box();
    		PaperBox box2 = new PaperBox();
    		GoldPaperBox box3 = new GoldPaperBox();
    
    		wrapBox(box1);
    		wrapBox(box2);
    		wrapBox(box3);
    	}
    
    	public static void wrapBox(Box box){
    		if (box instanceof GoldPaperBox){
    			((GoldPaperBox)box).goldWrap();
    		}
    		else if (box instanceof PaperBox){
    			((PaperBox)box).paperWrap();
    		}
    		else{
    			box.simpleWrap();
    		}
    	}
    }

    댓글

Designed by Tistory.