0% found this document useful (0 votes)
5 views

Object Oriented Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Object Oriented Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

OBJECT ORIENTED

PROGRAMMING
with

JAVA
(PF101)
Object Oriented Programming
It focuses on implementing real world objects
using Classes to create variations of Objects,
that has attributes and purpose.

It helps us create much flexible and efficient


code than procedural programming.
Classes and Objects
 CLASSES  CLASS Creation
 OBJECTS  CLASS Instantiation
 ATTRIBUTES  ACCESSING Attributes
CLASSES
It is created by the programmer, It will act as a
blueprint of an object that you want to
implement in your program.

They contain all the attributes and methods


that your desired object should have.
OBJECTS
It is created by Instantiating a Class.

It is anything that has an attribute and a


purpose.

Example: Person, Furniture and Food


ATTRIBUTES
These are the global variables declared
inside the class of our object.

It is used to create variations of an


object using only one class.
CLASS Creation
modifiers className {

//Attributes
//Methods or Purpose
}
CLASS Creation
Person public Person class
- First Name {
- Last Name //Attributes
- Sex String firstName;
- Age String lastName;
char sex;
int age;

//Methods or Purpose

}
CLASS Instantiation
The process of creating an
Object using a class so we
can use it on our program.
CLASS Instantiation
Classname identifier = new
Classname();
Person p = new Person();
ACCESSING Attributes
ClassName identifier = new ClassName();

WRITING Attributes
identifier attribute = value;

READING Attributes
System.out.println(identifier.attribute);
Constructors
 CONSTRUCTORS
 CREATING Constructors
 THIS Keyword
 USING Constructors
 USER INPUT Object Creation
CONSTRUCTOR
Is the method called when you
instantiate a class / create an
object.

It is used to initialize the attributes


of an object or run a block of code
when an object is created.
CREATING
Constructors
Constructors method are named
after their Class Name.

modifiers className class {

className() {
//Constructor
}
}
Constructors method are named after
their Class Name.

public class Product {

Product() {
System.out.println(“Product
Created”);
}
}
Constructors are used to initialize
attributes.

public Product class {

String name;
float price;

Product(String name, float price) {


this.name = name;
this.price = price;
}
}
THIS Keyword
The this keyword refers to the class
itself.

The this keyword will enables you


to access global variables inside
the class if you have the same
variable names in a parameter
USING Constructors
ClassName identifier = new ClassName
(parameters);

Product p1 = new Product(“Milk”, 150.0f);


Product p2 = new Product(“Noodles”, 15.25f);
Product p3 = new Product(“Softdrinks”, 12.50f);
USER INPUT Object
Creation
Create a class of your choice then
create an object from that class
using user input
Object Methods
 UNDERSTANDING Object Methods
 CREATING & CALLING Object Methods
 STUDENT Object Simulation
OBJECT METHODS
Are methods declared inside an
Object Class.

Object Methods are considered as


the Object’s purpose.
CREATING Object
Methods
Objects Methods are same as the Methods we talked
about.

modifiers className class {

modifiers returntype methodName


(arguments) {
//Do Anything Here

}
Objects Methods are same as the Methods we talked
about.

public Character class {

String name, dialog;


int hp, mp, lvl;

void introduce() {
System.out.println(“I am” + name);

}
CALLING Object
Methods
Objects Methods are same as the Methods we talked
about.

SYNTAX
Classname cn = new ClassName(constructor);
cn.methodName(arguments);

EXAMPLE
Character c = new Character(constructor);
c.introduce();
STUDENT Object
Simulation
Create a Class Student that has the
attributes:
• firstName
• lastName
• year
• course
• section
• midtermGrade
• finalGrade
Create a Constructor for the class.

Create the following Object Methods:

• introduceSelf()
• Outputs the Full Name, Course, Year and
Section of the Student

• evaluateGrade()
• Average the Midterm and Final Grade
output their average and their standing if
they are an honor, passed or failed.
Grade Criteria:

Above 100 : Invalid Grade


98 - 100 : With Highest Honors
95 - 97.99 : With High
Honors
90 - 94.99 : With Honors
75 - 89.99 : Passed
Below 75 : Failed
Encapsulation
 ENCAPSULATION
 USING Encapsulation
 GETTERS and SETTERS
 CREATING Getters and Setters
 USING Getters and Setters
ENCAPSULATION
Is an OOP technique
used to hide data
from direct access.
USING Encapsulation
Declare Attributes as PRIVATE

modifiers className class {


//Private Attributes

}
Declare Attributes as PRIVATE

public User class {


private int userID;
private String username;
private String firstName,
lastName;

}
GETTERS and SETTERS
Are methods used to get and
set
encapsulated variables.
SETTER
Is a method used to set encapsulated
variables

void setUserID(int userID) {

this.userID = userID;

}
GETTER
Is a method used to get encapsulated
variables

in getUserID(int userID) {

return userID;

}
OVERLOADING
Constructors
Is an OOP technique used to create
multiple constructors with different
arguments, It is used to cope up with the
needs of a certain instance of an object.

PS: This is the same as Method


Overloading
Overloading
Constructors
Is an OOP technique used to create
multiple constructors with different
arguments, It is used to cope up with the
needs of a certain instance of an object.

PS: This is the same as Method


Overloading
Create Multiple Methods with the Class Name
and with Different Arguments

modifiers class className { class Employee {


//Attributes //Attributes
className(arguments) { Employee(arguments) {

} }

className(arguments) { Employee(arguments) {

} }

} }
Inheritance
 INHERITANCE
 SUBCLASS and SUPERCLASS
 EXTENDS keyword  OVERRIDING Methods
 USING inheritance  OVERRIDING Constructors
 SUPER keyword
INHERITANCE
Is an OOP technique used to
inherit attributes and methods
from one class to another.
SUPERCLASS
The class where we will inherit the
attributes and methods.
SUBCLASS
The class who will inherit the
attributes and methods from a
superclass.
EXTENDS Keyword
Used after the class name and it
indicates that the certain class will
inherit from another class.
USING Inheritance
Extends Keyword after subclass name

modifers class subClassName extends


superClassName {

//Attributes and Methods

}
class person { class toddler extends
person {
String name, sex;
int age; void drink();
//Prints “Drinking Milk”
//Methods
}
}
}
OVERRIDING
You are required to call the constructor of the
Constructors
superclass
class person { class toddler extends
person {
person(arguments)
{ toddler(arguments) {
//constructors super(arguments);
//add attributes
}
}
}
}
SUPER Keyword
Super keyword can only be used by a
subclass and it is used to call their
superclass so we can access their
constructors, attributes and methods.
OVERRIDING Methods
To retain the Functionality from the Super Class
use the Super Keyword with the Method Name

class person { class toddler extends


person {
void checkStatus()
{ void checkStatus() {
//output attributes super checkStatus();
//add Functionalities
}
}
}
Polymorphism
“Poly” means MANY and Morph means
“Take Different Forms”

It is an OOP technique that utilizes


inheritance to create 1 class and make
several classes inherit from that class so
that it can take many forms.
USING Polymorphism
class Animal { class Dog extends Animal {

void makeSound() {
void makeSound() { //Arf
//Sound }

}
}
class Cat extends Animal {

} void makeSound() {
//Meow

}
CHALLENGE
Apply this technique (Polymorphism)
on your own create any class that
could act as the Superclass and make
Subclasses that will inherit from that
superclass.
Abstraction
 ABSTRACTION
 ABSTRACT Classes
 ABSTRACT Methods
 USING Abstraction (Abstract Class)
ABSTRACTION
It is an OOP technique that hides certain details
and only shows the important information.

It can be achieved in 2 ways:


• Abstract Classes (Uses abstract modifier)
• Interfaces (Uses implements keyword)
ABSTRACTION Class
It cannot be instantiated, to access it you need
to inherit it from another class.

ABSTRACTION Methods
Can only be declared inside an abstract class
it is a Method without a body and it needs to
be overriden in the subclass of the abstract
class
USING Abstraction
(Abstract Classes)

abstract class Animal { class Dog extends Animal {

void makeSound() {
abstract void //Arf
makeSound() { }

}
}
class Cat extends Animal {

} void makeSound() {
//Meow

}
Abstraction (Interface)
 ABSTRACTION
 INTERFACE
 IMPLEMENTS Keyword
 USING Abstraction (Interface)
ABSTRACTION
It is an OOP technique that hides certain details
and only shows the important information.

It can be achieved in 2 ways:


• Abstract Classes (Uses abstract modifier)
• Interfaces (Uses implements keyword)
INTERFACE
It is a full Abstract Class that is
implemented in other classes.

Any method you declare would be an


abstract method by default you can use
the default modifier to create a method with
body the method would be static.

Any variable that you declare would be


static and final
IMPLEMENTS Keyword
Used after the class name so that we can
implement an interface in that certain
class.

Implemented interface requires the


class to override every method inside the
interface.

PS We can implement 1 or more


interfaces in each class.
USING (Interfaces)
Abstraction
interface Animal { class Dog implements Animal {

void makeSound() {
void makeSound() { //Arf

}
}
}
class Cat implements Animal {
}
void makeSound() {
//Meow

}
Arrays of Objects
 ARRAY OF OBJECTS
 INITIALIZING Array of Objects
 STORING Objects inside Array
 ACCESSING Objects inside Array
 Student Registration Simulation Challenge
ARRAY OF OBJECTS
You can declare an array of a
certain class and store instances of
that class inside that array.
INITIALIZING Array of
Objects
className identifier[] = new
className[size];
Employee employee[] = new Employee[5];
STORING Objects
Inside Array
className identifier[] = new
className[size];
identifier[index] = new
className(constructor);

Employee employees[] = new


Accessing Array
VALUE Employee 1 Employee 2 Employee 3 Employee 4 Employee 5

INDEX 0 1 2 3 4
ACCESSING Objects
Inside Array
employee[0].instroduceSelf();
employees[0].firstName;
STUDENT Registration
Simulation
Make the User Input the number of students to be
registered.

Make the User Input the credentials of the students.

Student
• First Name
• Last Name
• Year
• Course
• Section

PS Use every knowledge you’ve learned so far, ex. Constructors


and Encapsulation
Enums
A Special Class that contains a
collection of constant values.

They can represent words as


Objects.
DECLARING Enums
enum AlLevel {
EASY,
MEDIUM,
HARD
}
USING Enums
AILevel level =
AILevel.HARD;
Enums in Conditional
Statement
AILevel level = AILeve.HARD;
if(level == AILevel.EASY) {
} else if(level ==if
AILevel.MEDIUM) {
} else if(level == AILevel.HARD){
}

You might also like