Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Lecture 9 Concepts of Programming Languages
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ fundamentals.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSCI-383 Object-Oriented Programming & Design Lecture 14.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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.
Object-Oriented Programming Chapter Chapter
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CITA 342 Section 1 Object Oriented Programming (OOP)
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Abstract Data Types and Encapsulation Concepts
7. Inheritance and Polymorphism
Final and Abstract Classes
Inheritance and Polymorphism
11.1 The Concept of Abstraction
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object Oriented Analysis and Design
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Java Programming Language
More Object-Oriented Programming
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Introduction to Classes and Objects
Presentation transcript:

Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms

Object Oriented Programming The development of complicated computer applications has seen the need for data abstraction with the software engineering field. The main advantages of data abstraction are: 1. It reduces the conceptual load for the programmer. 2. It provides a method of fault containment by preventing the programmer from using code in an inappropriate way. 3. It increases the independence among program components. Programmers can work on individual parts of the code without affecting the interface between components. The final point is difficult to achieve in the ``wild’’

Defining a class – C++ Style class list_node { private: list_node* prev; list_node* next; list_node* head; public: int val; list_node(); ~list_node(); list_node* get_prev(); list_node* get_next(); void insert(); void remove(); }; Access Specifiers Destructor ; after the }

Defining a class – C++ Style class list { public: int empty(); list_node* head(); void append(list_node* ln); ~list(); }; In C++, the boolean type can be represented by either the bool type which works identically to the Java version or through the use of an integer value ( carried over from C). Any non-zero integer value evaluates to true in C++. Booleans are ints in C++!

Defining a class - cont This class definition is usually contained within a ``header’’ file, typically labelled file.h To create instances (objects) of the list_node class: 1. list_node element; 2. list_node * element = new list_node(); Both are equally valid ways of creating the object in C++, what is the difference between the two? The invocation of the special method to create an object is called the constructor. In programming languages that do not feature automatic memory management, the objects must be deleted manually via a special method known as the destructor.

Data Access OOP allows data to be hidden and protected in a way that is different to imperative programming. We can declare data that belongs to a class as either public or private. This allows us to control the visibility of the data when classes interact. In C++, every member is private by default, but the use of the public: syntax allows any fields that follow to be declared as public. This is in contrast to the simple nature of the struct in C. All of the data members in structs are public in C but private in C++!!

Subroutines These are referred to as methods in OOP. As data members can be declared to be private, it is convention to supply get and set methods for each private member. Our code for list_node should now show the following changes: private: … int val; public: void set_val ( int v ); int get_val (); …

Derived Classes In OOP we can choose to have an abstract idea coded in a base class. We can then derive a concrete class from the base class. This derivation allows the derived class (child class, sub class) to inherit all of the features of the parent class. By deriving new classes from old ones, the programmer can create arbitrarily deep class hierarchies with added functionality at every level of the hierarchy. Our code from earlier now looks like this: class queue : public list { public: void enqueue (list_node * n); list_node * dequeue(); }; Inheritence in C++

Accessing Derived Methods In the base class, the method definiton for remove could look like this: void list::remove() { head = head->next; } In the derived class, queue, how do we call methods from the parent class? In Java we can use the super keyword to call methods and fields from the parent class. To call methods belonging to a class in C++, we use the scope resolution operator :: void queue::remove() { list::remove(); }

Encapsulation & Inheritance Encapsulation is a technique that allows the programmer to group data and the subroutines that operate on that data and place them together in a single place. With the ability to inherit fields and methods from base classes, some rules for data hiding and data management must be defined. Some questions to be answered include: 1. Should private members of a base class be visible to methods of a derived class? 2. Should public members of a base class always be public members of a derived class? 3. How much control should a base class have over the visibility of its members?

Example class list { public: int empty(); list_node* head(); void append(list_node* ln); ~list(); }; class queue : private list { public: using list::empty; using list::head; void enqueue (list_node * n); list_node * dequeue(); }; We are extending the class list privately. What difference does this make? We must explicitly make public methods that are privately derived accessible. We use the using keyword for this purpose.

C++ Visibility Rules 1. Any class can limit the visibility of its members. i. Public members are visible anywhere the class declaration is in scope. ii. Private members are visible only inside the classes methods. iii. Protected members are visible only inside the methods of the class or any of its descendants. iv. An exception, the friend keyword, what does that do? 2. A derived class can restrict the visibility of members of a base class but can never increase it. i. Private members of a base class are never visible in a child class. ii. Public and protected members of a base class are public or protected respectively in a derived class. iii. Public and protected members of a privately derived base class are private members of the derived class. 3. A protected or privately derived base class can have its members visibility restored through an explicit using declaration.

Comparison versus Java Java also features the keywords public, private and protected. There are slight semantic differences In Java though: 1. Classes cannot be derived as protected or private, hence a derived class cannot increase or restrict the visibility of members of the base class. 2. The protected keyword has a different meaning In Java also. A protected member is visible not only in the base class and derived classes but also throughout the whole package it is declared in.

Constructors and Destructors Most object-oriented languages provide some features for initialising an object at the start of its lifetime. This special method is known as a constructor. This constructor initializes the space in memory that has been allocated. How does the compiler know to allocate the space? Some other languages provide methods, known as a destructor, to provide some housekeeping after the object has reached the end of its lifetime. We have seen that the tilda, ~, is used in C++ to indicate that the special method is a destructor. How do you create a destructor in Java?

Execution Order for Constructors When an object of a derived class is created in C++, the compiler guarantees that the constructor for any base classes will be executed, outermost first, before the constructor of the derived class. How do we manage this in C++, given the absence of a super keyword? class queue : public list { … public : queue::queue( args passed to queue ) : list ( args for list ) { … } … }; Similar syntax can also be used to pass default values to a constructor. list_node::list_node( ) : val ( 0 ) { } This is the same as in Java: queue ( args ) { super ( list args ); }

Dynamic Method Binding One of the main advantages of inheritance is that some derived class D has all of the members of its base class B. Once D is not hiding any of the public members of B, then it is possible for an object of D to represent B in any context where a B could be used. This feature is known as subtype polymorphism.

An Example of Dynamic Binding class person { … void person :: print() { // prints details for the person } }; class student : public person { … void student :: print () { //adds more specific information } }; class lecturer : public person { … void lecturer :: print () { //adds more details } }; … student *s = new student (); lecturer *t = new lecturer (); s -> print (); t -> print (); This is a polymorphic call person *x = s; person *y = t; x -> print () ; y -> print () ;

Example cont… person *x = s; person *y = t; x -> print () ; y -> print () ; Does the choice of the method to be called depend on the types of x and y ? If so then this is known as static binding. Does the choice of the method to be called depend on the classes of the objects s and t to which those variables refer ? If so then is known as dynamic binding. Dynamic binding is central to OOP and ensures that even if we are using the base class to refer to a child class, the correct methods will always be called.