1 CS2104- ADT/Inheritance Lecturer: Dr. Abhik Roychoudhury School of Computing Reading : Chapter 7.1, 7.2 of textbook.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
C++ Classes & Data Abstraction
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.
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Encapsulation by Subprograms and Type Definitions
PZ06A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06A - Inheritance Programming Language Design and Implementation.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Inheritance and Polymorphism CS351 – Programming Paradigms.
OOP Languages: Java vs C++
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS 2104 Prog. Lang. Concepts Subprograms
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Inheritance Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Structure of programming languages OOP. Programming Paradigm A way of conceptualizing what it means to perform computation and how tasks to be carried.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Oriented Programming Chapter Chapter
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 CS Programming Languages Class 22 November 14, 2000.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Classes, Interfaces and Packages
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
ISBN Chapter 12 Support for Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Structure of programming languages OOP. Inheritance.
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Abstract Data Types and Encapsulation Concepts
Review: Two Programming Paradigms
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Inheritance Programming Language Design and Implementation
PZ05A - Abstract data types
Abstract Data Types and Encapsulation Concepts
Subprograms and Programmer Defined Data Type
Dr. Bhargavi Dept of CS CHRIST
Abstract data types Programming Language Design and Implementation
Inheritance Programming Language Design and Implementation
Java Programming Language
Abstract data types Programming Language Design and Implementation
Abstract data types Programming Language Design and Implementation
Inheritance Programming Language Design and Implementation
Inheritance Programming Language Design and Implementation
Presentation transcript:

1 CS2104- ADT/Inheritance Lecturer: Dr. Abhik Roychoudhury School of Computing Reading : Chapter 7.1, 7.2 of textbook

2 Topics covered Abstract Data Types Encapsulation Inheritance Virtual functions, Mixins etc. Abstract classes, Interfaces etc. Diagrams etc. adapted from Pratt/Zelkowitz lecture notes

3 Types A type (data type) is a set of values that an object can have. An abstract data type (ADT) is a: data type Set of functions (operations) that operates on data of that type Each function is defined by its signature. Can also specify operations formally (see section 4.2.5) (Algebraic data types): f(g(A,S)) = A

4 Example ADT ADT student: Operations: SetName: name x student  student GetName: student  name SetCourse: course x student  student GetCourse: student  course GetCredits: student  integer

5 Example ADT In this example, Name and Course are undefined ADTs. They are defined by some other abstraction. Information hiding: We have no idea HOW a student object is stored, nor do we care. All we care about is the behavior of the data according to the defined functions.

6 Information hiding Information hiding can be built into any language C example: typedef struct {i:int;... } TypeA; typedef struct {... } TypeB; P1 (TypeA X, other data) {... } - P1:other data  TypeA P2 (TypeB U, TypeA V) {... } - P2:TypeA  TypeB

7 Information hiding Problems with this structure: No enforcement of information hiding. In P2 can write V.i to access component i of of TypeA object instead of calling P1. Success depends upon conventions But advantage is that it can be done in Pascal or C. We will look at mechanisms later to enforce information hiding (Smalltalk, C++, Java, Ada). We will call this enforcement encapsulation.

8 Recap: subprogram storage Before looking at ADTs in detail, we first need to recall the usual method for subprograms to create local objects. Each subprogram has a block of storage containing such information, called an activation record. Each invocation of a subprogram causes a new activation record to be created. A stack structure is used for activation record storage. Subprograms capture the notion of modularity in programming which is refined through ADT.

9 Encapsulated data types Example: StudentRecord is type Externally visible: void SetName(StudentRecord, Name) name GetName(StudentRecord) Internal to module: char Name[20]; float GPA; char Address[50]; CourseType Schedule[10];

10 Packages in ADA Usual Implementation in Ada: package RationalNumber is type rational is record -- User defined type num, den: integer end record; procedure mult(x in rational; -- Abstract operation y in rational; z out rational); end package;

11 Packages in ADA package body RationalNumber is -- Encapsulation procedure mult(x in rational; y in rational; z out rational) begin z.num := x.num * y.num; z.den := x.den * y.den; end; end package;

12 Use of encapsulated data Usual use of encapsulated RationalNumber: Example: In main procedure do: var A, B, C: rational; A.num := 7;  Should be illegal A.den := 1; Mult(A, B, C); No enforcement of encapsulation

13 Use of encapsulated data Any procedure has access to components of type rational. Can manipulate A.num and A.den without using procedures in package RationalNumber. Let's look at alternative model to enforce encapsulation.

14 Private types package RationalNumber is type rational is private; -- User defined type procedure mult(x in rational; -- Abstract operation y in rational; z out rational); private type rational is record -- User defined type num, den: integer end record; end package;

15 Private types package body RationalNumber is -- Same as before procedure mult(x in rational; y in rational; z out rational) begin z.num := x.num * y.num; z.den := x.den * y.den; end; end package;

16 Private types add protection var A: rational; A.num := 7; -- Now illegal. Private blocks use of num and den outside of package RationalNumber. What is role of private? Any declarations in private part is not visible outside of package What is difference in implementation of rational? This solution encapsulates and hides implementation details of rational.

17 C++ RationalNumber example C++ creates objects of a user defined class. Data storage Set of operations Type rational can be specified in C++ as: class rational{ public: void mult( rational x; rational y) { num = x.num * y.num; den = x.den * y.den;}

18 C++ RationalNumber example protected: int num; int den } rational A, B, C; A.mult(B,C)  invoke encapsulated function A.num = B.num * C.num  Illegal. No access to num and den

19 Storage for C++ classes Visibility of objects: public: globally known private: locally known only protected -- provides for inheritance

20 Inheritance Inheritance provides for passing information from one data object to another automatically It provides a form of data scope similar to syntactic scope. Inheritance through data in object oriented languages is explicit through derived types.

21 Inheritance Static scope (above) - Names are known implicitly through nested procedure names

22 C++ derived classes Consider C++ class rational discussed earlier: class complex: rational { public: void mult( complex x; complex y); { realpt.mult(x.realpt,y.realpt)- realpt.mult(x.imagpt,y.imagpt)... void initial(complex x) {x.realpt.num = 0; x.realpt.den = 1 }

23 C++ derived classes // complex inherits rational components. private: rational realpt; rational imagpt }... complex M, N, P; M.mult(N,P)

24 Power of inheritance class rational { public: mult(...) {... } protected: error(...) {... }... private:... } class complex:rational { public: mult(...) {... } private:... } complex X;

25 Power of inheritance Function error is passed (inherited) to class complex, so X.error is a valid function call. Any derived class can invoke error and a legal function will be executed. But what if we want error to print out the type of its argument? (i.e., want to know if error occurred in a rational or complex data?)

26 Power of inheritance (continued) Inheritance is normally a static property: Function error in class complex is known by compiler to be within class rational. x.error  compiler knows where the error function is. So how can rational::error know where it was invoked, as either rational::error or complex::error? One way - Use function argument: error('rational') or error('complex') Alternative: Use of virtual functions

27 Virtual functions Base class: class rational { error() { cout << name() << endl; } string name() { return “Rational”;}... } Derived class: class complex: rational { string name() { return “Complex”;}... } But if error is called, Rational is always printed since the call rational::name is compiled into class rational for the call in the error function.

28 Virtual functions But if name is defined as: virtual string name() { return “Rational”;} then name() is defined as a virtual function and the function name in the current object is invoked when name() is called in rational::error.

29 Implementing virtual functions Virtual functions imply a runtime descriptor with a location of object rational A; complex B; A.error()  error will call name() in rational B.error()  error will call name() in complex

30 Mixin inheritance Assume want to add feature X to both class A and B: Usual way is to redefine both classes.

31 Mixin inheritance Mixin inheritance: Have definition which is addition to base class (Not part of C++) For example, the following is possible syntax: featureX mixin {int valcounter}  Add field to object newclassA class A mod featureX; newclassB class B mod featureX; Can get similar effect with multiple inheritance: class newclassA:A,featureX {... } class newclassB:B,featureX {... }

32 Abstract classes A class C might be abstract in Java. No instance of C can be created. But instances of subclasses of C can be created. Useful to capture the commonality shared by a set of classes. Expression binary Var. Value

33 Abstract clases abstract class Expression { … } class Binary extends Expression {…} class Variable extends Expression { … } class Value extends Expression { … } In an abstract class Some of the methods are defined inside the abstract class Rest of the methods defined via the subclasses

34 Class Interfaces Typically, identifies a collection of methods to be implemented by various classes. All methods are “abstract” : not defined in the interface. Concrete methods are implemented in the classes implementing the interface. All methods must be implemented in such class definitions.

35 Class Interfaces Public abstract interface Enumeration { // the method signatures appear here public abstract boolean hasMoreElements(); public abstract object nextElement (); } Public class stringTok extends Object implements Enumeration{ // the method implementations appear here public boolean hasMoreElements() {…} public Object nextElement() {…} }

36 Inheritance principles 1. Specialization: Usual form of inheritance: Checking inherits properties of Account. Opposite is generalization: Account is more general than Checking.

37 Inheritance principles 2. Decomposition: Breaking an encapsulated object into parts. A rational object is a num and a den. Useful if inheritance of methods disallowed.

38 Inheritance principles 3. Instantiation: Creation of instances of an object: rational A, B, C;  Represents 3 instantiations of object rational.

39 Inheritance principles 4. Individualization: Related to specialization. Separate objects by function, not structure. A stack and a set can both be an array and and an index pointer, but functionality different. Opposite is grouping.