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

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
Advertisements

Final and Abstract Classes
MT311 (Oct 2007) Java Application Development Control Structures, Subprograms and Its Implementation Tutorial 7.
Object Oriented Programming with Java
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object-Oriented Programming
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
ISBN Chapter 12 Support for Object- Oriented Programming.
C++ fundamentals.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Object Oriented Programming
Chapter 11 Abstract Data Types and Encapsulation Concepts.
Programming Languages and Paradigms Object-Oriented Programming.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CS 403 – Programming Languages Class 25 November 28, 2000.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Introduction to Object Oriented Programming CMSC 331.
Chapter 12 Support for Object oriented Programming.
Chapter 12 Support for Object-Oriented Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
1 Abstract Data Types & Object Orientation Abstract Data Types (ADT) Concepts –Data Abstraction –ADT in PLs –Encapsulation Object Orientation –Principal.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
ISBN Object-Oriented Programming Chapter Chapter
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Abstract Data Types and Encapsulation Concepts
Support for Object-Oriented Programming
Support for Object-Oriented Programming
ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Object-Oriented Programming
Abstract Data Types and Encapsulation Concepts
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Programming Languages and Paradigms
Lecture 10 Concepts of Programming Languages
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

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

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

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Comparing Smalltalk with Java

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