You are on page 1of 23

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

CHAPTER 2
OBJECT-ORIENTED
PROGRAMMING

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Objectives
1. Understand the concept about data
abstraction, encapsulation, inheritance
and polymorphism.
2. Understand about objects and classes
and the relationship between them.
3. Understand about method and can write
java application with a simple method.
4. Understand about instance variables,
class variables, instance method and
class method.

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Object-Oriented Concept
Encapsulation

Mechanism that binds together code and the


data it manipulated, and keeps both safe from
outside interference and misuse.

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

How to understand encapsulation


One way to think about encapsulation is as a
protective wrapper that prevents the code and
data from being accessed by other code
defined outside the wrapper.
The basis of encapsulation is the class

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Inheritance
The process by which object acquires the properties
of another object.
Arrange all classes in a strict hierarchy
Each class has a superclass
( the class above it in the hierarchy)
Each class can have one or more subclasses
(classes below that class in the hierarchy)
Subclasses inherit the methods and variables from
their superclass.

MANAGEMENT & SCIENCE UNIVERSITY

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

TCS 2044 JAVA PROGRAMMING

Class C

Class A

The superclass of B

Class B

Subclass of A
the superclass of C,D and E

Class D

Class E
Subclasses of B

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Polymorphism

A feature that allows one interface to be used for a


general class of actions

one interface, multiples methods.


Possible to design a generic interface to a group of
related activities. This helps reduce complexity by
allowing the same interface to be used to specify a
general class of action.

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

Object

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Object and Class

Broad terms that stands for many things


Ex : a student, a desk, a circle and a box
Contains data and methods
Properties of object known as data fields.
Objects behaviours are defined by methods.

data field 1
data field n
method 1
method n

object
MANAGEMENT & SCIENCE UNIVERSITY

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

TCS 2044 JAVA PROGRAMMING

Example
width
height
depth
Box

What to do with the box ?


Compute the volume of the box
Object : Box
Data : width, height and depth
method : computeVolume()
MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Classes
Structures that define objects.
A blueprint that defines what an objects data and method
will be.

data field : width,


height, depth

class Box
{
double width, height, depth;
double computeVolume()
{
return(width*height*depth);
}

Method :
computeVolume()

Box

MANAGEMENT & SCIENCE UNIVERSITY

10

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

TCS 2044 JAVA PROGRAMMING

Declaring and creating objects


Declaring objects
An object is an instance of a class.
Many instance can be created for the class.
A class can have many different objects.

Syntax to declare an object:

Classname objectName

Example
Many instances
can be created
for the class

Box class

Box b1;

b1
MANAGEMENT & SCIENCE UNIVERSITY

bn
11

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Creating object

The declaration does not create an object.

To create an object, we need to use new operator.


Syntax to create an
object:

ObjectName = new className();

Example
b1 = new Box();

MANAGEMENT & SCIENCE UNIVERSITY

Create an object b1, and


allocates
memory for it.

12

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Combine declaration and creating in one step


Syntax :
Classname objectName = new className();

Example
Box b1 = new Box();
Statement
Box b1;

Effect
null
b1

b1 = new Box();
b1

width
height
depth

Box object
MANAGEMENT & SCIENCE UNIVERSITY

13

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

TCS 2044 JAVA PROGRAMMING

Example of Using Objects


Main
class

The box
class

MANAGEMENT & SCIENCE UNIVERSITY

Output of the program

The program creates a Box


object and display its
volume

14

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Example 2 of Using Objects : Set value in main class


import java.io.*;
public class TestBox
{
public static void main(String arg[])
{
Box b1 = new Box(10, 6, 5);
System.out.println("The volume of the box is "+ b1.computeVolume());
}
}
public class Box
{
int width, height, depth;
Box ( int w, int h, int d )
{
width = w;
height = h;
depth = d;
}
double computeVolume ()
{
return ( width * height * depth);
}
MANAGEMENT & SCIENCE UNIVERSITY

15

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Example 3 of Using Objects : ask the user input values in main class
import java.io.*;
public class TestBox
{
public static void main(String arg[])
{
Box b1 = new Box();
//********** ask the user input for width, height and depth
b1. setData (w, h, d);
System.out.println("The volume of the box is "+ b1.computeVolume());
}
}
public class Box
{
int width, height, depth;
Box (){ }
void setData (int w, int h, int d){
width = w;
height = h;
depth = d; }
double computeVolume ()
{
return ( width * height * depth);
}
MANAGEMENT & SCIENCE UNIVERSITY

16

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Exercise
(a) Define a class named staff with the following variables
declarations:
private String name;
private String staff_ID;
provide a default constructor and another constructor with two
parameters. The constructor with parameters will assign the
two values of the arguments to the object attributes
accordingly.
Define a public instance method named display () that
functions to display the name and staff_id.
(b) Define another class named testing to test the staff
class. Use the input
dialog method from JOptionPane class to
get name and staff_ID values from the user. Call the display
() method to display the objects attributes through println
() method.
MANAGEMENT & SCIENCE UNIVERSITY

17

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Exercise

Write an application to calculate area of circle by using object


orientation. Run this application in Testing.java.
1)Create a class named Circle
2)Create an object named circle
3)Formula to calculate area is: radius* radius* Math.PI. Calculate
the area in class Circle.
4)Ask user to input radius.
5)Display your answer/output in class Circle

MANAGEMENT & SCIENCE UNIVERSITY

18

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Exercise

Write an application to calculate area of circle by using object


orientation. Run this application in Testing.java.
1)Create a class named Circle
2)Create an object named circle
3)Formula to calculate area is: radius* radius* Math.PI. Calculate
the area in class Circle.
4)Set the input for radius is 2.5 in main class
5)Display your answer/output in main class.

MANAGEMENT & SCIENCE UNIVERSITY

19

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

TCS 2044 JAVA PROGRAMMING

Differences between variables of


primitive Data types and object types
Primitive type

int i = 1

Object type

Circle c

reference

c: Circle
Created using
new Circle()

MANAGEMENT & SCIENCE UNIVERSITY

radius = 1

20

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Copying Variables of Primitive Data Types


and Object Types
Primitive type assignment
i=j

Object type assignment


c1 = c2

Before:

After:

Before:

c1

c1

c2

c2

MANAGEMENT & SCIENCE UNIVERSITY

After:

c1: Circle

c2: Circle

radius = 5

radius = 9
21

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Garbage Collection
As shown in the previous figure, after the
assignment statement c1 = c2, c1 points to the
same object referenced by c2.
The object previously referenced by c1 is no
longer useful. This object is known as garbage.
Garbage is automatically collected by JVM
(Java Virtual Machine).

MANAGEMENT & SCIENCE UNIVERSITY

22

TCS 2044 JAVA PROGRAMMING

CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3)

Garbage Collection (cont.)


TIP:
If you know that an object is no longer
needed, you can explicitly assign null
to a reference variable for the object.
The Java VM will automatically collect
the space if the object is not referenced
by any variable.
MANAGEMENT & SCIENCE UNIVERSITY

23

You might also like