Object Oriented Programming: Objects and Classes
Object Oriented Programming: Objects and Classes
Lecture 05
Objects and Classes
MCS
Object Oriented Programming
• Object-oriented programming (OOP) involves
programming using objects.
• An object represents an entity in the real world
that can be distinctly identified. For example, a
student, a desk, a circle, a button, and even a
loan can all be viewed as objects.
• An object has a unique identity, state, and
behaviors.
• The state of an object consists of a set of data
fields (also known as properties) with their
current values.
• The behavior of an object is defined by a set of
methods.
MCS
Classes
• In OOP, a class is an extensible template for creating
objects, providing initial values for state (data field)
and implementations of behavior (methods).
• In Java, the class name is used as the name for the
class (the template itself), the name for the
constructors of the class (subroutines that create
objects), and as the type of objects generated by
instantiating the class.
• Example: Car class
Data Fields (properties/State) Methods (Behavior)
Model startEngine()
Year stopEngine()
Color accelerate()
MCS
Classes
class Circle {
/** The radius of this circle */
double radius; Data
Data Field
Field
/** Construct a circle object */
Circle() {
radius = 1.0;
}
Constructors
Constructors
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}
/** Return the area of this circle */
double getArea() { Method
Method
return Math.pow(radius, 2) * Math.PI;
}
}
MCS
Constructors
• Constructors are a special kind of methods that are
invoked to perform initializing actions.
• A constructor with no parameters is referred to as a default
constructor.
• Constructors must have the same name as the class itself.
• Constructors do not have a return type – not even void.
• Constructors are invoked using the new operator when an
object is created.
• A class may be declared without constructors. In this case,
a default constructor with an empty body is implicitly
declared in the class.
• Classes can have more than one constructor, in that case
all constructors have the same name (the class name),
where each constructor differs from the others in either the
number or types of its arguments.
Class
MCS Data Field
Method
Constructor
Output
MCS
Classes
class Circle {
/** The radius of this circle */
double radius; Data
Data Field
Field
/** Construct a circle object */
Circle() {
radius = 1.0;
}
Constructors
Constructors
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}
/** Return the area of this circle */
double getArea() { Method
Method
return Math.pow(radius, 2) * Math.PI;
}
}
MCS
Creating & Accessing Objects
public class CircleDemo {
public static void main(String[] args) {
// Create a circle with radius 5.0
Circle myCircle = new Circle(5.0);
System.out.printf("Radius: %.2f; Area:
%.2f\n",
myCircle.radius, myCircle.getArea());
// Create a circle with radius 1.0
Circle yourCircle = new Circle();
System.out.printf("Radius: %.2f; Area:
%.2f\n",
yourCircle.radius,
yourCircle.getArea());
// Modify circle radius
yourCircle.radius = 100.0;
System.out.printf("Radius: %.2f; Area:
MCS
Objects
data field 1 State (attributes) radius = 10
consists of a set of
… data fields
(properties)
data field m with their current values
(A) (B)
A generic object An example of
Circle object
MCS
UML Diagrams
Circle Class Name
Th }
this.id = id;
this.name = name;
Student s3 = s1.Clone();
Student s4 = s2.CloneNewRef();
s1.Display();
e Student(Student student) {
id = student.id;
s2.Display();
s3.Print();
name = student.name; s4.Print();
th }
Student() { }
}
is }
this(-1, "Unknown");
ke
static void Display(Student std)
{
System.out.printf("%d:
yw %s\n",
std.id, std.name);
}
or void Display() {
Display(this);
d }
void Print() {
this.Display();
in }
Student Clone() {
Ja
return this;
}
Student CloneNewRef() {
va }
return new Student(this);
class Student { public class StudentDemo {
int id; public static void main(String[] args) {
String name; Student s1 = new Student(123, "Ali");
MCS Student(int id, String name) { Student s2 = new Student();
Th }
this.id = id;
this.name = name;
Student s3 = s1.Clone();
Student s4 = s2.CloneNewRef();
s1.Display();
e this keyword can be used to refer current class
Student(Student student) {
id = student.id;
s2.Display();
s3.Print();
instance variable (shadowed by the parameters).
name = student.name; s4.Print();
th }
Note: You can only use the this reference for
Student() { }
}
ke
static void Display(Student std)
{
System.out.printf("%d: A static variable is common to all the instances (or
yw %s\n",
std.id, std.name);
objects) of the class because it is a class level
variable. only a single copy of static variable is
}
or void Display() {
Display(this);
created and shared among all the instances of the
class.
d }
void Print() {
this.Display();
Instance variables are created when an object is
created with the use of the keyword 'new' and
in }
Student Clone() {
destroyed when the object is destroyed.
Local Variable are declared inside method of the
Ja
return this; class. Their scope is limited to the method which
}
Student CloneNewRef() {
means that You can’t change their values and
va }
return new Student(this); access them outside of the method.
class Student { public class StudentDemo {
int id; public static void main(String[] args) {
String name; Student s1 = new Student(123, "Ali");
MCS Student(int id, String name) { Student s2 = new Student();
Th }
this.id = id;
this.name = name;
Student s3 = s1.Clone();
Student s4 = s2.CloneNewRef();
s1.Display();
e Student(Student student) {
id = student.id;
s2.Display();
s3.Print();
name = student.name; s4.Print();
th }
Student() { }
}
is }
this(-1, "Unknown");
yw %s\n",
constructor. We use the this() call to invoke current
std.id, std.name);
}
class constructor.
or void Display() {
Rule: this() must be called within constructor only
Display(this);
d }
and it should be the first statement.
void Print() {
this.Display();
in }
Student Clone() {
Ja
return this;
}
Student CloneNewRef() {
va }
return new Student(this);
class Student { public class StudentDemo {
int id; public static void main(String[] args) {
String name; Student s1 = new Student(123, "Ali");
MCS Student(int id, String name) { Student s2 = new Student();
Th }
this.id = id;
this.name = name;
Student s3 = s1.Clone();
Student s4 = s2.CloneNewRef();
s1.Display();
e Student(Student student) {
id = student.id;
s2.Display();
s3.Print();
name = student.name; s4.Print();
th }
Student() { }
}
is }
this(-1, "Unknown");
ke
static void Display(Student std)
{
System.out.printf("%d:
yw %s\n",
std.id, std.name);
}
or void Display() {
this can be passed as an
Display(this);
d }
argument in the method call.
void Print() {
this.Display();
in }
Student Clone() {
Ja
return this;
}
Student CloneNewRef() {
va }
return new Student(this);
class Student { public class StudentDemo {
int id; public static void main(String[] args) {
String name; Student s1 = new Student(123, "Ali");
MCS Student(int id, String name) { Student s2 = new Student();
Th }
this.id = id;
this.name = name;
Student s3 = s1.Clone();
Student s4 = s2.CloneNewRef();
s1.Display();
e Student(Student student) {
id = student.id;
s2.Display();
s3.Print();
name = student.name; s4.Print();
th }
Student() { }
}
is }
this(-1, "Unknown");
ke
static void Display(Student std)
{
System.out.printf("%d:
yw %s\n",
std.id, std.name);
}
or void Display() {
Display(this);
d }
void Print() {
this keyword can be used to
this.Display();
in }
invoke current class method.
Student Clone() {
Ja
return this;
}
Student CloneNewRef() {
va }
return new Student(this);
class Student { public class StudentDemo {
int id; public static void main(String[] args) {
String name; Student s1 = new Student(123, "Ali");
MCS Student(int id, String name) { Student s2 = new Student();
Th }
this.id = id;
this.name = name;
Student s3 = s1.Clone();
Student s4 = s2.CloneNewRef();
s1.Display();
e Student(Student student) {
id = student.id;
s2.Display();
s3.Print();
name = student.name; s4.Print();
th }
Student() { }
}
is }
this(-1, "Unknown");
ke
static void Display(Student std)
{
System.out.printf("%d:
yw %s\n",
std.id, std.name);
}
or void Display() {
Display(this);
d }
void Print() {
this.Display();
in }
Student Clone() {
this keyword can also be used to
Ja
return this;
}
return the current class instance.
Student CloneNewRef() {
va }
return new Student(this);
MCS
Default Value: Data Fields
public class PersonDemo {
public static void main(String[] args) {
Person p = new Person();
System.out.println("name: " + p.name);
System.out.println("age: " + p.age);
System.out.println("gender: " + p.gender);
System.out.println("isMarried: " + p.isMarried);
}
}
class Person {
String name; // Default Value: null
int age; // Default Value: 0
char gender; // Default Value: '\u0000'
boolean isMarried; // Default Value: false
}
MCS
Default Value: Data Fields
public class PersonDemo {
public static void main(String[] args) {
Person p = new Person();
System.out.println("name: " + p.name);
The default value of a data field is:-
System.out.println("age: " + p.age);
• System.out.println("gender:
null for a reference type, " + p.gender);
• System.out.println("isMarried:
0 for a numeric type, " + p.isMarried);
•} false for a boolean type,
}
• and '\u0000' for a char type.
class Person {
String name; // Default Value: null
int age; // Default Value: 0
char gender; // Default Value: '\u0000'
boolean isMarried; // Default Value: false
}
MCS
Default Value: Local Variables
• Java assigns no default value to a local variable
inside a method.
public class Test {
public static void main(String[] args)
{
int x; // x has no default value
String y; // y has no default value
System.out.println("x is " + x);
System.out.println("y is " + y);
}
}
• The above code will not be compiled, with
compilation error “variables not initialized”.