java fir stoop

39
1 Introduction to OO Concepts and UML

Upload: haroonaliabbasi

Post on 09-Apr-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 1/38

1

Introduction to OO Concepts andUML

Page 2: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 2/38

2

Overview

Introduction to Classes and Objects

Introduction to UML

Features of Object Oriented Paradigm

Encapsulation

Data Abstraction

Inheritance

Polymorphism

 Association

Persistence

Page 3: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 3/38

3

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 andtherefore more general purpose.

 Abstraction is the fundamental concept that differentiatesprocedural programming languages such as C from OO languagessuch as Java, C++.

Page 4: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 4/38

4

 Abstraction in Procedural Languages

 Abstraction in procedural languages is provided through functionsor procedures.

Functions modify external data by performing a particularoperation.

e.g. A c function to calculate the average of two numbers.

float calculate_average (float a, float b) {

float result;

result = (a + b)/2;

return result;

}

Page 5: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 5/38

5

 Abstraction in OO Languages

 Abstraction in OO languages is provided through an abstract datatype (ADT), which contains data and procedures that operate ondata.

 Abstract Data Type (ADT) is a definition that contains data and

procedures that operate on the data. In Java, class is an implementation on an abstract data type.

In OO terminology;

data is referred to as fields, parameters or attributes

procedures are referred to as methods or operations

 A class contains attributes and methods that operate on theattributes.

 A class is a generalization of a real world entity that is used in aOO program depending on the application.

e.g. animal, student, car, account etc.

Page 6: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 6/38

6

 Account Class Example

account name

 Account 

account number

withdraw cash

deposit cash

check account balance

Data

Methods

Page 7: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 7/38

7

 Account Class

// Pseudo Code for Account class

class Account {

String accountName;double accountBalance;

withdrawCash();

depositCash();

checkBalance();} // Class Account

Page 8: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 8/38

8

Circle Class Example

centre

Circle

radius

area

move

circumference

Data

Methods

Page 9: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 9/38

9

Circle Class

// Pseudo Code for Circle class

class Circle {

double cetreX, centreY;double radius;

area();

circumference();

move();

} // Class Circle

Page 10: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 10/38

10

Class vs Object 

 An Object is an instance of a class.e.g. The object: Zahids Account is an instance of Account class.

The object: Zahids Account  is another instance of the Account class.

Objects hold state information while classes do not.e.g. Zahids Account 

 Account Name = Zahid Account Balance = 15,000

 A class represents a template for several objects that havecommon properties.

 An Object Oriented system is a collection of interacting Objects.

Page 11: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 11/38

11

Examples of Objects

Figure 1.9: Examples of objects

CAR

VDU

BOY GIRL

TREEBOOK

CLOCK

TRIANGLE

Page 12: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 12/38

12

Classes: Objects with the sameattributes and behaviorPerson Objects

Vehicle Objects

Polygon Objects

Abstract Person Class

Attributes:

Operations:

Name, Age, Sex

Speak(), Listen(), Walk()Into

Abstract Vehicle Class

Attributes:

Operations:

Name, Model, Color

Start(), Stop(), Accelerate()Into

AbstractPolygon Class

Attributes:

Operations: Draw(), Erase(), Move()

Vertices, Border,

Color, FillColorInto

Figure 1.12: Objects and classes

Page 13: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 13/38

13

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.

It is a standard that has international support.

Was developed by Rational Software

Grady Booch, Jim Rumbaugh and Ivar Jacobson

Page 14: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 14/38

14

UML History

1994 Grady Booch and Jim Rumbaugh started at Rationalcreating the new notation

Grandy Booch Booch Method

Jim Rumbaugh Object Modeling Technique

1995 Ivar Jacobson Joined the team Object-Oriented Software Engineering

First Version of UML 0.8

1995 Object Management Group (OMG) agreed to make UMLthe standard.

1996 Additional companies got involved.

Current version of UML is 2.0

Page 15: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 15/38

15

UML Notations

In the next few weeks, UML notation will be introduced alongwith java to represent Object Oriented concepts.

 And Thats your Assignment 

Page 16: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 16/38

Page 17: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 17/38

17

Encapsulation

 All information (attributes and methods) in an object orientedsystem is stored/hidden within objects/classes.

Information can be manipulated through operations performed on

the object/class interface to the class. Implementation is hiddenfrom the user.

Objects support Information Hiding  Some attributes andmethods can be hidden from the user.

This information hiding capability is called Encapsulation.

Page 18: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 18/38

18

Encapsulation - Example

accountName

accountBalance

checkBalance()

message

message

message

Page 19: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 19/38

19

Encapsulation - Example

Only methods withdrawCash(), depositCash() and checkBalance()have access to (can read or modify) accountName andaccountBalance.

class Account {

private String accountName;

private double accountBalance;

public withdrawCash();

public depositCash();

public checkBalance();

} // Class Account

Page 20: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 20/38

20

Data Abstraction

The technique of creating new data types that are well suited to anapplication.

done by defining new classes.

It allows the creation of user defined data types, having theproperties of built in data types and more.

Example : Creating new classes Account and Circle creates new datatypes Account and Circle that can be used any application.

Page 21: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 21/38

21

class Account {

private String accountName;

private double accountBalance;

public withdrawCash();

public depositCash();

public checkBalance();

} // Class Account

 Abstraction - Example

Creates a datatype Account 

 Account acctX; Account acctY;

Page 22: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 22/38

22

Inheritance

New data types (classes) can be defined as extensions topreviously defined types.

Parent Class (Super Class) Child Class (Sub Class)

Subclass inherits properties from the parent class.

Parent 

Child

Inheritedcapability

Page 23: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 23/38

23

Inheritance - 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). This

attribute may not be applicable to all types of accounts.

Instead of creating a new class CheckAccount which has allthree attributes, inheritence allows CheckAccount to be definedas a sub class of Account class

thus inheriting attributes accountName and accountBalancefrom the Account class.

 Account is the Super Class and CheckAccount is the Sub Class.

Page 24: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 24/38

24

Inheritance in UML

CheckingAccount is a subclass (inherited from ) of a parent class  Account.

 Account 

CheckAccount 

Page 25: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 25/38

25

Inheritance - Examples

Circle Class

Circle class in our example has attributes centre and radius.

Consider a Rectangle class. Can have an attribute centre.

However, radius in not an applicable attribute.

Rectangle class should have attributes height and width. Height and width not applicable to circle.

The methods area(), circumference() and move() defines inthe Circle Class are applicable to the Rectangle class.

Do not define a new class Rectangle holding all three attributesand methods.

Define a parent class Shape.

Move common properties to class Shape.

Circle class and Rectangle class can be then be defined assub classes of Shape class.

Page 26: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 26/38

26

Uses of Inheritance - Reuse

If multiple classes have common attributes/methods, thesemethods can be moved to a common class - parent class.

This allows reuse since the implementation is not repeated.

Example : Rectangle and Circle method have a common method move(),which requires changing the centre coordinate.

Page 27: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 27/38

27

Uses of Inheritance - Specialisation

Specialised behaviour can be added to child classes.

In this case the behaviour will be implemented in the child class.

E.g. The implementation of area() method in the Circle class isdifferent from the Rectangle class.

area() method in the child classes override defintions of themethod in parent classes.

Page 28: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 28/38

28

Uses of Inheritance Common Interface

Not all operations supported for Rectangle and Circle are thesame.

Some methods have common implementation and others dont. move() operation is common to classes and can be

implemented in parent. circumference(), area() operations are significantly different 

and have to be implemented in the respective classes.

The Shape class provides a common interface where all 3 operations move(), circumference() and area().

Page 29: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 29/38

29

Uses of Inheritance - Extension

Extend functionality of a class.

Child class adds new operations to the parent class but does not change the inherited behaviour.

E.g. Rectangle class might have a special operation that maynot be meaningful to the Circle class - rotate90degrees()

Page 30: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 30/38

30

Uses of Inheritance Multiple Iinheritance

Inherit properties from more than one class.

This is called Multiple Inheritance.

Shape

Circle

Graphics

Page 31: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 31/38

31

Uses of Multiple Inheritance

This is done when a class resides in more than one inheritenceheirarchy.

The class inherits behaviour from multiple parent classes.

Eg. Circle class can inherit move() from the Shape class andpaint() from the Graphics class.

Multiple inheritance is not supported in JAVA but is supported inC++.

Page 32: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 32/38

32

Polymorphism

Polymorphic which means many forms Poly many Morphos - forms.

In OOP, polymorphism itself has many forms.

Polymorphism allows a single object, method, operator to bedefined differently depending on the type of data passed to it.

Page 33: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 33/38

33

Polymorphism

 An object of type Circle or Rectangle can be assigned to a Shapeobject. The behaviour of the object will depend on the object passed.

circleA = new Circle(); Create a new circle object 

Shape shape = circleA;shape.area(); area() for circle class will be executed

rectangleA = new Rectangle(); Create a new rectangle object 

shape= rectangleA;shape.area() area() method for rectangle will be executed.

Page 34: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 34/38

34

Polymorphism Method Overloading

Multiple methods can be defined with the same name, acceptingdifferent input arguments.

Method 1 - initialize(int a)

Method 2 - initialize(int a, int b)

The Appropriate method will be called based on the input arguments.

initialize(2) Method 1 will be called.

initialize(2,4) Method 2 will be called.

Page 35: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 35/38

35

Polymorphism Operator Overloading

 Allows regular operators such as +, -, *, / to have different meanings based on the type.

E.g. + operator for Circle can re-defined

Circle c = c +2;

Not supported in JAVA. C++ does support it.

Page 36: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 36/38

36

 Association

 A class can maintain a relationship with another class which willallow the class to communicate with the other class.

This type of relationship is called an association.

Example :

The Circle and Rectangle objects may be contained in a Diagram

class. This creates and association between the Diagram class and

Circle class and also Diagram and Rectangle class.

Circle

Rectangle

Diagram

contains>

contains>

Page 37: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 37/38

37

 A simple class diagram

 A Class diagram shown classes and their relationships.

Draw a simple class diagram for the following description.

 A student can be an undergraduate or a graduate.

 An undergraduate student can be a type of tutor.

 A tutor tutors a student.

 A teacher and a professor are two types of instructors.

 A teacher assistant is a type of graduate student who assists a

teacher.

Page 38: Java Fir Stoop

8/8/2019 Java Fir Stoop

http://slidepdf.com/reader/full/java-fir-stoop 38/38

38

Why OOP?

Greater Reliability

Break complex software projects into small, self-contained, andmodular objects

Maintainability

Modular objects make locating bugs easier, with less impact onthe overall project 

Greater Productivity through Reuse!

Faster Design and Modelling