You are on page 1of 44

Google Classroom Code (2A):

BASIC CONCEPT OF
OBJECT-ORIENTED
PROGRAMMING
OBJECT ORIENTED PROGRAMMING
Object-Oriented Programming (OOP) refers to a type of
computer programming (software design) in
which programmers define the data type of a data structure,
and also the types of operations (functions) that can be
applied to the data structure.
In this way, the data structure becomes an object that
includes both data and functions. In addition, programmers
can create relationships between one object and another. For
example, objects can inherit characteristics from other
objects.
CONCEPT OF OOPs
■ It is necessary to understand some of the concepts used
extensively in object-oriented programming. These include:

✓ Object − Objects have states and behaviors. Example: A


dog has states - color, name, breed, as well as behavior
such as wagging their tail, barking, eating. An object is
an instance of a class.
✓ Class − A class can be defined as a template/blueprint
that describes the behavior/state that the object of its
type supports.
CONCEPT OF OOPs
✓ Methods − A method is basically a behavior. A class
can contain many methods. It is in methods where the
logics are written, data is manipulated and all the
actions are executed.
✓ Instance Variables − Each object has its unique set of
instance variables. An object's state is created by the
values assigned to these instance variables.
✓ Abstraction – The process of picking out (abstracting)
common features of objects and procedures.
✓ Inheritance – a feature that represents the "is a"
relationship between different classes.
CONCEPT OF OOPs
✓ Encapsulation – The process of combining elements to
create a new entity. A procedure is a type of
encapsulation because it combines a series of computer
instructions.
✓ Information hiding – The process of hiding details of an
object or function. Information hiding is a powerful
programming technique because it reduces complexity.
✓ Interface – The languages and codes that the
applications use to communicate with each other and
with the hardware.
CONCEPT OF OOPs
✓ Messaging – Message passing is a form of
communication used in parallel programming and
object-oriented programming.
✓ Polymorphism – A programming language's ability to
process objects differently depending on their data type
or class.
✓ Procedure – A section of a program that performs a
specific task.
BENEFITS OF OOPs
1. Re-usability
✓ It means reusing some facilities rather than building it again and
again. This is done with the use of a class. We can use it ‘n’
number of times as per our need.
2. Data Redundancy
✓ This is a condition created at the place of data storage (you can
say Databases) where the same piece of data is held in two
separate places. So the data redundancy is one of the greatest
advantages of OOP. If a user wants a similar functionality in
multiple classes he/she can go ahead by writing common class
definitions for the similar functionalities and inherit them.
BENEFITS OF OOPs
3. Code Maintenance
✓ This feature is more of a necessity for any programming
languages, it helps users from doing re-work in many ways. It is
always easy and time-saving to maintain and modify the existing
codes with incorporating new changes into it.
4. Security
✓ With the use of data hiding and abstraction mechanism, we are
filtering out limited data to exposure which means we are
maintaining security and providing necessary data to view.
CONCEPT OF OOPs
OBJECTS
Programming problem is analyzed in term of objects and
the nature of communication between them. Program objects
should be chosen such that they match closely with the real-
world objects. (Entity)

Representing an Object:
CONCEPT OF OOPs
OBJECTS
Programming problem is analyzed in term of objects and
the nature of communication between them. Program objects
should be chosen such that they match closely with the real-
world objects. (Entity)

Example #1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
CONCEPT OF OOPs
CLASSES
The entire set of data and code of an object can be made
a user-defined data type with the help of class. In fact, objects
are variables of the type class. Once a class has been defined,
we can create any number of objects belonging to that class.

✓ A class is thus a collection of objects similar types.


CONCEPT OF OOPs
States can be represented as instance
variables and behaviors as methods of the class.

So if you had to write a class based on states


and behaviors of House. It goes like this:
CONCEPT OF OOPs
CONCEPT OF OOPs

Example #2:
Object: Car
State: Color, Brand, Weight, Model
Behavior: Break, Accelerate, Slow Down, Gear change.
CONCEPT OF OOPs
A class can be considered as a blueprint using which you
can create as many objects as you like. For example, here we
have a class Website that has two data members (also known
as fields, instance variables and object states).
This is just a blueprint, it does not represent any website,
however using this we can create Website objects (or
instances) that represents the websites. We have created two
objects, while creating objects we provided separate
properties to the objects using constructor.
CONCEPT OF OOPs
CONCEPT OF OOPs
INHERITANCE
✓ Inheritance enables new classes to receive—or inherit—the
properties and methods of existing classes.
✓ Inheritance is a way to express a relationship between
blueprints (classes). It's a way of saying: I want to build a
new object that is similar to one that already exists, and
instead of creating the new class from scratch, I want to
reference the existing class and simply indicate what's
different.
INHERITANCE
In object-oriented programming, inheritance enables new
objects to take on the properties of existing objects. A class
that is used as the basis for inheritance is called
a superclass or base class.
A class that inherits from a superclass is called
a subclass or derived class. The terms parent class and child
class are also acceptable terms to use respectively. A child
inherits visible properties and methods from its parent while
adding additional properties and methods of its own.
INHERITANCE
TWO OBJECTS OF INHERITANCE
✓ Subclassing – making a new class based on a previous
one.
✓ Overriding – changing how a previous class works

You can organize your objects into a hierarchy. Using


inheritance to make this hierarchy often creates easier to
understand code, but most importantly it allows you to reuse
and organize code more effectively.
OBJECT-ORIENTED
PROGRAMMING
OBJECT-ORIENTED PROGRAMMING
✓A modular approach to computer programming and
software design.
✓An object-oriented program may be seen as
composed of objects that interacts with each other.
✓A procedural or structured program is seen as a set
of instructions to the computer.
WHAT IS AN OBJECT?
✓ An object combines data and code that acts on that data.
✓ May represent:
➢ Real world objects – Car, Person, Phone, Book, etc.
➢ Concepts – Time, Bank Account, Sale, etc.
✓ An object has:
➢ State – variables/data/attributes
➢ Behavior – operators/codes/methods
✓ Each object has its own data, through the code within a
class is shared for economy
EXAMPLE: CAR OBJECT
ATTRIBUTES:
✓ Current Gear = 1st
✓ Speed = 5kph
METHODS:
✓ Brake
✓ Shift gear
✓ Accelerate
✓ Back up
✓ Steer
EXAMPLE: BANK ACCOUNT
ATTRIBUTES:
✓ Name
✓ Number
✓ Balance
METHODS:
✓ Deposit
✓ Withdraw
✓ Close
✓ Get Balance
✓ Get Name
✓ Get Number
ENCAPSULATION
• Identifying an object’s attributes and methods and bundling
them together in structure called a class.
• Often related with information hiding.
-Keeping certain details of an object private.
-Programmers only need to know what input is required
and what outputs are expected from the object.
• Advantages
-Protects other parts of the program from change even if
the implementation of the object is changed.
-Protects the object’s implementation from being modified
by other parts of the program.
CLASSES
• A class is a blueprint or prototype that defines the
attributes and methods common to all objects of a
certain kind.
• Programmers can use a class repeatedly to create
numerous objects of a certain kind.
• Most basic structure of object-oriented programming.
CLASSES in JAVA
TERMINOLOGY
• Instance – an object created from a certain class.
-Class: Dog
-Instances/Objects of Dog: Snoopy, Pluto, Rin Tin Tin
• Instantiate – create a new object
• Member – general term referring to the contents of
an object.
-Attribute – fields or properties
-Method – functions or operations
OBJECT REFERENCES
• Consider the following example:
OBJECT REFERENCES
• If we assign str2 to str1,
DECLARING OBJECT REFERENCES
• SYNTAX:
-ClassName identifier;
• Examples:
File textFile;
String message;
Date payday;
CREATING OBJECTS
• To create an object or an instance of a class, we use
the new operator.
• For example, if you want to create an instance of the
class String, we write the following code,
String str = new String(“Hello World!”);
or also equivalent to,
String str = “Hello World!”;
• String class is a special (and only) class you can
create an instance without using new keyword as
shown above.
CREATING OBJECTS
• The new operator
-allocates memory for that object and returns a
reference of that memory location to you.
-when you create an object, you actually invoke the
class constructor.
• The constructor
-is a method where you place all the initializations
-has the same name as the class
CLASSES VS OBJECTS
USING OBJECTS
• Accessing attributes
• Calling methods
ACCESSING ATTRIBUTES
• Syntax
objectName.attribute;
• Examples;
car porsche = new car();
porsche.speed = 100;
porsche.currentGear = 3;
CALLING INSTANCE METHODS
• Method
-is a separate piece of code that can be called by a main
program or any other method to perform some specific
function.
• The following are characteristics of methods:
-it can return one or no values
-it may accept as many parameters it needs or no
parameters at all. Parameters are also called arguments.
-after the method has finished execution, it goes back to
the method that called it.
CALLING METHODS
• Syntax:
objectName.methodName();
objectName.methodName(argumentList);
• Examples:
Car Porsche = new Car();
Porsche.accelerate(50);
Porsche.brake();

String s = “hello”;
int index = s.indexOf(‘1’, 2);
CALLING METHODS
• To illustrate how to call methods, let’s use the
String class as an example.
• You can use the Java API documentation to see
all the available methods in String class.
CALLING INSTANCE METHODS
• 2 sample methods
public char charAt(int index)

public Boolean equalsIgnoreCase(String


anotherString)
CALLING INSTANCE METHODS
• Using the methods,
String str1 = “Hello”;

char x = str1.charAt(0);

String str2 = “hello”;

Boolean result = str.equalsIgnoreCase(str2);


COMMON PITFALL
• When you encounter a NullPointerException,
chances are, you are trying to access a member
of an object which has not been created.
Remember to create an object first through the
new operator.
car porsche;
porsche.getSpeed();

You might also like