예제 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

Similar documents
gnu-lee-oop-kor-lec06-3-chap7

q 이장에서다룰내용 1 객체지향프로그래밍의이해 2 객체지향언어 : 자바 2

Design Issues

쉽게 풀어쓴 C 프로그래밍

No Slide Title

JAVA PROGRAMMING 실습 07. 상속

PowerPoint 프레젠테이션

Cluster management software

PowerPoint 프레젠테이션

슬라이드 1

PowerPoint Presentation

Microsoft PowerPoint - CSharp-10-예외처리

JAVA PROGRAMMING 실습 08.다형성

PowerPoint Presentation

JAVA PROGRAMMING 실습 02. 표준 입출력

5장.key

쉽게 풀어쓴 C 프로그래밍

비긴쿡-자바 00앞부속

(Microsoft PowerPoint - java1-lecture11.ppt [\310\243\310\257 \270\360\265\345])

PowerPoint Presentation

Microsoft PowerPoint - 2강

쉽게 풀어쓴 C 프로그래밍

PowerPoint 프레젠테이션

Spring Data JPA Many To Many 양방향 관계 예제

PowerPoint Presentation

자바 프로그래밍

<4D F736F F F696E74202D2036C0CFC2B05FB0B4C3BCC1F6C7E2C7C1B7CEB1D7B7A1B9D62E707074>

01-OOPConcepts(2).PDF

09-interface.key

PowerPoint 프레젠테이션

05-class.key

PowerPoint 프레젠테이션

Network Programming

10.0pt1height.7depth.3width±â10.0pt1height.7depth.3widthÃÊ10.0pt1height.7depth.3widthÅë10.0pt1height.7depth.3width°è10.0pt1height.7depth.3widthÇÁ10.0pt1height.7depth.3width·Î10.0pt1height.7depth.3width±×10.0pt1height.7depth.3width·¡10.0pt1height.7depth.3width¹Ö pt1height.7depth.3widthŬ10.0pt1height.7depth.3width·¡10.0pt1height.7depth.3width½º, 10.0pt1height.7depth.3width°´10.0pt1height.7depth.3widthü, 10.0pt1height.7depth.3widthº¯10.0pt1height.7depth.3width¼ö, 10.0pt1height.7depth.3width¸Þ10.0pt1height.7depth.3width¼Ò10.0pt1height.7depth.3widthµå

PowerPoint Presentation

02 C h a p t e r Java

PowerPoint Presentation

PowerPoint Presentation

PowerPoint Presentation

A Tour of Java V

Microsoft PowerPoint - chap11

Microsoft PowerPoint 자바-기본문법(Ch2).pptx

Microsoft Word - java19-1-midterm-answer.doc

PowerPoint Presentation

Java ~ Java program: main() class class» public static void main(string args[])» First.java (main class ) /* The first simple program */ public class

C++ Programming

슬라이드 1

Microsoft PowerPoint - 04-UDP Programming.ppt

Microsoft PowerPoint - Java7.pptx

PowerPoint 프레젠테이션

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Microsoft PowerPoint - 04_OOConcepts(2010).pptx

어댑터뷰

Microsoft PowerPoint - lec4_1 [호환 모드]

PowerPoint 프레젠테이션

예외 예외정의예외발생예외처리예외전파 단정 단정의선언 단정조건검사옵션 2

소프트웨어공학개론 강의 5: 객체지향개념 최은만동국대학교컴퓨터공학과

(Microsoft PowerPoint - java2-lecture3.ppt [\310\243\310\257 \270\360\265\345])

Microsoft PowerPoint - java2 [호환 모드]

Microsoft PowerPoint - lec12 [호환 모드]

PowerPoint Presentation

파일로입출력하기II - 파일출력클래스중에는데이터를일정한형태로출력하는기능을가지고있다. - PrintWriter와 PrintStream을사용해서원하는형태로출력할수있다. - PrintStream은구버전으로가능하면 PrintWriter 클래스를사용한다. PrintWriter

rmi_박준용_final.PDF

9장.예외와 단정

교육자료

Semantic Consistency in Information Exchange

JAVA PROGRAMMING 실습 05. 객체의 활용

PowerPoint Presentation

PowerPoint Presentation

JAVA PROGRAMMING 실습 09. 예외처리

ThisJava ..

JAVA 프로그래밍실습 실습 1) 실습목표 - 메소드개념이해하기 - 매개변수이해하기 - 새메소드만들기 - Math 클래스의기존메소드이용하기 ( ) 문제 - 직사각형모양의땅이있다. 이땅의둘레, 면적과대각

fundamentalOfCommandPattern_calmglow_pattern_jstorm_1.0_f…

Microsoft PowerPoint - hci2-lecture12 [호환 모드]

Microsoft PowerPoint - additional07.ppt [호환 모드]

PowerPoint Presentation

gnu-lee-oop-kor-lec10-1-chap10

제11장 프로세스와 쓰레드

PowerPoint Presentation

쉽게

mytalk

Interstage5 SOAP서비스 설정 가이드

1. 객체의생성과대입 int 형변수 : 선언과동시에초기화하는방법 (C++) int a = 3; int a(3); // 기본타입역시클래스와같이처리가능 객체의생성 ( 복습 ) class CPoint private : int x, y; public : CPoint(int a

1

PowerPoint Template

Spring Boot

Microsoft PowerPoint - Chapter 6.ppt

PowerPoint Presentation

* Factory class for query and DML clause creation * tiwe * */ public class JPAQueryFactory implements JPQLQueryFactory private f

ch09

chap10.PDF

Spring Boot/JDBC JdbcTemplate/CRUD 예제

12-file.key

Microsoft Word - EEL2 Lab4.docx

Slide 1

JUNIT 실습및발표

Microsoft PowerPoint - additional08.ppt [호환 모드]

오버라이딩 (Overriding)

Microsoft PowerPoint - java1-lab5-ImageProcessorTestOOP.pptx

<4D F736F F F696E74202D20C1A63139C0E520B9E8C4A120B0FCB8AEC0DA28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Transcription:

제 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-