Question
1) Type Casting. 형변환은 부모 - 자식관계.
2) super
3)
One of the most important conepts of JAVA
Literally, polymorphism is a derived concept of Inheritance.
이 때 source --> super class 로 해서 만들어줘야지.
혹은 이클리스의 경우, source --> fields 누른 다음 탭에서 super class 를 추가한다고 클릭.
생략
Son contains Parents. 아들은 부모를 품는다. 부모가 아들을 품는 것이 아니다.
논리학처럼 생각할 것. P --> S // S -/-> P
Instructor emphasized this concepts. He underlined the fact that it underlies the baisc asuumptions of inheritance.
Manager is a Employee. (o)
Employee is a Manager. (x)
Constructor는 상속할 수 없다. Constructor만큼은 내가 선언해줘야한다.
다시 한 번 기억하자. 필드/메소드는 상속하지만, 생성자는 상속하지 않는다
(1) 왜? 부모에 있는 변수를 받아오고 싶은데 over-ride할려고 하면, 그 변수가 private으로 되어있어서 못 끌고 오는 경우가 많기 때문이다.
부모 변수를 private로 되어있는 경우.
public double getAnnSalary() {
double result = 0.0;
result = this.salary + this.bonus;
return result;
} //this.salary를 쓸 수가 없잖아...
public class Employee {
// Field
protected double salary; //이렇게 바꿔주면 된다
(2) 하지만, 이렇게 변수의 field자체를 건드려버리면 나중에 수정되어버려서 안정성 문제가 생길 수도 있다 .따라서, 강조하지만, getter-setter를 활용하는 것이 좋다.
대신 이 경우에는 부모에다 getter를 설정하고, 아래 자식 class에도 this.salary가 아니라 this.getSalary()와 같이 써줘야한다.
//Over-riding
@Override //a notation.
public double getAnnSalary() {
double result = 0.0;
result = this.getSalary() + this.bonus;
return result;
}
//Getter-Setter as fields are private.
public double getSalary() {
return salary;
}
(3) 혹은 getter-setter 없이도 super를 쓸 수도 있다.
super라는 접근자의 가장 큰 장점은 getter와 setter 없이도 쓸 수가 있다는 점이다.
반드시, this나 super는 클래스 맨 위에 있어야한다.
Question : super는 부모의 메소드만 불러올 수가 있는가?
Question : super는 부모의 변수를 getter와 setter private이 없어도 그대로 결과값을 가지고 오는 것인가?
이건 뭐야???
super(id, name, salary);
참고사항인데, @Over-ride를 쓰면, 장점은 오타를 줄일 수가 있다.
예를 들어, @Override 를 쓰면 똑같은 메소드 이름을 써야함....그럼, 실수로 오타내는 걸 막을 수가 있지.
다형성의 핵심원리는 Over-ride에서 나온다. Over-ride를 안 쓰면 다형성이 만족될 수가 없다.
다형성 = 오버라이드
//Parent codes
draw{}
Son1
삼각형 그리고
Son2
사각형 그리고
부모 클래스에 final을 했다고 하면 그 클래스에 있는 모든 함수 메소드가 재정의할 수 없다.
Q. final과 접근제어자(private)와는 무슨 차이가 있냐?
결국 이렇게 올라가고 올라가다보면, 정말 구체적으로 표현하기 어려운 클래스가 나온다.
예를 들어, 도형이라고 해보자. 원, 사각형, 삼각형은 구체적인 그림을 그릴 수 있지만, 올라가다보면 구체적이지 못한 추상적인 개념으로 올라가게 된다.
Vehicle
아래 코드는 매우 직관적으로 이해가 된다. 하지만 매우 중요하다. 이를 자동 타입변환이라고 한다.
package inheritance;
public class App2 {
public static void main(String[] args) {
//Manager is a Employee
Employee e = new Manager("100", "james", 500, 50);
//It works!!!!!!
Employee ee[] = new Employee[4];
ee[0] = new Employee("100", "james", 500);
ee[1] = new Manager("100", "james", 500, 50);
// It works!!!! Interesting!!! as manager contains employee.
ee[2] = new Employee("100", "james", 500);
ee[3] = new Manager("100", "james", 500, 50);
//However, codes below doesn't work.
//Employee is not a Manager
Manager m = new Employee("100", "james", 550);
}
}
for(Employee em : ee) {
System.out.println(em);
System.out.println(em.getAnnSalary());
}
하지만, 다형성은 아래와 같은 상황을 주의해야합니다.아래의 코드는 눈으로 계속 익히고 외울 정도로 두세요.
Employee ee[] = new Employee[5];
ee[0] = new Employee("100", "james", 500);
ee[1] = new Manager("100", "james", 500, 50); // It works!!!! Interesting!!! as manager contains employee.
ee[2] = new Employee("100", "james", 500);
ee[3] = new Manager("100", "james", 500, 50);
ee[4] = new Sales("194", "jun", 500, "NewYork", 0.3);
for(Employee em : ee) {
System.out.println(em);
System.out.println(em.getAnnSalary());
}
Basically, even though ee[4] is declared by Sales. Generally speaking, as it is declared in Employee, It is unable to use methods of Sales.
Sales as sun, Employee as parents.
But, You can use these code below.
for(Employee em : ee) {
System.out.println(em);
System.out.println(em.getAnnSalary());
if(em instanceof Sales) { //em 중에 Sales타입이 있다면,
System.out.println(((Sales) em).getIncentive());
//Sales 타입의 메소드를 이요하세요.
//위의 코드는 아래의 코드입니다.
//Sales s = (Sales)em; //주1
//System.out.println(s.getIncentive());
}
}
주1울 보자. 왜 저기는 new를 사용하지 않을까?
Story를 잘 기억해보자.
1. Employee라는 객체로 선언을 했기 때문에, Employee코드만 쓸 수 있다.
2. Sales의 instance라면,
혹은 그냥 Employee에 빈 getIncentive 메소드를 그려줘도 된다. 그러면, 오류가 낭텐데 return값은 0.0으로 그냥 기본값 적어주면 된다.