Java How to Program, 9/e
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
• Download JDK 17 (or any latest release):
https://download.oracle.com/java/17/latest/jdk-
17_windows-x64_bin.exe
• Download IntelliJ IDEA :
https://www.jetbrains.com/idea/download/?
section=windowshttps://www.jetbrains.com/idea/
download/?section=windows
• Git for Windows: https://gitforwindows.org/
©1992-2012 by Pearson Education,
Inc. All Rights Reserved.
• Lectures:
https://drive.google.com/drive/folders/1_4DRaVSCRQW
BxdhrlChnAqPCp7ZtbLCy?usp=sharing
• Books:
https://drive.google.com/drive/folders/1XI5DDz09ivYSu3
MZfLd9pz8yEiYxFKN2?usp=sharing
• Labs:
https://drive.google.com/drive/folders/1Y4vHVFOCD67_
RIAYxDNmq7XwHGvTs_TP?usp=sharing
• GitHub link: https://github.com/aksamiftikhar/oop-
bsai
©1992-2012 by Pearson Education,
Inc. All Rights Reserved.
Visit my office for discussing
Any topic you studied in the class
Problems you are facing
Any other issue for which you need counselling
Please do not ask to repeat the whole topic!
Instead, come prepared with your specific queries:
what exactly is creating problems for you and why?
Office Hours (Room 24, Faculty Block)
See next slide
©1992-2012 by Pearson Education,
Inc. All Rights Reserved.
©1992-2012 by Pearson Education,
Inc. All Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
What is an object?
◦ In real world, any tangible (perceptible by touch) thing is an object.
◦ In software world, any tangible/intangible thing can be represented as an object.
Objects, or more precisely, the class objects, are essentially reusable
software components.
◦ There are date objects, time objects, audio objects, video objects, automobile
objects, people objects, etc.
◦ Almost any noun can be reasonably represented as a software object in terms of
attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving
and communicating).
Object-oriented programs are often easier to understand, correct and
modify.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Example: The Automobile as an Object
◦ Suppose you want to drive a car to make it go faster in some direction
◦ The automobile object interacts with other objects like pedal, brakes,
gears etc. for driving
◦ Pedal hides from the driver the complex mechanisms that actually make
the car go faster, just as the brake hides the mechanisms that slow the
car, and the steering wheel “hides” the mechanisms that turn the car.
◦ Such information hiding is an important characteristic of OOP
◦ Enables people with little or no knowledge of how engines, braking and
steering mechanisms work to drive a car easily.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
◦ Before you can drive a car, it must be built from the
engineering drawings that describe it.
Drawing – class
Build – create an object
Car – object
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
A completed car has an accelerator pedal to make the car go
faster, but even that’s not enough—the car won’t accelerate on
its own, so the driver must press the pedal to accelerate the car.
Methods and Classes
◦ Performing a task in a program requires a method.
◦ The method houses the program statements that actually perform its
tasks.
◦ Hides these statements from its user, just as the accelerator pedal of a
car hides from the driver the mechanisms of making the car go faster.
◦ In Java, we create a program unit called a class to house the set of
methods that perform the class’s tasks.
◦ A class is similar in concept to a car’s engineering drawings, which
house the design of an accelerator pedal, steering wheel, and so on.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Instantiation
◦ Just as someone has to build a car from its engineering
drawings before you can actually drive a car, you must build
an object of a class before a program can perform the tasks
that the class’s methods define.
◦ An object is then referred to as an instance of its class.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Reuse
◦ Just as a car’s engineering drawings can be reused many times to build
many cars, you can reuse a class many times to build many objects.
This is analogous to creating a function once and calling it many times with
different arguments to reuse it.
◦ Reuse of existing classes when building new classes and programs saves
time and effort.
◦ Reuse also helps you build more reliable and effective systems, because
existing classes and components often have gone through extensive
testing, debugging and performance tuning.
◦ Reusable classes are crucial to the software revolution that has been
made possible by object technology.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Messages and Methods Calls
◦ When you drive a car, pressing its gas pedal sends a message
to the car to perform a task—that is, to go faster.
◦ Similarly, you send messages to an object.
◦ Each message is implemented as a method call that tells a
method of the object to perform its task.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Attributes and Instance Variables
◦ Any feature of an object is called its attribute.
◦ A car has attributes
Color, its number of doors, current amount of gas in its tank, its
current speed, maximum speed and its record of total miles driven
(i.e., its odometer reading).
◦ Every car has its own attributes.
Car 1 has red color, 1000cc engine capacity.
Car 2 has black color; 1300cc engine capacity.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Encapsulation
◦ Classes encapsulate (i.e., wrap) attributes and methods into
objects—an object’s attributes and methods are intimately
related. You have to swallow this capsule altogether (as a
whole and not in isolation) to get something done.
◦ Objects may communicate with one another, but they’re
normally not allowed to know how other objects are
implemented—implementation details are hidden within the
objects themselves.
◦ This Abstraction or Information hiding, as we’ll see, is crucial
to good software engineering.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Inheritance
◦ Perhaps the most important but simple concept in OOP
◦ A new class of objects can be created quickly and conveniently
by inheritance—the new class absorbs the characteristics of an
existing class, possibly customizing them and adding unique
characteristics of its own.
◦ In our car analogy, an object of class “convertible” certainly is
an object of the more general class “automobile,” but more
specifically, the roof can be raised or lowered.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Object-Oriented Analysis and Design (OOAD)
◦ How will you create the code (i.e., the program instructions)
for your programs?
◦ Follow a detailed analysis process for determining your
project’s requirements (i.e., defining what the system is
supposed to do)
◦ Develop a design that satisfies them (i.e., deciding how the
system should do it).
◦ Carefully review the design (and have your design reviewed by
other software professionals) before writing any code.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
◦ Analyzing and designing your system from an object-oriented
point of view is called an object-oriented analysis and design
(OOAD) process.
◦ Languages like Java are object oriented.
◦ Object-oriented programming (OOP) allows you to implement
an object-oriented design as a working system.
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
The UML (Unified Modeling Language)
◦ The Unified Modeling Language (UML) is the most widely
used graphical scheme for modeling object-oriented systems.
◦ More to come soon about UML
©1992-2012 by Pearson Education, Inc. All
Rights Reserved.
Object-Oriented Programming helps in
modeling real-world scenarios.
- Objects as real-world entities
- Classes as blueprints
- Methods as actions
It allows us to create a more intuitive and
structured representation of the world
around us.
Code reusability is one of the major
advantages of OOP.
- Classes can be reused in different projects
- Inheritance allows extension of existing classes
This reduces development time and
increases productivity.
Code written in an object-oriented paradigm
is.
Modular
Less prone to errors
Easier to debug
Easier to modify and extend
This reduces development time and
increases productivity (although OOP
requires more boilerplate code.
1. Encapsulation: Bundling of data and methods
2. Abstraction: Hiding complex reality
3. Inheritance: Acquiring properties and methods
4. Polymorphism: Many shapes, one interface
These features make code more modular, easier to
manage, debug, and scale.