제 10 장상속 예제 1) ConstructorTest.java class Parent public Parent() super - default"); public Parent(int i) this("hello"); super(int) constructor" + i); public Parent(char c) this(); super(char) constructor "+ c); public Parent(String s) this('a'); super(string) constructor "+ s); class Child extends Parent public Child() super(10); //5 라인으로.. sub() constructor"); public Child(int i) //super(); 컴파일러가넣어줌 sub(int) constructor"); public class ConstructorTest args) Child c1=newchild();//20 라인으로... ----------------- "); Child c2= newchild(10); Copyrightc 2017HyeJa Bang's Laboratory. -1-
예제 2) Test.java class A intvar= 10; void method() class B extends A intvar= 20; 1"); void method() 2"); void method1() public class Test 3"); args) A a=newb(); System.out.println(a.var); a.method(); class Parent1 protected void dosomething() Parent.doSomething()"); class Child1 extends Parent1 public void dosomething() public class UseBoth2 Child.doSomething()"); public void dootherthing() Parent1 p1=new Parent1(); Child1 c1=newchild1(); Copyrightc 2017HyeJa Bang's Laboratory. -2-
SEOULTECH CSE 2017JavaProgramming Parent1 p2=new Child1(); p1.dosomething(); c1.dosomething(); p2.dosomething(); UseBoth.doOtherthing()" args) UseBoth2 ub=new UseBoth2(); ub.dootherthing(); 예제 4) Employee.java public class Employee private String name; private intage; private intsalary; public Employee() public Employee(String name, intage,intsalary) this.name =name; this.a ge= age; this.salary =salary; public void getdetails( ) System.out.print(name +"\t" + age+ "\t" + salary); class Engineer extends Employee private String specialty; public void getdetails() super.getdetails(); \t" + specialty); Copyright c 2017HyeJa Bang's Laboratory. -3-
Manager.ja va public class Manager extends Employee private String department; public Manager() public Manager(String name, intage,intsalary, String department) super(name, age,salary); this.department = department; public void getdetails() super.getdetails(); \t" + department); public String getdepartment() return department; TestCasting.java public class TestCasting args) Employee emp= newemployee(" 홍명보 ",30,1000); Manager man= newmanager(" 히딩크 ",40,5000, " 감독 "); Employee empman = newmanager(" 김남일 ",28,3000, " 선수 "); //1 emp= man;//castupok // emp.getdetails(); //2 emp= empman;//ok // emp.getdetails(); //3 empman = emp;//ok // empman.getdetails(); //4 man= emp; //compile error -Downward cast 필요, 컴파일러가체크 //5 man=(manager)emp; //compile OKbutrun-time error(classcast Exception) //Downward cast 로컴파일은통과했으나... 런타임시 object type 체크에 서에러 // man.getdetails(); // System.out.println(man.getDepartment()); // 만약허용된다면문제가될수있는부분 Copyright c 2017HyeJa Bang's Laboratory. -4-
//6 man= empman;//compile error //7 man= (Manager)empMan; //compile OK // 런타임시 object type 체크해보니 Manager 객체이더라.. // 존재하는기능이므로문제가안됨 System.out.println(man.getDepartment()); 예제 5) Animals.java class Pet public void speak() class Catextends Pet pet"); public void speak() class Dog1extends Pet 야옹야옹야옹 "); public void speak() class Duckextends Pet 멍멍멍 "); public void speak() public class Animals 꽥꽥꽥 "); public static void main(string args[]) PetmyPets[] = newpet[4]; mypets[0] = newpet(); mypets[1] = newcat(); mypets[2] = newduck(); mypets[3] = newdog1(); for(int i=0; i<mypets.length; i++) mypets[i].speak(); Copyrightc 2017HyeJa Bang's Laboratory. -5-
예제6)J2SE 5.0 에서새로추가된기법 ConvariantReturnType.java class SuperClass // 리턴타입 ->SuperClass public SuperClass get() return this; class SubClass extends SuperClass // 리턴타입 ->SubClass // 리턴타입이 SubClass 부모 get() 메소드의리턴타입이재정의 // 이전자바에서는자식클래스에서부모클래스메소드의리턴타입의재정의가불가능 //J2SE5.0 에서는 [ 자식클래스메소드 ] 의리턴타입이 //[ 부모클래스메소드리턴타입 ] 의서브클래스로변경되는것에한하여허용 public SubClass get() return this; public void print() Exclusive insubclass."); public class ConvariantReturnType args) //get() 메소드를통해자식클래스가리턴되고자식클래스의 print() 메소드가호출 newsubclass().get().print(); Copyrightc 2017HyeJa Bang's Laboratory. -6-
SEOULTECH CSE 2017JavaProgramming AbstractTest.java abstract class Animal abstract void crying(); class Dogextends Animal void crying() 멍멍 ); class Catextends Animal void crying() 야옹야옹 ); public class AbstractTest args) Dogdog=new Dog(); Catcat= new Cat(); dog.cryin g(); cat.crying(); Copyri ghtc 2017HyeJaBang'sLaborat ory. -7-