0% found this document useful (0 votes)
19 views34 pages

Objects Classes

The document provides an overview of object-oriented programming (OOP) concepts, focusing on objects, classes, and key principles such as abstraction, encapsulation, inheritance, and polymorphism. It explains how objects represent real-world entities with unique identities, states, and behaviors, and discusses the role of classes in defining these objects. Additionally, it covers the importance of encapsulation in hiding internal details and controlling access to class variables through getter and setter methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views34 pages

Objects Classes

The document provides an overview of object-oriented programming (OOP) concepts, focusing on objects, classes, and key principles such as abstraction, encapsulation, inheritance, and polymorphism. It explains how objects represent real-world entities with unique identities, states, and behaviors, and discusses the role of classes in defining these objects. Additionally, it covers the importance of encapsulation in hiding internal details and controlling access to class variables through getter and setter methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Objects and Classes

1
OO Programming Concepts

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.

2
OO Programming Concepts

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.

3
Principles of OOP

 Abstraction
 Encapsulation
 Inheritance
 Polymorphism

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
27
Principles of OOP

 Abstraction
 Encapsulation
 Inheritance
 Polymorphism

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
27
Abstraction

Abstraction:
– represent complex real world in simplest manner.
– identify the relevant qualities and behaviors of an object
– represent the necessary feature and ignore the unnecessary
ones

Abstraction is the concept of describing/representing something in


simpler terms, i.e. abstracting away the details, in order to focus
on what is important.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Abstraction

Abstraction is generic term and could be achieved in various


ways. For example:
 through creating classes (see soon)
 through subclassing (see later)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Abstraction (Example)
A person in different roles:
– At school: "Student".
– At work: "Employee".
– At government institution: "Citizen".

So, different representation of a person:


– Payroll System- Employee(PRN, Full Time/Part Time, Designation).
– Course Enrollment System-Student(Roll Number, Age, Gender, Course
Enrolled).
– NID Information System- Citizen( DOB, Gender, District, etc.)

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Classes

Classes are constructs that define objects of the


same type.
A Java class uses variables to define data fields
and methods to define behaviors.
Additionally, a class provides a special type of
methods, known as constructors, which are
invoked to construct objects from the class.

9
Classes

10
Objects

An object has both a state and behavior. The state


defines the object, and the behavior defines what the
object does.

11
UML Class Diagram

12
Constructors
Constructors are a special kind of methods that
are invoked/called to construct objects.

Circle() {
}

Circle(double newRadius) {
radius = newRadius;
}
13
Constructors, cont.
Constructors:
 with no parameters is referred to as a no-arg
constructor.
 must have the same name as the class itself.
 do not have a return type—not even void.
 are invoked using the new operator when an
object is created.
 play the role of initializing objects.
14
Creating Objects Using
Constructors
Format:
new ClassName();

Example:
new Circle();
new Circle(5.0);

15
Default Constructor
A class may be declared without constructors. In
this case, a no-arg constructor with an empty
body is implicitly declared in the class.

This constructor, called a default constructor, is


provided automatically only if no constructors
are explicitly declared in the class.

16
Declaring Object Reference Variables
To reference an object, assign the object to a reference
variable.

To declare a reference variable, use the syntax:

Format:
ClassName objectRefVar;

Example:
Circle myCircle;
17
Declaring/Creating Objects
in a Single Step
ClassName objectRefVar = new ClassName();

Assign object reference Create an object


Example:
Circle myCircle = new Circle();

18
Accessing Objects
 Referencing the object’s data:
objectRefVar.data
e.g., myCircle.radius

 Invoking the object’s method:


objectRefVar.methodName(arguments)
e.g., myCircle.getArea()

19
animation
Trace Code Declare, create,
and assign in one
statement
JFrame frame1 = new JFrame(); frame1
:
reference
JFrame
frame1.setTitle("Window 1"); titl
frame1.setSize(200, 150); e:
frame1.setVisible(true); JFrame wi
dth:
frame2 = new JFrame(); hei
frame2.setTitle("Window 2"); ght:
frame2.setSize(200, 150); visi
ble:
frame2.setVisible(true);

20
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
Set title property
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); JFrame "Window 1"
width:
frame2 = new JFrame(); height:
frame2.setTitle("Window 2"); visible:
frame2.setSize(200, 150);
frame2.setVisible(true);

21
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
Set size property
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible:
frame2.setSize(200, 150);
frame2.setVisible(true);

22
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1" Set visible
width: 200 property
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150);
frame2.setVisible(true);

23
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150); Declare, create,
:
frame2 reference and assign in one
frame2.setVisible(true); JFrame
statement
titl
e:
wi
dth:
hei
ght:
visi
ble:

24
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150);
frame2 reference
frame2.setVisible(true);
: JFrame Set title property
title:
"Window 2"
width:
height:
visible:

25
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150);
frame2 reference
frame2.setVisible(true);
: JFrame
title:
Set size property
"Window 2"
width: 200
height: 150
visible:

26
animation
Trace Code
JFrame frame1 = new JFrame(); frame1 reference
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title:
frame1.setVisible(true); "Window 1"
width: 200
JFrame frame2 = new JFrame(); height: 150
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150);
frame2 reference
frame2.setVisible(true);
: JFrame
title:
"Window 2" Set visible
width: 200 property
height: 150
visible: true

27
Principles of OOP

 Abstraction
 Encapsulation
 Inheritance
 Polymorphism

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
27
Encapsulation

• A process of hiding all the internal details of an object


from the outside real world.

• Like enclosing into the capsule.

• Restricts client from seeing implementation detail.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Encapsulation (Example)
• For example:

 if someone wants to know my name then he cannot directly


access my brain cells to get to know what is my name.
Instead that person will either ask my name.

 If a driver wants to speed up a vehicle then he doesn’t start


to put more gas/oil inside the engine. He uses the interface
(accelerator pedal, gear, etc) for that purpose.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Encapsulation in Java
The variables of a class might need to be hidden from other
classes, and can be accessed only through the methods of their
current class, it is also known as data hiding.

To achieve encapsulation (data hiding) in Java


– Declare the variables of a class as private.
– Provide public setter methods (also known as accessors)
and getter methods (also known as mutators) to write and
read the variables values.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Encapsulation in Java (Example)
public class EncapsulationTest{

private String name;


private int age;

public int getAge(){


return age;
}

public String getName(){


return name;
}

public void setAge( int newAge){


age = newAge;
}

public void setName(String newName){


name = newName;
}
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Encapsulation in Java (Example)
public class RunEncapsulation{

public static void main(String args[]){


EncapsulationTest encap = new EncapsulationTest();
encap.setName("James");
encap.setAge(20);

System.out.print("Name : " + encap.getName() + " Age : "


+ encap.getAge());
}
}

Output:
Name : James Age : 20

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
Benefits of Encapsulation in Java
Benefits of Encapsulation:

– The fields of a class can be made read-only or write-only.


– A class can have total control over what is stored in its fields.
– The users of a class do not know how the class stores its data. A
class can change the data type of a field and users of the class do
not need to change any of their code.

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2

You might also like