23
TCS 2044 JAVA PROGRAMMING MANAGEMENT & SCIENCE UNIVERSITY CHAPTER 2 OBJECT ORIENTED PROGRAMMING (Week 3) CHAPTER 2 OBJECT-ORIENTED PROGRAMMING 1

TCS2044 Chapter2 Object Oriented Programming Week3 (New)

Embed Size (px)

DESCRIPTION

OOP

Citation preview

Information System DevelopmentCHAPTER 2
OBJECT-ORIENTED PROGRAMMING
*
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.
TCS 2044 JAVA PROGRAMMING
*
Mechanism that binds together code and the data it manipulated, and keeps both safe from outside interference and misuse.
Encapsulation
*
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
TCS 2044 JAVA PROGRAMMING
*
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.
Inheritance
*
Subclasses of B
*
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.
Polymorphism
*
Ex : a student, a desk, a circle and a box
Contains data and methods
Objects behaviours are defined by methods.
Object and Class
*
Object : Box
method : computeVolume()
*
Structures that define objects.
A blueprint that defines what an object’s data and method will be.
class Box
Box
Method :
computeVolume()
*
A class can have many different objects.
Example
Declaring objects
TCS 2044 JAVA PROGRAMMING
*
The declaration does not create an object.
To create an object, we need to use new operator.
ObjectName = new className();
memory for it.
*
Syntax :
Statement
Effect
*
The program creates a Box object and display its volume
Main class
{
}
}
double computeVolume()
*
Example 2 of Using Objects : Set value in main class
import java.io.*;
{
System.out.println("The volume of the box is "+ b1.computeVolume());
}
}
{
*
Example 3 of Using Objects : ask the user input values in main class
import java.io.*;
{
b1. setData (w, h, d);
System.out.println("The volume of the box is "+ b1.computeVolume());
}
}
width = w;
height = h;
depth = d; }
double computeVolume ()
*
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 object’s attributes through println () method.
TCS 2044 JAVA PROGRAMMING
*
Exercise
Write an application to calculate area of circle by using object orientation. Run this application in Testing.java.
Create a class named Circle
Create an object named circle
Formula to calculate area is: radius* radius* Math.PI. Calculate the area in class Circle.
Ask user to input radius.
Display your answer/output in class Circle
TCS 2044 JAVA PROGRAMMING
*
Exercise
Write an application to calculate area of circle by using object orientation. Run this application in Testing.java.
Create a class named Circle
Create an object named circle
Formula to calculate area is: radius* radius* Math.PI. Calculate the area in class Circle.
Set the input for radius is 2.5 in main class
Display your answer/output in main class.
TCS 2044 JAVA PROGRAMMING
*
Created using
new Circle()
1
reference
*
i
c2
1
j
Before:
2
i
*
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).
TCS 2044 JAVA PROGRAMMING
*
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.
class TestBox
{
+ b1.computeVolume());
double computeVolume()