ConstructorEx.java
기본 생산자일 경우 262p
package kr.or.kihd.inheritancereview;
class A{
public A() {
//super(); 기본생성자가 아닐경우 표시함
System.out.println("생성자A");
}
}
class B extends A{
public B() {
//super();
System.out.println("생성자B");
}
}
class C extends B{
public C() {
//super();
System.out.println("생성자C");
}
}
public class ConstructorEx {
public static void main(String[] args) {
C c = new C();
}
}
기본 생성자가 아닐 경우
package kr.or.kihd.inheritancereview;
class A2{
public A2() {
System.out.println("생성자A");
}
public A2(int x) {
}
}
class B2 extends A2{
public B2() {
System.out.println("생성자B");
}
public B2(int x) {
super(30);
}
}
public class ConstructorEx2 {
public static void main(String[] args) {
B2 b2 = new B2(3);
}
}
업케스팅 예제
package kr.or.kihd.inheritancereview;
class Person{
String name;
String id;
public Person(String name) {
this.name = name;
}
}
class Student extends Person{
String grade;
String department;
public Student(String name) {
super(name); //Person이 기본생성자가 없기 때문.
}
}
public class UpcastingEx {
public static void main(String[] args) {
Student stu = new Student("홍길동");
Person per = new Student("최지만"); //Upcasting (자동형변환) 부모타입만 접근가능하다
per.id = "111";
}
}
//타입은 부모타입으로 선언하고 생성자는 자식타입 => 다형성 구현
다운케스팅 예제
package kr.or.kihd.inheritancereview;
class Person2{
String name;
String id;
public Person2(String name) {
this.name = name;
}
}
class Student2 extends Person2{
String grade;
String department;
public Student2(String name) {
super(name);
}
}
public class DowncastingEx {
public static void main(String[] args) {
Person2 person2;
person2 = new Student2("test");
//업케스팅 자식->부모 타입으로
Student2 student2 = (Student2)person2;
student2.grade = "A";
//다운케스팅 부모->자식 타입으로
//모든멤버 접근가능
}
}
instanceof 사용
package kr.or.kihd.inheritancereview;
class Person3 {}
class Student3 extends Person3 {}
class Researcher3 extends Person3 {}
class Professor3 extends Researcher3 {}
public class InstanceOfEx {
//연산자가 어떤 타입인지 확인
static void print(Person3 person3) {
if(person3 instanceof Person3)
System.out.println("Person타입임");
if(person3 instanceof Student3)
System.out.println("Student3타입임");
if(person3 instanceof Researcher3)
System.out.println("Researcher3타입임");
if(person3 instanceof Professor3)
System.out.println("Professor3타입임");
}
public static void main(String[] args) {
InstanceOfEx.print(new Student3());
print(new Researcher3());
print(new Professor3());
}
}
메서드 오버라이딩 예제
package kr.or.kihd.inheritancereview;
class Shape {
public Shape next;
public Shape() {
this.next = null;
}
public void draw() {
System.out.println("Shape");
}
}
class Line extends Shape {
@Override
public void draw() {
System.out.println("Line");
}
}
class Rect extends Shape {
@Override
public void draw() {
System.out.println("Rect");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Circle");
}
}
public class MethodOverriding {
static void paint(Shape shape) { // Shape를 상속받은 모든 객체들이
// 매개변수 shape로 넘어올수 있음.
shape.draw(); //shape가 가리키는 객체 내에서 오버라이딩한 draw() 호출. 동적바인딩
}
public static void main(String[] args) {
Line line = new Line();
paint(line);
paint(new Shape());
paint(new Rect());
paint(new Circle());
}
}