Professional Documents
Culture Documents
Lecture 5 OOP (Revised)
Lecture 5 OOP (Revised)
DEVELOPMENT
LECTURE 5 OBJECTED ORIENTED PROGRAMMING
1. Variables 1. Conditional
2. Data types statements
3. Data type 2. Loops
conversion 3. Methods
4. Arrays
What is OOP?
• Object-Oriented Programming is assembly programming.
In other words, it is to assemble objects to create an
entire program.
Bicycle (Program) = Body frame (Object) + Wheels (Object) + Steering wheel (Object)
Advantages of OOP
• Easy for maintenance
• Reusability
• Scalability
Class and Object
• How to make an object?
Instantiation
Program
(Bicycle)
Class Objects
(Blueprint) (Parts)
Class and Object (Cont’)
• Class Structure
Fields (state)
Methods (Behavior)
Class Name
Fields (State)
Methods
(Behavior)
Class and Object (Cont’)
Class Name
Fields (State)
Methods
(Behavior)
class Cat{
/*fields*/
String name;
String breed;
double weight;
/*methods*/
void claw() {
System.out.println("CLAW!!");
}
void meow() {
System.out.println("MEOW!!");
}
}
Class and Object (Cont’)
• Exercise 1
• Please make a square class and then return the area.
// field
// method
Constructor in Java
• What is a constructor?
• A constructor in Java is a special method to create an object
Constructor
Constructor
Constructor
Constructor in Java (Cont’)
• How to create / use a constructor?
• You already used a constructor using the keyword, new!
Constructor in Java (Cont’)
• 2 roles of a constructor
1. Create an object
2. Initialize an object