Override(우선순위가 높은) 메소드

앞 게시글에서 문제점이 생겼다. 해결하는 방법은 무엇일까 ? 상속받은 객체에만 지명할수 있는 이름이 super(차선책)이다. this:NewlecExam 에는 두개의 객체가있다. -> NewlecExam에 total 을 정의해두면된다. (이걸 오버라이드 메소드라한다.)

부모가 가지고 있는 기능을 가리는 함수, 재정의 한 함수, 고쳐쓸수있는 함수를 오버라이드 메소드라한다.

class NewlecExam

import part3.ex4.UI코드분리하기.Exam;

public class NewlecExam extends Exam{
	
	
	
	private int com;

	public int getCom() {
		return com;
	}

	public void setCom(int com) {
		this.com = com;
	}
	
	@Override
	public int total() {
		return super.total()+com;
   	}
	@Override
	public float avg() {
		return total()/4.0f;
	}
}

class Program

import part3.ex4.UI코드분리하기.Exam;

public class Program {
	

	public static void main(String[] args) {
		NewlecExam exam = new NewlecExam();
		exam.setKor(10);
		exam.setEng(10);
		exam.setMath(10);
		exam.setCom(10);
		
		System.out.println(exam.total());
		System.out.println(exam.avg());
		
	}

}

isa

댓글남기기