SlideShare a Scribd company logo
Name : Muhammad Asif Subhani
Email: asif.subhani@umt.edu.pk









Objects are key to understanding object-oriented
technology.
Look around right now and you'll find many examples of
real-world objects: your dog, your desk, your television
set, your bicycle.
Real-world objects share two characteristics: They all have
state and behavior.
Dogs have state (name, color, breed, hungry) and behavior
(barking, fetching, wagging tail).
Bicycles also have state (current gear, current pedal
cadence, current speed) and behavior (changing gear,
changing pedal cadence, applying brakes).
Identifying the state and behavior for real-world objects is
a great way to begin thinking in terms of object-oriented
programming.








Take a minute right now to observe the real-world objects
that are in your immediate area.
For each object that you see, ask yourself two questions:
"What possible states can this object be in?" and "What
possible behavior can this object perform?".
Make sure to write down your observations. As you do,
you'll notice that real-world objects vary in complexity;
your desktop lamp may have only two possible states (on
and off) and two possible behaviors (turn on, turn off), but
your desktop radio might have additional states (on, off,
current volume, current station) and behavior (turn on,
turn off, increase volume, decrease volume, seek, scan,
and tune).
You may also notice that some objects, in turn, will also
contain other objects. These real-world observations all
translate into the world of object-oriented programming.








Software objects are conceptually similar to realworld objects: they too consist of state and
related behavior.
An object stores its state in fields (variables in
some programming languages) and exposes its
behavior through methods (functions in some
programming languages).
Methods operate on an object's internal state and
serve as the primary mechanism for object-toobject communication.
Hiding internal state and requiring all interaction
to be performed through an object's methods is
known as data encapsulation — a fundamental
principle of object-oriented programming.






Consider a bicycle, for example:
A bicycle modeled as a software object.
By attributing state (current speed, current
pedal cadence, and current gear) and
providing methods for changing that state,
the object remains in control of how the
outside world is allowed to use it.
For example, if the bicycle only has 6 gears, a
method to change gears could reject any
value that is less than 1 or greater than 6






Bundling code into individual software objects
provides a number of benefits, including:
Modularity: The source code for an object can be
written and maintained independently of the
source code for other objects. Once created, an
object can be easily passed around inside the
system.
Information-hiding: By interacting only with an
object's methods, the details of its internal
implementation remain hidden from the outside
world.




Code re-use: If an object already exists (perhaps
written by another software developer), you can
use that object in your program. This allows
specialists to implement/test/debug complex,
task-specific objects, which you can then trust to
run in your own code.
Pluggability and debugging ease: If a particular
object turns out to be problematic, you can
simply remove it from your application and plug
in a different object as its replacement. This is
analogous to fixing mechanical problems in the
real world. If a bolt breaks, you replace it, not the
entire machine.









What Is a Class?
In the real world, you'll often find many individual
objects all of the same kind. There may be
thousands of other bicycles in existence, all of
the same make and model.
Each bicycle was built from the same set of
blueprints and therefore contains the same
components.
In object-oriented terms, we say that your bicycle
is an instance of the class of objects known as
bicycles.
A class is the blueprint from which individual
objects are created.
1.

class Bicycle {

2.

int cadence = 0;

3.

int speed = 0;

4.

int gear = 1;

5.

void changeCadence(int newValue) {
cadence = newValue;

6.
7.

}

8.

void changeGear(int newValue) {
gear = newValue;

9.
10.

}

11.

void speedUp(int increment) {
speed = speed + increment;

12.
13.

}

14.

void applyBrakes(int decrement) {
speed = speed - decrement;

15.
16.

}

17.

void printStates() {
System.out.println("cadence:" +

18.
19.

cadence + " speed:" +

20.

speed + " gear:" + gear);
}

21.
22.

}
1.

class BicycleDemo {
public static void main(String[] args) {

2.

3.

// Create two different

4.

// Bicycle objects

5.

Bicycle bike1 = new Bicycle();

6.

Bicycle bike2 = new Bicycle();

7.

// Invoke methods on

8.

// those objects

9.

bike1.changeCadence(50);

10.

bike1.speedUp(10);

11.

bike1.changeGear(2);

12.

bike1.printStates();

13.

bike2.changeCadence(50);

14.

bike2.speedUp(10);

15.

bike2.changeGear(2);

16.

bike2.changeCadence(40);

17.

bike2.speedUp(10);

18.

bike2.changeGear(3);

19.

bike2.printStates();
}

20.
21.

}


The output of this test prints the ending pedal cadence, speed, and gear for
the two bicycles:
cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3
Different kinds of objects often have a certain amount in
common with each other.
 Mountain bikes, road bikes, and tandem bikes, for example,
all share the characteristics of bicycles (current speed, current
pedal cadence, current gear).
 Yet each also defines additional features that make them
different:
 Tandem bicycles have two seats and two sets of handlebars;
 Road bikes have drop handlebars;
 Mountain bikes have an additional chain ring, giving them a
lower gear ratio.






Object-oriented programming allows classes to inherit
commonly used state and behavior from other classes.
In this example, Bicycle now becomes the superclass of
MountainBike, RoadBike, and TandemBike.
In the Java programming language, each class is allowed to
have one direct superclass, and each superclass has the
potential for an unlimited number of subclasses:


1.
2.
3.
4.
5.




The syntax for creating a subclass is simple. At the beginning
of your class declaration, use the extends keyword, followed
by the name of the class to inherit from:
class MountainBike extends Bicycle
{
// new fields and methods defining
// a mountain bike would go here
}
This gives MountainBike all the same fields and methods as
Bicycle, yet allows its code to focus exclusively on the
features that make it unique.
This makes code for your subclasses easy to read. However,
you must take care to properly document the state and
behavior that each superclass defines, since that code will not
appear in the source file of each subclass.







As you've already learned, objects define their
interaction with the outside world through
the methods that they expose.
Methods form the object's interface with the
outside world;
The buttons on the front of your television
set, for example, are the interface between
you and the electrical wiring on the other side
of its plastic casing.
You press the "power" button to turn the
television on and off.




Interfaces form a contract between the class
and the outside world, and this contract is
enforced at build time by the compiler.
If your class claims to implement an interface,
all methods defined by that interface must
appear in its source code before the class will
successfully compile.


In its most common form, an interface is a
group of related methods with empty bodies.
A bicycle's behavior, if specified as an
interface, might appear as follows:
1.
2.
3.
4.
5.

6.
7.
8.

interface Bicycle
{
// wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
To implement this interface, the name of your
class would change (to a particular brand of
bicycle, for example, such as ACMEBicycle),
and you'd use the implements keyword in the
class declaration:
1. class ACMEBicycle implements Bicycle
2. {
3.
// remainder of this class
4.
// implemented as before
5. }



Implementing an interface allows a class to
become more formal about the behavior it
promises to provide.






A package is a namespace that organizes a set of
related classes and interfaces.
Conceptually you can think of packages as being
similar to different folders on your computer.
You might keep movies in one folder, images in
another, and scripts or applications in yet
another.
Because software written in the Java programming
language can be composed of hundreds or
thousands of individual classes, it makes sense to
keep things organized by placing related classes
and interfaces into packages.









The Java platform provides an enormous class library (a set of
packages) suitable for use in your own applications.
This library is known as the "Application Programming Interface",
or "API" for short.
Its packages represent the tasks most commonly associated with
general-purpose programming.
For example, a String object contains state and behavior for
character strings;
File object allows a programmer to easily create, delete, inspect,
compare, or modify a file on the filesystem;
Socket object allows for the creation and use of network sockets;
GUI objects control buttons and checkboxes and anything else
related to graphical user interfaces.
There are literally thousands of classes to choose from. This
allows you, the programmer, to focus on the design of your
particular application, rather than the infrastructure required to
make it work.




Java Platform API Specification contains the
complete listing for all packages, interfaces,
classes, fields, and methods supplied by the
Java SE platform.
As a programmer, it will become your single
most important piece of reference
documentation.

More Related Content

Viewers also liked

Opkr40 007 b chasis & suspension (13)
Opkr40 007 b chasis & suspension (13)Opkr40 007 b chasis & suspension (13)
Opkr40 007 b chasis & suspension (13)Eko Supriyadi
 
Year10 biochem1
Year10 biochem1Year10 biochem1
Year10 biochem1
luborrelli
 
Would you rather
Would you ratherWould you rather
Would you rather
DavidTizzard
 
Позакласний захід
Позакласний західПозакласний захід
Позакласний західOlga_Pidkivka
 
Pagine da gips6
Pagine da gips6Pagine da gips6
C言語超入門
C言語超入門C言語超入門
C言語超入門
Mercury Soft
 
Politisk konsulent til Den Socialdemokratiske Folketingsgruppe.
Politisk konsulent til Den Socialdemokratiske Folketingsgruppe.Politisk konsulent til Den Socialdemokratiske Folketingsgruppe.
Politisk konsulent til Den Socialdemokratiske Folketingsgruppe.Sidsel Diemer Leonhardt
 

Viewers also liked (7)

Opkr40 007 b chasis & suspension (13)
Opkr40 007 b chasis & suspension (13)Opkr40 007 b chasis & suspension (13)
Opkr40 007 b chasis & suspension (13)
 
Year10 biochem1
Year10 biochem1Year10 biochem1
Year10 biochem1
 
Would you rather
Would you ratherWould you rather
Would you rather
 
Позакласний захід
Позакласний західПозакласний захід
Позакласний захід
 
Pagine da gips6
Pagine da gips6Pagine da gips6
Pagine da gips6
 
C言語超入門
C言語超入門C言語超入門
C言語超入門
 
Politisk konsulent til Den Socialdemokratiske Folketingsgruppe.
Politisk konsulent til Den Socialdemokratiske Folketingsgruppe.Politisk konsulent til Den Socialdemokratiske Folketingsgruppe.
Politisk konsulent til Den Socialdemokratiske Folketingsgruppe.
 

Similar to 2 oop

Object Oriented Programing and JAVA
Object Oriented Programing and JAVAObject Oriented Programing and JAVA
Object Oriented Programing and JAVA
Gujarat Technological University
 
Java chapter 3 - OOPs concepts
Java chapter 3 - OOPs conceptsJava chapter 3 - OOPs concepts
Java chapter 3 - OOPs concepts
Mukesh Tekwani
 
COMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptxCOMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptx
FarooqTariq8
 
Introduction to OOP.pptx
Introduction to OOP.pptxIntroduction to OOP.pptx
Introduction to OOP.pptx
ParthaSarathiBehera9
 
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Sakthi Durai
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
Sakthi Durai
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Getting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular DeveloperGetting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular Developer
Fabrit Global
 
Hello Android
Hello AndroidHello Android
Hello Android
Trong Dinh
 
L9
L9L9
L9
lksoo
 
02 objective-c session 2
02  objective-c session 202  objective-c session 2
02 objective-c session 2
Amr Elghadban (AmrAngry)
 
Java oop concepts
Java oop conceptsJava oop concepts
Java oop concepts
Syeful Islam
 
Cs2305 programming paradigms lecturer notes
Cs2305   programming paradigms lecturer notesCs2305   programming paradigms lecturer notes
Cs2305 programming paradigms lecturer notes
Saravanakumar viswanathan
 
Chapter 1-Note.docx
Chapter 1-Note.docxChapter 1-Note.docx
Chapter 1-Note.docx
TigistTilahun1
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Bill Buchan
 
CSc investigatory project
CSc investigatory projectCSc investigatory project
CSc investigatory project
DIVYANSHU KUMAR
 
OOP programming for engineering students
OOP programming for engineering studentsOOP programming for engineering students
OOP programming for engineering students
iaeronlineexm
 

Similar to 2 oop (20)

Object Oriented Programing and JAVA
Object Oriented Programing and JAVAObject Oriented Programing and JAVA
Object Oriented Programing and JAVA
 
Java chapter 3 - OOPs concepts
Java chapter 3 - OOPs conceptsJava chapter 3 - OOPs concepts
Java chapter 3 - OOPs concepts
 
COMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptxCOMP111-Week-1_138439.pptx
COMP111-Week-1_138439.pptx
 
Introduction to OOP.pptx
Introduction to OOP.pptxIntroduction to OOP.pptx
Introduction to OOP.pptx
 
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Getting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular DeveloperGetting Started with React, When You’re an Angular Developer
Getting Started with React, When You’re an Angular Developer
 
Hello Android
Hello AndroidHello Android
Hello Android
 
L9
L9L9
L9
 
02 objective-c session 2
02  objective-c session 202  objective-c session 2
02 objective-c session 2
 
Java oop concepts
Java oop conceptsJava oop concepts
Java oop concepts
 
Cs2305 programming paradigms lecturer notes
Cs2305   programming paradigms lecturer notesCs2305   programming paradigms lecturer notes
Cs2305 programming paradigms lecturer notes
 
Class 1 blog
Class 1 blogClass 1 blog
Class 1 blog
 
Class 1
Class 1Class 1
Class 1
 
Ad507
Ad507Ad507
Ad507
 
Chapter 1-Note.docx
Chapter 1-Note.docxChapter 1-Note.docx
Chapter 1-Note.docx
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
 
CSc investigatory project
CSc investigatory projectCSc investigatory project
CSc investigatory project
 
OOP programming for engineering students
OOP programming for engineering studentsOOP programming for engineering students
OOP programming for engineering students
 

Recently uploaded

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 

2 oop

  • 1.
  • 2. Name : Muhammad Asif Subhani Email: asif.subhani@umt.edu.pk
  • 3.       Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
  • 4.      Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?". Make sure to write down your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.
  • 5.
  • 6.     Software objects are conceptually similar to realworld objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-toobject communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.
  • 7.     Consider a bicycle, for example: A bicycle modeled as a software object. By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6
  • 8.
  • 9.    Bundling code into individual software objects provides a number of benefits, including: Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system. Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
  • 10.   Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
  • 11.      What Is a Class? In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.
  • 12. 1. class Bicycle { 2. int cadence = 0; 3. int speed = 0; 4. int gear = 1; 5. void changeCadence(int newValue) { cadence = newValue; 6. 7. } 8. void changeGear(int newValue) { gear = newValue; 9. 10. } 11. void speedUp(int increment) { speed = speed + increment; 12. 13. } 14. void applyBrakes(int decrement) { speed = speed - decrement; 15. 16. } 17. void printStates() { System.out.println("cadence:" + 18. 19. cadence + " speed:" + 20. speed + " gear:" + gear); } 21. 22. }
  • 13. 1. class BicycleDemo { public static void main(String[] args) { 2. 3. // Create two different 4. // Bicycle objects 5. Bicycle bike1 = new Bicycle(); 6. Bicycle bike2 = new Bicycle(); 7. // Invoke methods on 8. // those objects 9. bike1.changeCadence(50); 10. bike1.speedUp(10); 11. bike1.changeGear(2); 12. bike1.printStates(); 13. bike2.changeCadence(50); 14. bike2.speedUp(10); 15. bike2.changeGear(2); 16. bike2.changeCadence(40); 17. bike2.speedUp(10); 18. bike2.changeGear(3); 19. bike2.printStates(); } 20. 21. }
  • 14.  The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles: cadence:50 speed:10 gear:2 cadence:40 speed:20 gear:3
  • 15. Different kinds of objects often have a certain amount in common with each other.  Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear).  Yet each also defines additional features that make them different:  Tandem bicycles have two seats and two sets of handlebars;  Road bikes have drop handlebars;  Mountain bikes have an additional chain ring, giving them a lower gear ratio.
  • 16.    Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:
  • 17.
  • 18.  1. 2. 3. 4. 5.   The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from: class MountainBike extends Bicycle { // new fields and methods defining // a mountain bike would go here } This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass.
  • 19.     As you've already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object's interface with the outside world; The buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off.
  • 20.   Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
  • 21.  In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:
  • 22. 1. 2. 3. 4. 5. 6. 7. 8. interface Bicycle { // wheel revolutions per minute void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); }
  • 23. To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration: 1. class ACMEBicycle implements Bicycle 2. { 3. // remainder of this class 4. // implemented as before 5. } 
  • 24.  Implementing an interface allows a class to become more formal about the behavior it promises to provide.
  • 25.     A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep movies in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
  • 26.         The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface", or "API" for short. Its packages represent the tasks most commonly associated with general-purpose programming. For example, a String object contains state and behavior for character strings; File object allows a programmer to easily create, delete, inspect, compare, or modify a file on the filesystem; Socket object allows for the creation and use of network sockets; GUI objects control buttons and checkboxes and anything else related to graphical user interfaces. There are literally thousands of classes to choose from. This allows you, the programmer, to focus on the design of your particular application, rather than the infrastructure required to make it work.
  • 27.   Java Platform API Specification contains the complete listing for all packages, interfaces, classes, fields, and methods supplied by the Java SE platform. As a programmer, it will become your single most important piece of reference documentation.