TCS2044 Chapter2 Object Oriented Programming Week3 (New)
TCS2044 Chapter2 Object Oriented Programming Week3 (New)
CHAPTER 2
OBJECT-ORIENTED
PROGRAMMING
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.
Object-Oriented Concept
Encapsulation
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.
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
Polymorphism
Object
data field 1
data field n
method 1
method n
object
MANAGEMENT & SCIENCE UNIVERSITY
Example
width
height
depth
Box
Classes
Structures that define objects.
A blueprint that defines what an objects data and method
will be.
class Box
{
double width, height, depth;
double computeVolume()
{
return(width*height*depth);
}
Method :
computeVolume()
Box
10
Classname objectName
Example
Many instances
can be created
for the class
Box class
Box b1;
b1
MANAGEMENT & SCIENCE UNIVERSITY
bn
11
Creating object
Example
b1 = new Box();
12
Example
Box b1 = new Box();
Statement
Box b1;
Effect
null
b1
b1 = new Box();
b1
width
height
depth
Box object
MANAGEMENT & SCIENCE UNIVERSITY
13
The box
class
14
15
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
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
Exercise
18
Exercise
19
int i = 1
Object type
Circle c
reference
c: Circle
Created using
new Circle()
radius = 1
20
Before:
After:
Before:
c1
c1
c2
c2
After:
c1: Circle
c2: Circle
radius = 5
radius = 9
21
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).
22
23