예습/code

오버라이딩

밀김 2023. 1. 13. 10:34
728x90

 

 

 

public class Over {
	public static void main(String[] args) {
		YourPoint t = new YourPoint(); //자손your이 부모Point의 상속을 받고 있으니 객체를 자손으로 만들면됨. 어차피 부모값까지 딸려옴
		t.x = 3;
		t.y = 5;
		t.z = 7;
		
		System.out.println(t.getLocation()); //오버라이딩된 자손 클래스를 호출함.
	}

}

class Point {
	int x;
	int y;
	
	String getLocation() { //매개변수가 없어서,
		return "x = "+x+"y = "+y; //값을 return으로 String 형태로 반환. Sop쓰면 오류남
	}
}

class YourPoint extends Point {
	int z;
	// 오 버 라 이 딩 //
	String getLocation() {
		return "x = "+x+"y = "+y+"z = "+z; 
	}
}

 

 

 

 

728x90