You are on page 1of 38

Introduction to OO Concepts and UML



Introduction to Classes and Objects Introduction to UML Features of Object Oriented Paradigm  Encapsulation  Data Abstraction  Inheritance  Polymorphism  Association  Persistence  


From Procedural to OO 

All programming languages support four basic concepts.  Calculation constants, variables, operators, expressions  Selection if-else, switch, ?  Iteration - while, do, for  Abstraction The process of creating self-contained units of software that allows the solution to be parameterized and therefore more general purpose. Abstraction is the fundamental concept that differentiates procedural programming languages such as C from OO languages such as Java, C++. 


float b) { float result.g. result = (a + b)/2.Abstraction in Procedural Languages   Abstraction in procedural languages is provided through functions or procedures. A c function to calculate the average of two numbers. } 4 . return result. Functions modify external data by performing a particular operation. e. float calculate_average (float a.

Abstract Data Type (ADT) is a definition that contains data and procedures that operate on the data. which contains data and procedures that operate on data. car. account etc. e. class is an implementation on an abstract data type.  data is referred to as fields.Abstraction in OO Languages       Abstraction in OO languages is provided through an abstract data type (ADT). In OO terminology. parameters or attributes  procedures are referred to as methods or operations A class contains attributes and methods that operate on the attributes. student. animal.g. 5 . A class is a generalization of a real world entity that is used in a OO program depending on the application. In Java.

Account Class Example Account account name Data account number withdraw cash deposit cash check account balance Methods 6 .

checkBalance(). } // Class Account 7 . double accountBalance.Account Class // Pseudo Code for Account class class Account { String accountName. withdrawCash(). depositCash().

Circle Class Example Circle centre Data radius area circumference move Methods 8 .

move(). } // Class Circle 9 . double radius. area(). centreY.Circle Class // Pseudo Code for Circle class class Circle { double cetreX. circumference().

The object: Zahid s Account is another instance of the Account class. The object: Zahid s Account is an instance of Account class.000 A class represents a template for several objects that have common properties.g. 10    . An Object Oriented system is a collection of interacting Objects. e.Class vs Object  An Object is an instance of a class. Zahid s Account Account Name = Zahid Account Balance = 15. Objects hold state information while classes do not.g. e.

9: Examples of objects 11 .Examples of Objects CAR BOY GIRL CLOCK VDU BOOK TREE TRIANGLE Figure 1.

Walk() Vehicle Objects Abstract Into Vehicle Class Attributes: Name. Age. Move() Figure 1. Color Operations: Start(). Border. Accelerate() Polygon Objects Abstract Into Polygon Class Attributes: Vertices. Listen(). Erase(). Color. FillColor Operations: Draw().12: Objects and classes 12 . Model.Classes: Objects with the same attributes and behavior Person Objects Abstract Into Person Class Attributes: Name. Sex Operations: Speak(). Stop().

Introduction to UML   UML Unified Modeling Language A graphical modeling language that can be used to represent artifacts of object oriented analysis. design and implementation. Was developed by Rational Software  Grady Booch. It is a standard that has international support. Jim Rumbaugh and Ivar Jacobson   13 .

0 14 .   Current version of UML is 2. 1996 Additional companies got involved.8  First Version of UML  1995 Object Management Group (OMG) agreed to make UML the standard.UML History  1994 Grady Booch and Jim Rumbaugh started at Rational creating the new notation  Grandy Booch Booch Method  Jim Rumbaugh Object Modeling Technique 1995   Ivar Jacobson Joined the team Object-Oriented Software Engineering 0.

UML notation will be introduced along with java to represent Object Oriented concepts.UML Notations  In the next few weeks. And That s your Assignment 15 .

Object Oriented Paradigm: Features  Encapsulation Data Abstraction Inheritance Polymorphism Association Persistence      16 .

Information can be manipulated through operations performed on the object/class interface to the class. This information hiding capability is called Encapsulation. Implementation is hidden from the user.Encapsulation  All information (attributes and methods) in an object oriented system is stored/hidden within objects/classes. Objects support Information Hiding Some attributes and methods can be hidden from the user.    17 .

Example message messag e accountN ame accountBa lance checkBalance() message 18 .Encapsulation .

private double accountBalance. public withdrawCash(). public depositCash().Encapsulation .Example  Only methods withdrawCash(). depositCash() and checkBalance() have access to (can read or modify) accountName and accountBalance. class Account { private String accountName. } // Class Account 19 . public checkBalance().

 done by defining new classes. having the properties of built in data types and more.  Example : Creating new classes Account and Circle creates new data types Account and Circle that can be used any application.Data Abstraction  The technique of creating new data types that are well suited to an application. 20 . It allows the creation of user defined data types.

21 . checkBalance().Abstraction . private double accountBalance. public public public } // Class withdrawCash().Example class Account { private String accountName. depositCash(). Account Creates a data type Account Account acctX. Account acctY.

Paren t Inherited capability Child 22 . Parent Class (Super Class) Child Class (Sub Class) Subclass inherits properties from the parent class.Inheritance    New data types (classes) can be defined as extensions to previously defined types.

Examples  Account Class  Account class in our example had two attributes : accountName and accountBalance.  A check account:  has attributes accountName and accountBalance.  In addition: number of cheques issued (numChecks).  Account is the Super Class and CheckAccount is the Sub Class. inheritence allows CheckAccount to be defined as a sub class of Account class  thus inheriting attributes accountName and accountBalance from the Account class. 23 .  Instead of creating a new class CheckAccount which has all three attributes. This attribute may not be applicable to all types of accounts.Inheritance .

Account CheckAccount 24 .Inheritance in UML  CheckingAccount is a subclass (inherited from ) of a parent class Account.

 Consider a Rectangle class.Inheritance .  Circle class and Rectangle class can be then be defined as sub classes of Shape class.Examples  Circle Class  Circle class in our example has attributes centre and radius. radius in not an applicable attribute.  The methods area().  However.  Define a parent class Shape. 25 .  Rectangle class should have attributes height and width.  Height and width not applicable to circle.  Do not define a new class Rectangle holding all three attributes and methods. circumference() and move() defines in the Circle Class are applicable to the Rectangle class. Can have an attribute centre.  Move common properties to class Shape.

parent class. Example : Rectangle and Circle method have a common method move(). these methods can be moved to a common class . which requires changing the centre coordinate.Uses of Inheritance . This allows reuse since the implementation is not repeated.Reuse  If multiple classes have common attributes/methods.  26 .

g.Specialisation  Specialised behaviour can be added to child classes. The implementation of area() method in the Circle class is different from the Rectangle class. In this case the behaviour will be implemented in the child class.  E.   27 .Uses of Inheritance . area() method in the child classes override defintions of the method in parent classes.

  28 . The Shape class provides a common interface where all 3 operations move(). area() operations are significantly different and have to be implemented in the respective classes.  move() operation is common to classes and can be implemented in parent.Uses of Inheritance  Common Interface Not all operations supported for Rectangle and Circle are the same. Some methods have common implementation and others don t.  circumference(). circumference() and area().

rotate90degrees() 29 . Rectangle class might have a special operation that may not be meaningful to the Circle class .Extension   Extend functionality of a class.g. Child class adds new operations to the parent class but does not change the inherited behaviour.Uses of Inheritance .  E.

Uses of Inheritance   Multiple Iinheritance Inherit properties from more than one class. This is called Multiple Inheritance. Graphics Shape Circle 30 .

Uses of Multiple Inheritance  This is done when a class resides in more than one inheritence heirarchy.  The class inherits behaviour from multiple parent classes. Eg. Circle class can inherit move() from the Shape class and paint() from the Graphics class. Multiple inheritance is not supported in JAVA but is supported in C++.   31 .

In OOP.Polymorphism  Polymorphic which means many forms  Poly many  Morphos . polymorphism itself has many forms. Polymorphism allows a single object.forms.   32 . operator to be defined differently depending on the type of data passed to it. method.

Create a new circle object Shape shape = circleA. The behaviour of the object will depend on the object passed.area() area() method for rectangle will be executed.Polymorphism  An object of type Circle or Rectangle can be assigned to a Shape object. 33 . circleA = new Circle().area(). shape. shape. Create a new rectangle object shape= rectangleA. area() for circle class will be executed rectangleA = new Rectangle().

4) Method 2 will be called.initialize(int a. initialize(2) Method 1 will be called.  34 . initialize(2. accepting different input arguments.initialize(int a) Method 2 . int b) The Appropriate method will be called based on the input arguments. Method 1 .Polymorphism  Method Overloading Multiple methods can be defined with the same name.

-. + operator for Circle can re-defined  Circle c = c + 2. 35 . / to have different meanings based on the type. *.Polymorphism  Operator Overloading Allows regular operators such as +. C++ does support it.  Not supported in JAVA.g. E.

contains> Circle Diagram contains> Rectangle 36 . This type of relationship is called an association. Example : The Circle and Rectangle objects may be contained in a Diagram class.Association   A class can maintain a relationship with another class which will allow the class to communicate with the other class. This creates and association between the Diagram class and Circle class and also Diagram and Rectangle class.

 37 .  A student can be an undergraduate or a graduate.A simple class diagram  A Class diagram shown classes and their relationships.  A teacher assistant is a type of graduate student who assists a teacher.  A tutor tutors a student.  A teacher and a professor are two types of instructors. Draw a simple class diagram for the following description.  An undergraduate student can be a type of tutor.

self-contained. and modular objects Maintainability  Modular objects make locating bugs easier.Why OOP?     Greater Reliability  Break complex software projects into small. with less impact on the overall project Greater Productivity through Reuse! Faster Design and Modelling 38 .