Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Abstract Data Types & Object Orientation Abstract Data Types (ADT) Concepts –Data Abstraction –ADT in PLs –Encapsulation Object Orientation –Principal.

Similar presentations


Presentation on theme: "1 Abstract Data Types & Object Orientation Abstract Data Types (ADT) Concepts –Data Abstraction –ADT in PLs –Encapsulation Object Orientation –Principal."— Presentation transcript:

1 1 Abstract Data Types & Object Orientation Abstract Data Types (ADT) Concepts –Data Abstraction –ADT in PLs –Encapsulation Object Orientation –Principal features ADTs, Classes, Encapsulation Inheritance Polymorphism –Object Oriented PLs (OOPLs)

2 2 Abstract Data Type (ADT) ADT encapsulates –The internal representation of the type –The operations associated with the type Example: Abstract data type LIST –Internal represention RECORD (data, next) –Operations new, append, add, find, etc.

3 3 ADTs and PLs PLs support of ADTs –An ADT is defined in a single syntactic unit –No access to the internal representation by clients Advantages –Program organization, modifiability Everything associated with an ADTs is together –Separate compilation –User code can't directly access ADT's objects Reliability –ADT's implementation can be changed without affecting user code All PLs provide support for built-in ADTs –integer, floating point, char, etc.

4 4 Object orientation and ADTs Object orientation (OOP - Object Oriented Programming) –Merges the objects and definition and implementation of operations associated with the objects User-defined ADTs began with OOP (Simula67) ADTs & OOP synthesize prior emphases in PL design –Modules and calling structure Procedural Scientific: Fortran –Data objects and transformation of one into another Data Flow Business: COBOL

5 5 Inheritance Constructing new ADTs by extension –New ADT can add state and behavior –New ADT “is an instance” of the old ADT Common inheritance scenario –Given an abstract data type (such as Employee) –How do we reuse this ADT in a new context? (e.g., Senior-Employee) Extend the old record’s data. Extend the old record’s set of operations. Maintain a distinction between the two. - But, Senior-Employees are still Employees, too!

6 6 Polymorphism/Dynamic Binding Given: an operation to be applied to an object –Example: promotion(employee) –There may be multiple definitions of “promotion” for different employee types The appropriate definition of “promotion” is determined at run-time from the actual type of employee Advantages –Consider an array of Senior-Employees, Junior- Employees, New-Employees, etc. –Can invoke promotion on all elements and correct definition for each object will be used.

7 7 Object-Oriented Concepts ADTs: classes Class instances: objects A class that inherits: derived class or subclass The class from which another class inherits: superclass Subprograms that define ADT's operations: methods Calls to methods: messages –Have two parts--a method name and the destination object Access controls to class members –Hidden from everyone - private –Visible to everyone - public –Visible to subclasses hidden from everyone else - protected A class can modify an inherited method: overriding –polymorphism

8 8 Object-Oriented Concepts (cont.) There are two kinds of variables in a class: –Class variables – one per class –Instance variables - one per object There are two kinds of methods in a class: –Class methods – messages to the class –Instance methods – messages to objects Single vs. Multiple Inheritance –interdependencies among classes due to multiple inheritance complicate maintenance

9 9 OOP in Smalltalk A “pure” object oriented language –Everything, including numbers, is an object –All computation involves invoking methods on objects Smalltalk popularized the “OOP Metaphor”: –Have “local” memory (internal data). –Have “local” processing (methods) –Inherit memory and processing from other objects. –Hide local memory/processing from other objects.

10 10 Smalltalk Environment Smalltalk was not only a revolutionary PL, but also a revolutionary PL environment. Smalltalk defines a rich library of predefined tools and tool construction facilities: –Editor –Compiler –Operating System –Graphical user interface (GUI) toolkit All library routines are written in Smalltalk, and allow enhancement/reuse through inheritance!

11 11 OOP in C++ Motivation for C++ –Large base of C code and C programmers –C provides little support for data abstraction No abstract data types Primitive information hiding No inheritance No dynamic binding Principle design goal for C++ –Provide advanced features for object orientation –Maintain compilation-level compatibility with C Any ANSI C program will compile in C++.

12 12 Design Features of C++ Use of OOP features is optional Advanced access control features –Public, private, protected methods –Multiple inheritance Programmer control over dynamic binding –By default, binding is compile-time. –Declaring a method “virtual” instructs the compiler to use dynamic binding. –Programmer controls overhead of binding. Preserves efficiency of C in normal uses.

13 13 OOP in Ada “Classic Ada” (Ada83) –Strong support for abstract data types. –Little support for dynamic binding and inheritance. Ada95 –Dynamic binding with “tagged types”. –Inheritance with “derived types”, “subtypes”. –No multiple inheritance.

14 14 OOP in Java Java is a hybrid of prior OOPLs: –Similarities to C++ Syntax Access control: public, private, protected –Similarities to Smalltalk All binding is dynamic Everything is an object (well, almost) –Similarities to Ada Strongly typed No multiple inheritance Java also has new features: –No pointers –Byte-code verification

15 15 OOP in Lisp Lisp is ideally suited to OOP extensions –1970’s: Flavors package for early Lisps –1980’s: “Our Lisp” –1980’s: Common Lisp Object System (CLOS) Interesting features of CLOS –multi-methods –generic functions –meta-object protocol

16 16 OOP Examples Object-Oriented Concepts Ada C++ C# Ruby

17 17 Examples: Ada Packages –Specification package: the interface –Body package: implementation of the entities in the specification Information Hiding –Specification package has a public and a private part –Implementation details are in the spec package?! Solution: Make all ADTs pointers ?? Subclasses can be derived from tagged types New entities are placed in a record definition No multiple inheritance Dynamic Binding through classwide types –Other bindings are static –Any method may be dynamically bound –Purely abstract base types can be defined in Ada 95

18 18 An Example in Ada package Stack_Pack is type stack_type is limited private; max_size: constant := 100; function empty(stk: in stack_type) return Boolean; procedure push(stk: in out stack_type; elem:in Integer); procedure pop(stk: in out stack_type); function top(stk: in stack_type) return Integer; private -- hidden from clients type list_type is array (1..max_size) of Integer; type stack_type is record list: list_type; topsub: Integer range 0..max_size) := 0; end record; end Stack_Pack

19 19 Example of a Tagged Type in Ada Package Person_Pkg is type Person is tagged private; procedure Display(P : in out Person); private type Person is tagged record Name : String(1..30); Address : String(1..30); Age : Integer; end record; end Person_Pkg; with Person_Pkg; use Person_Pkg; package Student_Pkg is type Student is new Person with record Grade_Point_Average : Float; Grade_Level : Integer; end record; procedure Display (St: in Student); end Student_Pkg; // Note: Display is being overridden from Person_Pkg

20 20 Examples: C++ Each instance of a class has its own data members Instances can be static, stack dynamic, or heap dynamic Visibility: private, public, protected Constructor functions –Initialize the instance's data members of instances do not create the objects –May also allocate storage if part of the object is heap-dynamic –Can have parameters –Implicitly called when an instance is created Destructor functions –Cleanup after an instance is destroyed; reclaim heap storage –Implicitly called when the object’s lifetime ends –Syntax: ~ and the class name

21 21 An Example in C++ class stack { private: int *stackPtr, maxLen, topPtr; public: stack() { // a constructor stackPtr = new int [100]; maxLen = 99; topPtr = -1; }; ~stack () {delete [] stackPtr;}; void push (int num) {…}; void pop () {…}; int top () {…}; int empty () {…}; }

22 22 Inheritance Example in C++ class base_class { private: int a; float x; protected: int b; float y; public: int c; float z; }; class subclass_1 : public base_class { … }; // In this one, b and y are protected and // c and z are public class subclass_2 : private base_class { … }; // In this one, b, y, c, and z are private, // and no derived class has access to any // member of base_class

23 23 Reexportation in C++ A member that is not accessible in a subclass (because of private derivation) can be declared to be visible there using the scope resolution operator :: –e.g. class subclass_3 : private base_class { base_class :: c; … }

24 24 Example: C# Classes are similar to Java’s classes –Besides classes supports struct less powerful stack-dynamic construct no inheritance An inherited method can be overridden using new –The parent class version can still be called with the prefix base To allow dynamic binding –The base class method is marked virtual –The method in a subclass is marked override An abstract method must be implemented in all subclasses All classes are ultimately derived from Object class Properties are supported explicitly

25 25 C# Property Example public class Weather { public int DegreeDays { //** DegreeDays is a property get {return degreeDays;} set { if(value 30) Console.WriteLine("Value out of range: {0}", value); else degreeDays = value; } private int degreeDays;... }... Weather w = new Weather(); int degreeDaysToday, oldDegreeDays;... w.DegreeDays = degreeDaysToday;... oldDegreeDays = w.DegreeDays;

26 26 OOP in Ruby ADT is the class Local variables have “normal” names Instance variable names begin with @ Class variable names begin with two @@ Instance methods have the syntax of Ruby functions – def … end Constructors are named initialize –only one per class Class members can be private or public – public is the default Classes are dynamic

27 27 OOP in Ruby Example class StackClass { def initialize @stackRef = Array.new @maxLen = 100 @topIndex = -1 end def push(number) … end def pop … end def top … end def empty … end end


Download ppt "1 Abstract Data Types & Object Orientation Abstract Data Types (ADT) Concepts –Data Abstraction –ADT in PLs –Encapsulation Object Orientation –Principal."

Similar presentations


Ads by Google