The Object Oriented Paradigm

Similar documents
The Imperative Paradigm

ECE 122. Engineering Problem Solving with Java

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Object-Oriented Programming in Java

What will this print?

CHAPTER 5 GENERAL OOP CONCEPTS

Encapsulation. Mason Vail Boise State University Computer Science

Elementary Concepts of Object Class

Principles of Object Oriented Programming. Lecture 4

CS1004: Intro to CS in Java, Spring 2005

Multi-Paradigm Approach for Teaching Programming

Chapter 4 Defining Classes I

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Chapter 5 Object-Oriented Programming

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Objects First with Java

The return Statement

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Introduction to Object-Oriented Programming

CLASSES AND OBJECTS IN JAVA

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Chapter 5: Classes and Objects in Depth. Information Hiding

Anatomy of a Class Encapsulation Anatomy of a Method

Chapter 4. Defining Classes I

Objects First with Java A Practical Introduction using BlueJ

Example: Fibonacci Numbers

Object-Oriented Design

Encapsulation in Java

Sri Vidya College of Engineering & Technology

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Defining Classes and Methods

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

UNDERSTANDING CLASS DEFINITIONS CITS1001

Object oriented programming Concepts

B16 Object Oriented Programming

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

From Module To Objects

Object-Oriented Design

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

Imperative Languages!

More on Objects and Classes

Programming II. Modularity 2017/18

What are the characteristics of Object Oriented programming language?

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

COMP6700/2140 Program Design

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Electronic Design Automation Using Object Oriented Electronics

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Inheritance and Subclasses

CS200: Advanced OO in Java

Modularity. Modular program development. Language support for modularity. Step-wise refinement Interface, specification, and implementation

CS Computable Functions. Reading: Chapter 2

Data Structures (list, dictionary, tuples, sets, strings)

Open Closed Principle (OCP)

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CE221 Programming in C++ Part 1 Introduction

Chapter 4: Writing Classes

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Programming II (CS300)

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Programming II (CS300)


Concepts of Programming Languages

Programming II (CS300)

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Methods and Data (Savitch, Chapter 5)

DESIGNING A TOOL TO MAP UML CLASS DIAGRAM INTO RELATIONAL DATABASE

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Classes. Classes as Code Libraries. Classes as Data Structures

2. COURSE DESIGNATION: 3. COURSE DESCRIPTIONS:

CMSC 202. Exceptions

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

What is an algorithm?

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

ENCAPSULATION AND POLYMORPHISM

Software and Programming 1

Enhancing Mood Metrics Using Encapsulation

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Course Outline. Introduction to java

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

An Introduction to Patterns

Lecture 23: Object Lifetime and Garbage Collection

CGS 2405 Advanced Programming with C++ Course Justification

Programming II (CS300)

Data Abstraction. Hwansoo Han

Objects First with Java A Practical Introduction using BlueJ

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Recursion 1. Recursion is the process of defining something in terms of itself.

Ticket Machine Project(s)

Component-Level Design

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Lecture 6 Introduction to Objects and Classes

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

CSI33 Data Structures

Transcription:

The Object Oriented Paradigm Joseph Spring 7COM1023 Programming Paradigms 1

Discussion The OO Paradigm Procedural Abstraction Abstract Data Types Constructors, Methods, Accessors and Mutators Coupling and Cohesion Encapsulation Encapsulation and Cohesion Inheritance Polymorphism 2

The OO Paradigm Procedural Abstraction and Abstract Data Types Involves Classes (act as types and define data objects that contain both code and data (encapsulation)) Objects (have the possibility tyo hide data in an object from code outside the object (data hiding and abstract data)) Encapsulation Inheritance (allows a derived class to be defined as an extension of another base class, (subtyping objects of a derived class type are subtypes of the base class)) Polymorphism Supports both Encapsulation of the data type with its functions Information hiding of data abstraction 3

Imperative and OO Programming Languages An OO programming language is: one which is Turing Complete Supports certain common features that have emerged with the development of the imperative paradigm Control structures Input/output Error and exception handling Procedural abstraction Step wise refinement Expressions and assignment Library support for data structures Features that appear in a variety of languages 4

Procedural Abstraction In the Imperative Programming Paradigm programs are modelled as Algorithms plus Data Structures (Wirth 1976) Algorithms are developed into programs using two complementary ideas Procedural Abstraction Stepwise Refinement Definition Procedural abstraction allows programmer to be concerned with the interface between functions and what they compute Ignoring details of how computations are achieved 5

Stepwise Refinement Definition (Stepwise refinement) utilises procedural abstraction by developing an algorithm from its most general form into a specific implementation This carries over to the OO Paradigm In addition to assignment, the imperative and OO paradigms support: Variable declarations Expressions Conditional statements and Loops Procedural abstraction and stepwise refinement 6

The Imperative and OO Paradigm Variable declarations (fields) Assign names to memory locations Associate types with stored values Expressions Interpreted by: retrieving the current value of named variables from their respective memory locations Computing a result from the above values Given any reference to a variable x, the memory returns the current value in the location associated with x 7

The Imperative and OO Paradigm Conditional statements and Loops Commands normally executed in the order that they appear in memory Procedural Abstraction See Sorting example in Imperative Paradigm 8

Java and OO Paradigm Inner part of class stores Fields Store data persistently within an object Constructors Responsible for ensuring that an object is set up correctly at initialisation Methods Implement the behaviour of an object; its functionality 9

Public class Account { } //Fields private double amount; private String owner_name; A Java Example //Constructor Initialises objects to appropriate state public Account(String o, double init) { amount=init; owner_name=o; } //methods accessors public double balance() { return amount; } public String owner() { return owner_name; } //methods mutator public void deposit(double a) { amount = amount+a; } public double withdraw(double a) { amount = amount-a; return a; } 10

Note that: The Account Class The class definition contains both data fields (often called attributes or data fields) Data hiding is supported by the private prefix in front of the data attributes (this varies between languages but most have some form of data hiding) The special method that has the same name as the class, Account, is used to initialise objects when they are created 11

Using the Account Class Public class Account {... } class TestAccount1 { public static void printacc(account a) { System.out.println(a.owner()+" s account="+a.balance()); } public static void main(string[] args) { Account mine = new Account("Bob", 100.0); Account yours = new Account("Bill", 1000000.0); System.out.println("Initial balances:"); printacc(mine); printacc(yours); mine.withdraw(99.0); yours.deposit(2000000.0); } } System.out.println("Final balances:"); printacc(mine); printacc(yours); 12

Note that: Using the Account Class the class name Account is used as a type to declare variables, mine and yours that doesn t actually create an object, that is done by:... new Account("Bob", 100.0) values of class type (ie. objects) can be stored in variables and passed as parameters operations are invoked on an object by calling: mine.withdraw(99.0); 13

Cohesion and Coupling Cohesion relates to: The number and diversity of tasks for which a single unit of an application is responsible (Barnes and Kolling Objects first with java ) Ideally: One unit of code is responsible for one cohesive task (i.e. one task that can be viewed as one logical unit) A method should implement one logical operation A class should represent one type of entity Motivation for this is reusable code Method can be used in different context 14

Cohesion and Coupling Coupling relates to: The interconnectedness of classes Good design seeks low coupling Level of coupling determines difficulty involved in making changes to s/w (Barnes and Kolling Objects first with java ) Higher the coupling: The greater the changes required in other classes The more time consuming it is to identify and resolve the changes required in other classes Motivation for this is efficient alterations to code in applications 15

Encapsulation Technique of hiding information within structure the hiding of instance data (representing the state of an object) within a class Hiding implementation information from view Suggests: Only information about what a class can do should be visible to the outside not how it does it Advantage If no other class knows how information is stored then it is easy to change how it is stored without breaking other classes Separation of what and how often achieved by making fields private and using an accessor method to access them Provide an example of code to illustrate encapsulation 16

Inheritance Property of objects by which instances of a class can have access to data fields and methods contained in a previously defined class without these definitions being restated Find one example of code to demonstrate inheritance 17

Many forms. Polymorphism Feature of a variable that can take on values of several different types A feature of a single function that can be executed by arguments of a variety of types Find an example of code for each of the following types of Polymorphism Polymorphic Variables Method Polymorphism 18

References 1) Dijkstra E. W., Go to statement considered harmful, Communications of ACM, 11, pp 147-148, (March) 1968 2) Turing A. M., On Computable Numbers, with an application to the Entscheidungsproblem, Proceedings of the London Mathematical Society 2, p265. A correction: 43, pp544-546, 1936 3) Wirth N, Algorithms + data Structures = Programs, Prentice Hall, 1976 4) Josuttis, Nicolai The Standard C++ Library, Addison Wesley, 1999 19