Presentation is loading. Please wait.

Presentation is loading. Please wait.

MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.

Similar presentations


Presentation on theme: "MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8."— Presentation transcript:

1 MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8

2 2 Tutor Information Edmund Chiu (Group 2) Please begin your subject with [MT311] Webpage:

3 3 Introduction to OO Programming Procedural programming using imperative programming languages was the most popular programming paradigm during 1970s. – modeling programs with subprograms and subprogram libraries Object-based languages appeared and quickly become popular in 1980s. – modeling data objects in a computation task – promote the idea of abstract data type and information hiding – Examples: Ada, Modula-2 Later, Object-oriented programming languages appeared – in addition to data abstraction, two new capabilities are introduced: inheritance and polymorphism

4 4 Data Abstraction Under the concept of data abstraction, programmers use abstract data types to model the data objects of the computation task on hand Abstract data types is a data type satisfying the following conditions – declarations of type and operations are contained in a single syntactic unit. Implementation may be separated. Variables of the defined type can be created. – The representation of the objects is hidden from the program unit using the type (information hiding)

5 5 Encapsulation An abstract data type is the encapsulation of an object which includes all the subprograms that are used to manipulate it – Users of the object were restricted from manipulating it using the provided subprograms only – Then, user code will be independent of the implementation of the object Advantages of encapsulation – Implementation of the ADT can be changed without affecting user’s code if the interfaces remain unchanged – Implementation of the object is separated from other parts of the system and thus can be tested individually – this increases the reliability of the system

6 6 Stack.h #ifndef _stack_h_ #define _stack_h_ struct stack { int elems[100]; int no_of_elems; }; void create(struct stack *s); void destroy(struct stack *s); int isempty(struct stack *s); void push(struct stack *s, int element); void pop(struct stack *s); int top(struct stack *s); #endif

7 7 Prevent User Accessing Data Directly Using stack written in C as an example – only stack.h and stack.o will be released to a user – however, the data structure of struct stack is defined in stack.h and therefore known to users – users can direct access the internal data structure Example: a_stack.no_of_elems=0; a_statck.elems[a_stack.no_of_elems++]=3; – Accessing data structure directly makes the code using the ADT depend on the implementation of the data type. If the implementation of the ADT is changed, user’s code must also change

8 8 Information Hiding For newer languages like Ada and Java, the programmer can specify the attributes of an object to be private For older languages like C, we do information hiding by removing entirely the data structure definition from the header file and require user to create a pointer to that data type only.

9 9 stack2.h and stack2.c #ifndef _stack2_h_ #define _stack2_h_ struct stack; struct stack *create(); void destroy(struct stack *s); int isempty(struct stack *s); void push(struct stack *s, int element); void pop(struct stack *s); int top(struct stack *s); #endif #include “stack2.h” struct stack { int elems[100]; int no_of_elems; } struct stack *create() { struct stack *res; res=(struct stack*) malloc(sizeof(struct stack)); res->no_of_elems=0; return res; } void destroy(struct stack *s) { free(s); } int isemtpty(struct stack *s) { return s->no_of_elems==0; } void push(struct stack *s, int element) { s->elems[s->no_of_elems]=element; s->no_of_elems++; } void pop(struct stack *s) { s->no_of_elems—; }

10 10 ADT in Ada Encapsulation – packages are used to encapsulate the details – specification package provides the interface of the encapsulation – body package provides the implementation – two packages may be compiled separately Information Hiding – Using private part in a program – Limited private is available to declare types without built-in operation – Even stack_type is known to user, the user still cannot directly access the private attributes

11 11 ADT in Java All user-defined data types in Java are classes Generic types are available only in Java 5.0 All subprograms in Java needed to be defined in classes – the object itself is passed into the method implicitly, so there is no need to pass “this” as the method parameter again – In Ada, you need to access the current object explicitly

12 12 Information Hiding in Ada and C C can only declare a pointer to ADT in order to keep information hidden Ada can declare a variable of the data type directly – though pointer is also a method Disadvantage of using pointer: – after a pointer is passed to a subprogram, we cannot prevent the subprogram from changing the contents pointed to by the pointer. Advantage of using pointer to enforce information hiding – Save compilation time – recompilation is not needed even if the ADT has been modified if only pointer is declared in user program

13 13 Parameterized ADT In the previous slides, we used stack of integers as an example – if new data types is needed, you may need to use “find & replace” and then save as another name. – this brings the problem that if the implementation change, you need to change all the files Modern languages allow you to define generic data types that can be fine-tuned for different data types. Examples: instantiating Generic_Stack in Ada – package Integer_Stack is new Generic_Stack(100, Integer);

14 14 Generic Stack in Ada generic Max_Size : Positive; type Element_Type is private; private Generic_Stack is -- The visibly entities / public interface type Stack_Type is limited private; function Empty(Stk : in Stack_Type) return Boolean; procedure Push(Stk : in out Stack_Type; Element : in Element_Type); procedure Pop(Stk : in out Stack_Type); function Top(Stk : in Stack_Type) return Element_Type; -- The hidden part private type List_Type is array (1..Max_Size) of Element_Type; type Stack_Type is record List : List_Type; Topsub : Integer range 0..Max_size := 0; end record; end Generic_Stack

15 15 Generic Type in Java Java 5.0 adds Generic type support – Stack example from: gentypes.html gentypes.html – More information for Generic type: generics.html Basics/Generic.htm generics.html Basics/Generic.htm – Generic type tutorial (PDF)

16 16 Generic Stack in Java class MyStack { private int ix = 0; public static final int MAX = 10; private T[] data = (T[])new Object[MAX]; public void push(T obj) { data[ix++] = obj; } public boolean isEmpty() { return ix =< 0; } public boolean isFull() { return ix == MAX; } public T pop() { if (!isFull()) { return data[--ix]; } throw new ArrayIndexOutOfBoundsException(-1); } }

17 17 Inheritance and Polymorphism Generic types make software reuse more feasible, so does inheritance and polymorphism Inheritance allows programmer to derive new classes from an existing one – new class may have new attributes and methods – new class may also override the existing method Polymorphism gives the ability to invoke the correct version of overridden method – polymorphism works under dynamic binding – dynamic binding means the value to an item is made at runtime – when a method of an object is invoked, the true type of the object (and parameters) will be checked and then the correct version will be called – In Java, dynamic binding of methods will always be used unless the method is defined as final

18 18 Design Issues for OO Languages Exclusivity of objects – In Java, not all entities are objects – primitive data types are used because of faster operations Are subclasses subtypes? – In Java, extends construct actually defined an “is-a” relationship – all functionality in parent class is inherited by the derived class Implementation and interface inheritance – Interface inheritance models “is-a” relationships. The derived class may add further methods; however, it cannot remove methods. – Implementation inheritance makes the (non-private) methods of the base class available for the derived class.

19 19 Design Issues for OO Languages (cont’d) Type Checking and Polymorphism – All types are specified when it is declared in Java (strongly- typed language) – Type checking can be done at compiled time – Polymorphism in Java invokes dynamic binding (not the case in some methods in C++) Single and Multiple Inheritance – Multiple inheritance is not supported in Java – only multiple interfaces can be implemented – Name collisions must be resolved by the programmer if multiple inheritance is allowed – In Java, the implementing method must be compatible to both interfaces in the derived class

20 20 Design Issues for OO Languages (cont’d) Storage allocation and deallocation – All runtime allocated storage are from the heap in Java – Deallocation of storage is implicitly done by the garbage collection mechanism Dynamic and Static Binding – In Java, all binding of method calls are done dynamically unless the method is declared as final

21 21 Comparing Smalltalk with Java

22 22 Implementation of OO Constructs Class Instance Record (CIR) – The storage structure of a class is very similar to a record structure in imperative languages – CIR structure is static. Access to each attribute is done by using offset from the beginning of the CIR instance – Access control (instructed by access modifier) is done statically

23 23 Implementation of Inheritance and Polymorphism In Java, the inheritance information is only needed in compilation time – If A extends B, CIR of A contains all attributes of both A and B – If the method is final, the compiler will determine the method to be bound statically – Each instance of a class has a pointer pointing to a virtual method table, storing the addresses of the virtual methods in the class – The exact destination of a method depends on the type of instance variable – which affects the values stored in the VMT In Smalltalk, all method bindings are dynamics – the method address is searched locally and then from the ancestor when a method call is received – inheritance information has to be available in runtime

24 24


Download ppt "MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8."

Similar presentations


Ads by Google