KEMBAR78
Classes and ObjectsPINIKAS | PDF | Object Oriented Programming | Class (Computer Programming)
0% found this document useful (0 votes)
4 views60 pages

Classes and ObjectsPINIKAS

The document provides an overview of Object-Oriented Programming (OOP) in Java, detailing concepts such as classes, objects, attributes, and constructors. It explains the benefits of using OOP, including code reusability, easier maintenance, and real-world modeling. Additionally, it outlines the process of creating classes and objects, as well as the differences between static and non-static attributes and constructors.

Uploaded by

kirtbayot1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views60 pages

Classes and ObjectsPINIKAS

The document provides an overview of Object-Oriented Programming (OOP) in Java, detailing concepts such as classes, objects, attributes, and constructors. It explains the benefits of using OOP, including code reusability, easier maintenance, and real-world modeling. Additionally, it outlines the process of creating classes and objects, as well as the differences between static and non-static attributes and constructors.

Uploaded by

kirtbayot1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

IT 103 - PROGRAMMING 2

Instructor: Jayson Bonggo


Classes and Objects
OBJECTIVES

Describe classes and objects


Create a class
Create instance methods in a class
Declare objects and use their
methods
Create and use constructors
Use the this reference
Use of Object Overloading
OBJECT-ORIENTED PROGRAMMING

Java: A high-level, powerful programming language that


uses the Object-Oriented Programming (OOP)
approach.
Object-Oriented Programming: A programming
style that models real-world entities as "objects."
Objects have:
Data (fields): Information or attributes.
Behavior (methods): Actions or functions.
OBJECT-ORIENTED PROGRAMMING
Traditional Programming (Procedural):
Programs are written as step-by-step instructions or
functions.
Focuses on operations performed on data.
Becomes hard to manage as programs grow larger and
more complex.

Object-Oriented Programming:
Organizes code into reusable objects.
Makes software development:
More structured.
Easier to scale.
Simpler to maintain.
OBJECT-ORIENTED PROGRAMMING

Why Use OOP in Java?

Code Reusability:
Write code once and reuse it multiple times, saving time
and effort.
Easier Maintenance:
Code is organized into objects, making it simpler to
debug and update.
Better Security:
Encapsulation keeps data safe from unintended
access or modification.
OBJECT-ORIENTED PROGRAMMING

Why Use OOP in Java?

Real-World Modeling:
OOP mirrors how objects interact in real life, making
it intuitive and practical.
Java’s Popularity:
Java is one of the most widely used OOP languages.
Ideal for both beginners and professionals.
CLASSES

What is a Class?

A class is like a blueprint or template for creating


objects.
It defines:
Data (fields/attributes): Information about the object.
Behavior (methods): Actions the object can perform.
CLASSES

Analogy:
Think of a class as a recipe for baking a cake.
The recipe lists ingredients (data) and steps
(methods).
The actual cakes (objects) are created using
the recipe, each with unique variations (e.g.,
flavors, decorations).
CLASSES

Variables in a Class:

Instance Variables:
Declared inside a class but outside any
method.
Non-static and unique to each object.
Class Variables:
Declared with the static keyword.
Shared across all objects of the class.
CLASSES

Variables in a Class:

Instance Variables:
Declared inside a class but
outside any method.
Non-static and unique to
each object.
Class Variables:
Declared with the static
keyword.
Shared across all objects
of the class.
OBJECTS

What is an Object?

An object is a real-world entity created from a class.


If a class is a blueprint, an object is the actual
product made from that blueprint.
Instantiation: The process of creating an object
from a class.
OBJECTS

Think of a class as a recipe for making cookies.


The recipe defines the ingredients (data)
and steps (methods).
The cookies (objects) are the actual
results of following the recipe.
Each cookie follows the same recipe but
may have slight differences (e.g., shape,
size).
ATTRIBUTES

What are Attributes?

Attributes are the characteristics or properties of


an object.
They store data that defines what an object is.
Declared inside a class but outside methods.
Have data types like int, String, double, etc.
ATTRIBUTES

What are Attributes?

Attributes are also called fields or instance


variables.
They belong to a class.
Each object has its own copy of instance
variables.
Instance variables are specific to an object
and can only be accessed within that
object.
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

A simple way to understand classes and


objects is by thinking about dogs. Let’s
break it down step by step!
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 1: Identifying Differences and


Common Traits

Imagine you see three different dogs. You


may notice they have different features,
such as:
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 1: Identifying Differences and


Common Traits

Imagine you see three different dogs of


different breeds. You may notice they
have different features, such as:
Breed (Labrador, Bulldog,
Poodle)
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 1: Identifying Differences and


Common Traits

Imagine you see three different dogs of


different breeds. You may notice they
have different features, such as:
Breed (Labrador, Bulldog,
Poodle)
Size (Small, Medium, Large)
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 1: Identifying Differences and


Common Traits

Imagine you see three different dogs of


different breeds. You may notice they
have different features, such as:
Breed (Labrador, Bulldog,
Poodle)
Size (Small, Medium, Large)
Age (Puppy, Adult, Senior)
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 1: Identifying Differences and


Common Traits

Imagine you see three different dogs of


different breeds. You may notice they
have different features, such as:
Breed (Labrador, Bulldog,
Poodle)
Size (Small, Medium, Large) Even though they are different, they
Age (Puppy, Adult, Senior) all share common characteristics
Color (Black, Brown, White) that make them dogs.
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 2: Identifying Common


Behaviors

All dogs can:


UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 2: Identifying Common


Behaviors

All dogs can:


Eat
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 2: Identifying Common


Behaviors

All dogs can:


Eat
Sleep
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 2: Identifying Common


Behaviors

All dogs can:


Eat
Sleep
Sit
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 2: Identifying Common


Behaviors

All dogs can:


Eat
Sleep
Sit
Run
These behaviors are the actions that
all dogs can perform.
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Step 3: Defining the Class and


Objects

Now, let’s relate this to Java:

Class → "Dog" (A blueprint that defines


characteristics and behaviors of a dog)
Data Members (Attributes) → breed,
size, age, color
Methods (Behaviors) → eat(), sleep(),
sit(), run()
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

Now, for different values of data members (breed size, age, and
color) in Java class, you will get different dog objects.
UNDERSTANDING CLASSES AND OBJECTS
SIMPLE ANALOGY TO UNDERSTAND CLASSES AND OBJECTS

A class is like a general category (Dogs).


Each object is a specific example of that class (Maltese,
TigerComando, ChowChow).
Different objects have different attributes, but they all share
common behaviors.
CLASS CREATION

STEP1: In the New Project window:

Select Java from the categories on the left.


Select Java Application from the projects on the right.
Click Next.
In the Project Name field, type a name for your project
(e.g., MyJavaProject).
Uncheck "Create Main Class" if you want to manually
create classes.
Click Finish.
CLASS CREATION

STEP 2: Create a New Java Class:

In the Projects panel on the left, locate your project


folder (e.g., MyJavaProject).
Right-click on the Source Packages folder.
Click New → Java Class.
In the Class Name field, enter a name for your class (e.g.,
Person).
Click Finish.
CLASS CREATION
CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS

Class Instantiation:

The process of creating an object from a class.


Java reserves memory for the object based on the class blueprint.
CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS

Class Instantiation

The process of creating an object from a class.


Java reserves memory for the object based on the class blueprint.

ClassName → The name of the class.


objectName → The name of the object (instance).
new → This keyword allocates memory for the object.
ClassName() → Calls the class constructor to initialize the object.
CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS

We can create non-static attributes (which belong to individual objects)


and static attributes (which belong to the class itself).
CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS

Understanding Static vs. Non-Static Attributes


CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS

Understanding Static vs. Non-Static Attributes

Static Attribute → Like a shared company rule (e.g., "All employees


work 8 hours a day").
Non-Static Attribute → Like an individual employee’s salary (each
employee has a different amount).
CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS
CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS
CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS

To access the attributes of a class, we create an object and use dot notation (.).
CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS
CLASS INSTANTIATION, ATTRIBUTE CREATION, AND ATTRIBUTE ACCESS
CONSTRUCTORS

What is a Constructor?

A special method used to initialize objects when they are


created.
It has the same name as the class and runs automatically
during object instantiation.
CONSTRUCTORS

Key Features of a Constructor

Same name as the class.


No return type (not even void).
Runs automatically when an object is created.
Used to set initial values for object attributes.
CONSTRUCTORS

Analogy: Constructor as a Recipe's Setup

Imagine a recipe for baking a cake:


Before baking, you set up ingredients
(flour, eggs, sugar).
Every time you follow the recipe, you
start with these same ingredients.
Similarly, a constructor sets up initial
values for every object you create.
CONSTRUCTORS

CREATING A CONSTRUCTOR
CONSTRUCTORS

CREATING A CONSTRUCTOR
CONSTRUCTORS

CREATING A CONSTRUCTOR
CONSTRUCTORS

CREATING A CONSTRUCTOR
CONSTRUCTORS

CREATING A CONSTRUCTOR
CONSTRUCTORS

Types of Constructors

1. Default Constructor (No Parameters):


Assigns default values to object attributes.
2. Constructor with Parameters:
Allows assigning custom values when
creating an object.
CONSTRUCTORS

Default Constructor:
No parameters.
Assigns default values.
Use when all objects should have the
same default values.
CONSTRUCTORS

Default Constructor:
No parameters.
Assigns default values.
Use when all objects should have the
same default values.
CONSTRUCTORS

Default Constructor:
No parameters.
Assigns default values.
Use when all objects should have the
same default values.
CONSTRUCTORS
Default Constructor:
No parameters.
Assigns default values.
Use when all objects should have the same default values.
CONSTRUCTORS

Constructor with Parameters:


Requires input values.
Assigns custom values.
Use when you want different values
for each object.
CONSTRUCTORS

Constructor with Parameters:


Requires input values.
Assigns custom values.
Use when you want different values
for each object.
CONSTRUCTORS

Constructor with Parameters:


Requires input values.
Assigns custom values.
Use when you want different values
for each object.
CONSTRUCTORS
Constructor with Parameters:
Requires input values.
Assigns custom values.
Use when you want different values for each object.
CONSTRUCTORS

Default Constructor → "Pre-filled Coffee Cup" ☕


Imagine buying a coffee cup from a vending machine that automatically
fills it with black coffee. You don't choose the type of coffee; it is
always black.
Constructor with Parameters → "Custom Coffee Order" 🍵
Now imagine going to a café where you can order a coffee with specific
ingredients (milk, sugar, flavors). You choose what you want, and the
barista prepares it accordingly.

You might also like