Object Oriented Concept

Preview:

Citation preview

By D .Nayanathara (BSc in MIS)

Object Oriented Concept

• What is OOP

• What is a Class & a Object

• What is Method

• Principles of OOP

• Abstraction

• Inheritance

• Encapsulation

• Polymorphism

Content

• OOP promote the component based approached to software development.

• Implementation details are hidden within the class

• Encapsulate data (attributes) and functions(behavior) into packages called classes . Class are re-useable

• Can add new functionally to program with out affection others functions.

• Ex: Java , C++

What is OOP

• Class is like a blueprint , provide the characteristic of an object • Ex : Car Class

Class

Car

YearNameModelColorNumber of doors

start()stop()changeGears()accelerate()decelerate()turn()break()

Attributes/Properties

Behaviors/Methods

Class Name

House Plan

• Instantiation of the class (objects are create from the class)

• Also called as Instance

• Object is a collection of data based on structure of the class

• One class can create multiple objects• Ex : Built the house according to blueprint

Create Object Using Car Class

Object

myAudiA3 : Car

Year = “2007”Name = “AudiA3”Model = “A3Diesel”Color = “Black”Number of doors = “4”

Object Name Class Name

• Method describes the behavior of an object

• Within a program, a method usually affect only one particular object

• Ex : Accelerate() method in car class• Every car can accelerate but Program

only need to call method Accelerate()

Method

myAudiA3 : Car

Year = “2007”Name = “AudiA3”Model = “A3Diesel”Color = “Black”Number of doors = “4”

start()stop()changeGears()

accelerate()decelerate()turn()break()

• Data Abstraction• Defining a data type and Execute abstract(specific) actions

• Inheritance• Inherits attributes and behaviors from the parent class to its subclass

• Encapsulation• Hide the operation of the object • An object hides what it does from other object and from outside world

• Polymorphism• Poly : many , Morphism : Forms • Ability for data to be processed in more than one form

Principles of OOP

• Provide only the essential details and hide their implementation details from the clients of the class

• Ex : Working with something we know ,how to use without knowing how it works internally.

• Car

• Phone

• TV

Data Abstraction

Call Button

• Inherits attributes and behaviors from the parent class to its subclass

• Sub Class can also include its own attributes and behaviors.

• Mainly used for code reusability

• Derived Class has all the attributes and behaviors of the Base Class• Ex: Car Class

Inheritance

Derived Class/ Sub ClassParent Class/Base Class /

Super Class Derived

Ex : Car Class

Inheritance Cont.

Car

YearNameModelColorNumber of doors

start()stop()changeGears()accelerate()decelerate()turn()break()

audiA4Car

..Model = A4

…accelerate()….

audiA6Car

…Model = A6

…accelerate()….

audiA3Car

…..Model = A3

…accelerate()….

• The process of bundling data and functions together and hide the object implementation details from other object and from outside world.

• Protect the integrity of the object by preventing changes done by users to the internal data of a object , provides safety and ensure system robustness .Ex : Bank Account Class • Ex :

• Encapsulation hides the internal state of the object from others and it conceals the details

• Accelerate method defines how accelerate occurs

• How Burns the fuel

• Generate Electricity etc. .

• When driving a car drive doesn’t need to knows the technical details about how accelerate has done

• When driving a car drive doesn’t need to knows the technical details

Encapsulation – Information hiding

• Poly : many , Morphism : Forms

• Ability for data to be processed in more than one form

• A Single function can used to handle different number and different type of arguments

• Ex : Break method in Car class

• Draw method in Shape Class

Polymorphism

Ex : Car Class

Polymorphism Cont ..

Car

…..

…..break()…..

audiA4Car

…..

…break()….

audiA6Car

…..

…break()….

audiA3Car

…..

…break()….

• Classes “audiA3Car” , “audiA6Car” and “audiA4Car” inherited the method called break() from the Car class(parent class)

• Each class produced different results after executing break method• Ex : “audiA3Car” may brake the rate 000 feet per second , Whereas “audiA6Car”

may brake the rate 111 feet per second …

Ex : Draw method in Shape Class

Polymorphism Cont ..

Shape

draw()

Shape

draw(box)

Shape

draw(circle)

Shape

draw(triangle)

Recommended