Java Programming Language Chapter 8 Objects Classes inheritance

Java Programming Language Chapter 8 Objects, Classes, inheritance and Exception 1



Classes 4

UML Class Diagram 5


Declaring Object Reference Variables รปแบบการประกาศตวแปร object : Class. Name object. Ref. Var; Example: Circle my. Circle; 7

Declaring/Creating Objects in a Single Step รปแบบการสราง object. Class. Name object. Ref. Var = new Class. Name(); Example: Assign object reference Create an object Circle my. Circle = new Circle(); 8

Accessing Objects F การอางอง Variable ของ object’s : object. Ref. Var. data e. g. , my. Circle. radius F การเรยกใช method ของ object’s : object. Ref. Var. method. Name(arguments) e. g. , my. Circle. get. Area() 9

The Date Class Java provides a system-independent encapsulation of date and time in the java. util. Date class. You can use the Date class to create an instance for the current date and time and use its to. String method to return the date and time as a string. 10

The Date Class Example For example, the following code java. util. Date date = new java. util. Date(); System. out. println(date. to. String()); displays a string like Sun Mar 09 13: 50: 19 EST 2003. 11


Serving as Proxy to the Calling Object 13

Calling Overloaded Constructor 14



inheritance F ������ การเขยนโปรแกรม class Subclass extends Super. Class { Data Member; Method Member; } FSubclass : ������������ FSuper. Class : ������������ 17 ��

super F ���������� ��� superclass ���������� F subclass ������� constructor ��� superclass ������ subclass ������ constructor ����� F ������ subclass super. member; super(parameter); 18


Abstract Class F ������� keyword “abstract”���� class F ���������� abstract method F ��������� object �������� override method �������� F ������ abstract classname{. . } 20

Abstract Class F ������ การเขยนโปรแกรม abstract classname{ abstract Data Member; abstract Method Member; } F abstract : ������������� abstract 21

Abstract Class F ����� abstract class ����� extends ������� abstract ����� Override Abstract method ���� Abstract Class ����������� Object ������� F ������� Override Method classname extends Abstract. Class { Abstract. Method. Member(){ Statement; } } 22

Interface Class F ������� keyword “interface” “class” ��� keyword F ����������� (final)��� abstract method ���� F ����� override method ����� interface class ����������� F ��������� multiple inheritance ������ keyword “implements” ���� class Action. Event 1 extends JFrame implements Action. Listener 23

Interface Class F ������ การเขยนโปรแกรม public interface. Name{ Data Member; public void Methodname(); } – interace : ����������� interace class – interace. Name : �������� – �������� Test. Interface. java 24

Interface Class F ��������������������� interface class F ������ class Class. Name extends superclass implements Interface. Name{ Data Member; Abstract. Method. Name{ statement; } } F implements 25 : �������


Exception Handler F ������ try { Statement; //�������� Exception { catch (Exception. Class e } ( Statement; //�������� Exception } 27

The finally Clause try { statements; } catch(The. Exception ex) { handling ex; } finally { final. Statements; } 28

animation Trace a Program Execution Suppose no exceptions in the statements try { statements; } catch(The. Exception ex) { handling ex; } finally { final. Statements; } Next statement; 29

animation Trace a Program Execution try { statements; } catch(The. Exception ex) { handling ex; } finally { final. Statements; } The final block is always executed Next statement; 30

animation Trace a Program Execution try { statements; } catch(The. Exception ex) { handling ex; } finally { final. Statements; } Next statement in the method is executed Next statement; 31

animation Trace a Program Execution try { statement 1; statement 2; statement 3; } catch(Exception 1 ex) { handling ex; } finally { final. Statements; } Suppose an exception of type Exception 1 is thrown in statement 2 Next statement; 32

animation Trace a Program Execution try { statement 1; statement 2; statement 3; } catch(Exception 1 ex) { handling ex; } finally { final. Statements; } The exception is handled. Next statement; 33

animation Trace a Program Execution try { statement 1; statement 2; statement 3; } catch(Exception 1 ex) { handling ex; } finally { final. Statements; } The final block is always executed. Next statement; 34

animation Trace a Program Execution try { statement 1; statement 2; statement 3; } catch(Exception 1 ex) { handling ex; } finally { final. Statements; } The next statement in the method is now executed. Next statement; 35

- Slides: 36