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