Object Oriented Programming
Object Oriented Programming
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.
//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.
className() {
//Constructor
}
}
Constructors method are named after
their Class Name.
Product() {
System.out.println(“Product
Created”);
}
}
Constructors are used to initialize
attributes.
String name;
float price;
}
Objects Methods are same as the Methods we talked
about.
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.
• 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:
}
Declare Attributes as PRIVATE
}
GETTERS and SETTERS
Are methods used to get and
set
encapsulated variables.
SETTER
Is a method used to set encapsulated
variables
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.
} }
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
}
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
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.
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)
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.
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);
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.
Student
• First Name
• Last Name
• Year
• Course
• Section