This document provides an introduction to object-oriented programming concepts like classes, objects, and methods in Java. It defines classes as templates that define attributes and behaviors of objects as variables and methods. Objects are instances of classes. The document explains how to declare a class with access modifiers, variables, constructors, and methods. It also demonstrates how to create objects using the new keyword and access object attributes and methods.
1. Using Java
MINISTRY OF EDUCATION & HIGHER EDUCATION
COLLEGE OF SCIENCE AND TECHNOLOGY
KHANYOUNIS- PALESTINE
Lecture 6
Introduction to classes and objects
2. What is Object-Oriented Programming ?
What classes, objects, methods ?
How to declare a class ?
How to use class to create an object?
How to declare methods in a class ?
Emank X Mezank
2Presented & Prepared by: Mahmoud R. Alfarra
3. It is a whole new way of thinking about
programming!
It is a way of modeling software that maps
your code to the real world.
Now please, Look in this room …
3Presented & Prepared by: Mahmoud R. Alfarra
What is Object-Oriented Programming ?
Chair
Door
Man
Window
.
.
No. of legs
Material
Color
Size
Purpose
.
.
Abstraction
Encapsulation
4. 4Presented & Prepared by: Mahmoud R. Alfarra
What is Object-Oriented Programming ?
Any Thing
Attributes
Behavior
Each one represented
by variable in class
Each one represented
by method in class
5. The terms class and object are often
confused, and it is important to understand
the distinction.
It may help you to visualize these terms using
the earlier racecar analogy.
Think of a class as the template for the car, or
perhaps the plans used to build the car.
The car itself is an instance of those plans, so
it could be referred to as an object.
5Presented & Prepared by: Mahmoud R. Alfarra
What are classes, objects ?
7. The behavior of the class will be represented
as a method in class.
The method can be declared as:
7Presented & Prepared by: Mahmoud R. Alfarra
What are methods ?
Access_modifiers return_type method_name (arguments)
{
// instructions
return value;
}
8. 8Presented & Prepared by: Mahmoud R. Alfarra
What are methods ? (Example)
Public void add (int x, int y)
{
int sum = x + y;
}
Public int even (int x)
{
if (x%2 ==0)
return x;
else
{JOptionPane.showMessageDialog(null, "The value is not even ");
Return -1; }
}
9. 9Presented & Prepared by: Mahmoud R. Alfarra
How to declare Class?
Access_modifiers class class_name{
//variables declaration
// constructor
// methods
}
Public class Car{
public String model;
public String ID;
public int price;
public Car (){
model = “Cevic”;
ID = “20023456”;
price= 12,000;
// methods
}
10. 10Presented & Prepared by: Mahmoud R. Alfarra
How to use class to create an object?
class_name obj_name = new constructor_name ();
Car c1 = new Car ();
Car c2 = new Car ();
Car c2 = new Car ();
c2.model = “Honda”;
c2.ID = “987435”;
c2.price = 4000;