introduction to oop lec one
TRANSCRIPT
-
8/9/2019 Introduction to OOP Lec One
1/23
Advance Programming Using Java
Designed by : Atif Azeem Khan
Introduction to JavaIntroduction to JavaLecture One
-
8/9/2019 Introduction to OOP Lec One
2/23
Applied Programming Using Java
ObjectivesObjectives
What is OOPsWhat is OOPs
Classes and ObjectsClasses and Objects
Difference between Object & ClassDifference between Object & Class
Class and Object Methods and DataClass and Object Methods and Data What is Data Abstraction & Encapsulation?What is Data Abstraction & Encapsulation?
What is Polymorphism?What is Polymorphism?
Graphical representation of Class & ObjectGraphical representation of Class & Object
Constant & variable data membersConstant & variable data members
-
8/9/2019 Introduction to OOP Lec One
3/23
What is OOPsWhat is OOPs
OOP stands for Object Oriented ProgrammingOOP stands for Object Oriented Programmingis a programming paradigm that usesis a programming paradigm that usesabstraction to create models based on the realabstraction to create models based on the realworld. It provides for better modelling of theworld. It provides for better modelling of thereal world object into computer systemreal world object into computer system
The power of objectThe power of object--oriented systems lies inoriented systems lies in
their promise of code reuse which will increasetheir promise of code reuse which will increaseproductivity, reduce costs and improveproductivity, reduce costs and improvesoftware qualitysoftware quality
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
4/23
Why OOPs ?Why OOPs ?
A major factor in the invention of OOPs approach isA major factor in the invention of OOPs approach isto remove some of the flaws encountered with theto remove some of the flaws encountered with theProcedural/Structured approach.Procedural/Structured approach. In OOP, data isIn OOP, data istreated as a critical element and does not allow it totreated as a critical element and does not allow it to
flow freely. It bounds data closely to theflow freely. It bounds data closely to the functionsfunctionsthat operate on it and protects it from accidentalthat operate on it and protects it from accidentalmodification from outside functions.modification from outside functions. OOP allowsOOP allowsdecomposition ofdecomposition of a problem into a number ofa problem into a number ofentities called objects and then builds data andentities called objects and then builds data and
functions around these objects.functions around these objects.A major advantageA major advantageof OOP is code reusability.of OOP is code reusability.
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
5/23
Advantages of OOPsAdvantages of OOPs
Some important features of Object OrientedSome important features of Object Orientedprogrammingprogramming are as follows:are as follows:
Emphasis on data rather than procedure.Emphasis on data rather than procedure. OOP has been hailed for its naturalness; understand ability,OOP has been hailed for its naturalness; understand ability,
extensibility, reusability, and easeextensibility, reusability, and ease--ofof--maintenance.maintenance. Programs are divided into Objects.Programs are divided into Objects. Data is hidden and cannot be accessed by external functions.Data is hidden and cannot be accessed by external functions.
Objects canObjects can communicatecommunicate with each other through functions.with each other through functions. New data and functions can be easily added wheneverNew data and functions can be easily added whenever
necessary.necessary. Follows bottomFollows bottom--up approach.up approach.
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
6/23
Advantages of OOPsAdvantages of OOPs
ObjectObject--Oriented Programming has the followingOriented Programming has the followingadvantages over conventional approaches:advantages over conventional approaches: OOP provides a clear modular structure for programsOOP provides a clear modular structure for programs
which makes it good for defining abstract data typeswhich makes it good for defining abstract data types
where implementation details are hidden and the unitwhere implementation details are hidden and the unithas a clearly defined interface.has a clearly defined interface.
OOP makes it easy to maintain and modify existing codeOOP makes it easy to maintain and modify existing codeas new objects can be created with small differences toas new objects can be created with small differences toexisting ones.existing ones.
OOP provides a good framework for code librariesOOP provides a good framework for code librarieswhere suppliedwhere supplied softwaresoftware components can be easilycomponents can be easilyadapted and modified by theadapted and modified by the programmerprogrammer . This is. This isparticularly useful for developing graphical userparticularly useful for developing graphical userinterfaces.interfaces.
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
7/23
Structure Vs OOPsStructure Vs OOPs
Program divided into smallProgram divided into smallsub programs calledsub programs calledfunctions. Data andfunctions. Data and
functions are separatefunctions are separatefunctions are the primaryfunctions are the primaryfocus. Different parts offocus. Different parts ofprogram has open access toprogram has open access todata. This follows the topdata. This follows the top
toto--bottom approach. Itbottom approach. Itinvolves writing programinvolves writing programtask specific.task specific.
Applied Programming Using Java
Program divided into differentProgram divided into differentclasses. Data and Functionsclasses. Data and Functionsare integrated into classes.are integrated into classes.OOPs focus is on Data. AccessOOPs focus is on Data. Access
is limited to data members.is limited to data members.OOPs follows the bottomOOPs follows the bottom--upupdesign It involves writing adesign It involves writing amodular program from specificmodular program from specificto general. Easy to modify andto general. Easy to modify and
reuse the code written once.reuse the code written once.
-
8/9/2019 Introduction to OOP Lec One
8/23
Concept of OOPsConcept of OOPs
ObjectsObjects
ClassesClasses
Data Abstraction and EncapsulationData Abstraction and EncapsulationInheritanceInheritance
PolymorphismPolymorphism
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
9/23
Data Abstraction & EncapsulationData Abstraction & Encapsulation
AbstractionAbstraction refers to the act of representingrefers to the act of representingessential features without including theessential features without including thebackground details orbackground details or explanations. Classesexplanations. Classes
use the concept of abstraction and are defineduse the concept of abstraction and are definedas a list of abstract attributes.as a list of abstract attributes.
Storing data and functions in a single unitStoring data and functions in a single unit(class) is(class) is encapsulation.encapsulation. Data cannot beData cannot be
accessible to the outside worldaccessible to the outside world and only thoseand only thosefunctions which are stored in the class canfunctions which are stored in the class canaccessaccess
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
10/23
InharitanceInharitance
InheritanceInheritance is the process by which objectsis the process by which objectscan acquire the properties of objects ofcan acquire the properties of objects of
other class. In OOP,other class. In OOP, inheritanceinheritance providesprovidesreusability, like, adding additional featuresreusability, like, adding additional featuresto an existing class without modifying it.to an existing class without modifying it.This is achieved by deriving a new classThis is achieved by deriving a new class
from the existing one. The new class willfrom the existing one. The new class willhave combined features of both the classes.have combined features of both the classes.
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
11/23
Object & ClassesObject & Classes
Two most important concepts in objectTwo most important concepts in object--oriented programming are the classes &oriented programming are the classes &objects. In broadest term, an object is aobjects. In broadest term, an object is athing, both tangible and intangible, that wething, both tangible and intangible, that wecan imagine and class is a sketch or designcan imagine and class is a sketch or designor a definition how the object will beor a definition how the object will be
created.created.
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
12/23
PolymorphismPolymorphism
PolymorphismPolymorphism means the ability to takemeans the ability to takemore than one form. An operation maymore than one form. An operation mayexhibit different behaviours inexhibit different behaviours indifferentdifferent instances. The behaviour dependsinstances. The behaviour dependson the data types used in the operation.on the data types used in the operation.Polymorphism is extensively used inPolymorphism is extensively used in
implementingimplementing Inheritance.Inheritance.
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
13/23
ClassClass
As explain earlier class isAs explain earlier class isand actual blueand actual blue--print of anprint of anobject that defines theobject that defines the
rules or standards that willrules or standards that willbe followed to createbe followed to createthose object. Wethose object. Werepresent the class inrepresent the class ingraphical representationgraphical representation
with a rectangle with classwith a rectangle with classname on topname on top
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
14/23
ObjectObject
Object could be any thingObject could be any thingthat we can imagine or seethat we can imagine or seeeither it is tangible oreither it is tangible orintangible. An object alwaysintangible. An object alwayshave somehave some propertiesproperties andandexhibit someexhibit some behaviourbehaviour
for example,for example,Tree, plane, cat, paper,Tree, plane, cat, paper,keyboard, car house, lightkeyboard, car house, lightbulb, table, chair all arebulb, table, chair all areobjects.objects.
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
15/23
Object ExampleObject Example
Every single object is comprised of someEvery single object is comprised of somedatadata (properties)(properties) and operationsand operations(behaviour)(behaviour)
For example,For example,A car has some propertiesA car has some properties (data)(data) like model,like model,colour, doors, type, registrationcolour, doors, type, registration--no, engine sizeno, engine sizeand extra . And car also do some functionsand extra . And car also do some functions(behaviour)(behaviour) like it start, run fast, run slow,like it start, run fast, run slow,breakdown, and extra.breakdown, and extra.
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
16/23
Object Graphical representationObject Graphical representation
We represent object with round edgedWe represent object with round edgedrectangle with name on toprectangle with name on top
Applied Programming Using Java
Object Car
moveFoward()
moveBackward()
-
8/9/2019 Introduction to OOP Lec One
17/23
Messages & Methods (Behaviour)Messages & Methods (Behaviour)
In objectIn object--oriented program we first defineoriented program we first defineclasses, and while the program is running weclasses, and while the program is running wecreate objects for these classes to accomplishcreate objects for these classes to accomplishtasks.tasks.
A task can range from adding two numbers toA task can range from adding two numbers tocomputing interest payment for a lone orcomputing interest payment for a lone orcalculating angle of a space shuttle.calculating angle of a space shuttle.
To instruct a class or an object we sendTo instruct a class or an object we sendmessages to it for example we send a messagemessages to it for example we send a messageof new car toof new car to class of carclass of car for an instance offor an instance ofobject carobject car..
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
18/23
For a class or an object to process that messageFor a class or an object to process that messageit must be programmed accordingly.it must be programmed accordingly.
For a class or an object to process message itFor a class or an object to process message itreceive, it must process a matching methodreceive, it must process a matching method(which is a sequence of instructions a class /(which is a sequence of instructions a class /object follows to perform a task).object follows to perform a task).
We define methods for classes & objects,We define methods for classes & objects,methods for classes calledmethods for classes called class methodsclass methodsandandmethods for object calledmethods for object called instance methodsinstance methods
Applied Programming Using Java
Messages & Methods (Behaviour)Messages & Methods (Behaviour)
-
8/9/2019 Introduction to OOP Lec One
19/23
Object Method ExampleObject Method Example
Lets say we have and object Account andLets say we have and object Account andwe have defined a method called deposit.we have defined a method called deposit.
Applied Programming Using Java
Account
deposit
250deposit
Deposit250
-
8/9/2019 Introduction to OOP Lec One
20/23
We send message toWe send message todeposit250 todeposit250 tomethod deposit andmethod deposit andmethod only depositmethod only depositit and does notit and does notreturn any thing toreturn any thing tosender. But somesender. But sometime we need totime we need to
information forminformation formclass or object inclass or object inthis casethis case
Applied Programming Using Java
Account
getMonthlyFee
Monthly fee
Object Method ExampleObject Method Example
-
8/9/2019 Introduction to OOP Lec One
21/23
Class Methods ExampleClass Methods Example
Lets say we have a method that will give usLets say we have a method that will give usthe average balance of all the objects thatthe average balance of all the objects thathave been instantiated for class accounthave been instantiated for class accountthen.then.
Applied Programming Using Java
Account
getAverageBalance
Average balance
You can not sendinstance message to
class
-
8/9/2019 Introduction to OOP Lec One
22/23
Class and Object Data Values orClass and Object Data Values or
PropertiesPropertiesLets remind the method deposit in AccountLets remind the method deposit in Account
instance(object) which receive ainstance(object) which receive amessage(value) form sender and aftermessage(value) form sender and afterreceiving where does it can store thatreceiving where does it can store thatvalue?value?
There comes the need for data member orThere comes the need for data member or
properties or data values.properties or data values.
Applied Programming Using Java
-
8/9/2019 Introduction to OOP Lec One
23/23
We canWe candefinedefinefollowingfollowing
datadatamembers formembers fora accounta account
class andclass andinstance.instance.
Applied Programming Using Java
Class and Object Data Values orClass and Object Data Values or
PropertiesProperties100
6427
Account
100 100100
This is a lock icon toindicate that a constant
value cannot change
Minimum balance
Account prefix
Opening BalanceOpening Balance Opening Balance
185.91558.91204.04
Current balance Current balance Current balance
Atifs AccToms AccAlis Acc