Notes
CHAPTER 1: INTRODUCTION TO OBJECT-
ORIENTED PROGRAMMING (OOP)
Objectives:
1. Define Object-Oriented Programming (OOP).
2. Explain OOP concepts and design (class).
3. Define an object’s elements (attributes and behaviors).
4. Define the characteristics of an OOP (abstraction, encapsulation, inheritance and
polymorphism).
1.1 INTRODUCTION TO OBJECT
A java program works by having objects that perform actions.
An object is a model of things, persons or situations that has a specific design in a program.
A program written in an object-oriented style will consist of objects instantiation (creation)
and interactions.
All objects of the same kind is said to be in the same class.
A class serves as a model of how an object will look like when the object is instantiated.
Example: Class and Objects
Class Objects
Car My car, your car, the neighbor’s car
Student Best student, student sitting next to you in class, yourself
Time Breakfast, lunch, dinner, meeting
In Java everything is an object (or representing a certain class).
1.2 ELEMENTS OF AN OBJECT: ATTRIBUTE, BEHAVIOR, STATE
For a program to create an object, we must provide a definition called a class.
Classes are essentially a template or blueprint for all instances of the class.
The class code also communicates to the compiler how to define, create, and interact with
objects of the class.
A class is an abstract data type that contains two parts:
1. attributes - data in an object (what an object has)
2. behaviors - Actions that the object perform (what an object can do)
1
Chapter 1 Introduction to Object-oriented Programming (OOP)
Notes
A class is a base from which an object is created.
A class must be defined before an object is created.
A Java class uses variables to define attributes and methods to define behaviors.
Example:
Object : Car
Attributes : manufacturer’s name, model name, year made, color numbers of doors,
size of engine
Methods : define data items (specify manufacturer’s name, model, year)
change a data item (colour, engine etc.)
display data items
calculate cost
Creating an Instance of a Class
Once you have created a class, you can create instances of the class (objects) in a Driver
Class or inside other Object Classes.
Instances (Objects):
• Inherit all attributes and methods defined in the class template.
• Interact independently of one another.
• Are reference objects.
• Are created using the new operator.
1.3 CHARACTERISTICS OF OOP: abstraction, encapsulation, inheritance, polymorphism
In object-oriented programs data is represented by objects.
Java is an object-oriented programming language therefore it works by having objects to
perform actions.
OOP Characteristics:
a. Abstraction ~ designing classes
the ability of the program to focus on the important matters and ignore some
aspects of the information it is manipulating
the creation of a software module containing relevant characteristics of the object to
be represented
b. Encapsulation ~ data hiding (private data)
the ability of the program to hide information about the implementation of an object
from its users
making object data inaccessible to non-member modules
Data hiding is important for several reasons.
2
Chapter 1 Introduction to Object-oriented Programming (OOP)
Notes
- It protects of attributes from accidental corruption by outside objects.
- It hides the details of how an object works, so the programmer can concentrate
on using it.
- It allows the maintainer of the object to have the ability to modify the internal
functioning of the object without “breaking” someone else's code.
c. Inheritance ~ use keyword of extends
the ability to create or define a new classes from existing classes
implement concept super class and subclass
d. Polymorphism ~ the use of abstract classes and virtual methods
The ability of an entity (object or method) to represent different things or actions
based on the context in which it is used.
Implement concept overloading and overriding
Advantages of OOP:
Simpler, easy to read programs
More efficient reuse of code
Faster time to market
More robust, error-free code
Example: Abstraction Concept
Her parents see her
Her teachers see her as their daughter.
as a student
Her classmates see Her siblings see her
her as their friend. as their sister.
A Student
Chapter 1 Introduction to Object-oriented Programming (OOP)
Notes
Example: Encapsulation Concept
Name, ID number and CGPA
are the part of this Student,
which cannot be or should not
be changed by other objects
such as his friends, parents,
teachers or siblings.
A Student
Example: Polymorphism Concept
study() in group:
study() at home: the the student discusses
student does revision with friends
study() in class:
the student listens to
study() in library: the lectures and take
student make references notes
A Student
Chapter 1 Introduction to Object-oriented Programming (OOP)
Notes
Example: Inheritance Concept
Name is a part of this
Person
A Person
Inherits
When a student inherits
(extends) a Person,
Name is automatically
becomes a part of this
student. This student
can also have other
attributes such as ID
number and CGPA. A
Student is a Person.
A Student
Chapter 1 Introduction to Object-oriented Programming (OOP)
Notes
No Characteristic Description
1. Simple
Java is partially modeled on C++, but greatly simplified and
improved
2. Dynamic Java was designed to adapt to an evolving environment. New
code can be loaded on the fly without recompilation.
3. Object
Oriented programming in Java is centered on creating objects, manipulating
objects, and making objects work together.
4. Distributed involves several computers working together on a network. Java is
designed to make distributed computing easy.
5. Interpreted Java programs are compiled into the Java Virtual Machine code
called bytecode, and it is machine independent.
6. Robust
Reliability because it puts a lot of emphasis on early checking for
possible errors. Java has a runtime exception-handling feature to
provide programming support for robustness.
Chapter 1 Introduction to Object-oriented Programming (OOP)
Notes
7. Secure
As an Internet programming language, Java is used in a
networked and distributed environment. Java implements
several security mechanisms to protect your system against
harm caused by stray program.
8. Portable
Java is architecture neutral. It can run in any platform without
being recompiled. The Java environment is portable to new
hardware and operating systems
9. Performance
Java’s performance is sometimes criticized compared to C++.
Because Java is interpreted, the bytecode is not directly executed by
the system, but is run through the interpreter.
10. Multithread
a program’s capability to perform several tasks simultaneously.
Chapter 1 Introduction to Object-oriented Programming (OOP)
Notes
Sample Program 1: Rectangle.java
public class Rectangle {
private double length;
private double width;
public void setLength(double len)
{
length = len;
}
public void setWidth(double wid)
{
width = wid;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
double area = length * width;
return area;
}
}
Program 2: TestRectangle.java
import javax.swing.*;
public class TestRectangle
{
public static void main(String[]args)
8
Chapter 1 Introduction to Object-oriented Programming (OOP)
Notes
{
Rectangle R = new Rectangle();
String str; double length, width;
str = JOptionPane.showInputDialog("Enter length :");
length = Double.parseDouble(str);
R.setLength(length);
str = JOptionPane.showInputDialog("Enter width :");
width = Double.parseDouble(str);
R.setWidth(width);
JOptionPane.showMessageDialog(null, "Length : " +R.getLength()
+"\nWidth : " +R.getWidth()
+"\nArea : " +R.getArea());
} }
Sample input:
Sample output:
Chapter 1 Introduction to Object-oriented Programming (OOP)