Professional Documents
Culture Documents
Chapter 3
Object Oriented Design Implementation
To implement object oriented design java provides number of keywords that are
integral part of base language system.
class and interface are two key words used to implement abstraction about an
object.
For e.g. consider currency object. Government of different country use currency
of various denominations. However one can observe information like denomination
value, the bank responsible to issue the currency are present on the all currency. Then,
how to abstract that information? Below is the abstraction of currency for our example
purpose.
class Currency{
private String country;
private String bank;
private String value;
}
class Currency{
private String country;
private String bank;
private String value;
tURBOPLUS
Object Oriented Design Implementation
}
public void setBankInfo(String bank){
this.bank=bank;
}
}
class AJavaClass{
---------
--------- Instance
--------- variable/static
(class)variable
Constructor
method
AJavaClass(){}
Static/non-static
methods
aJavaMethod(){
--------
--------
} Local variable
aJavaMethod(int x){}
Overloaded
} method
Instance variable
Exists when object instantiated. Each object has its own copy.
Local variable.
Static method
tURBOPLUS
Object Oriented Design Implementation
Non-static method
Constructor Method
Overloaded method
Constructor
The following figure illustrate life cycle of a java class that consists of static block
and constructor.
Executes
construct
Instance of
AnObject.cl
Executes static
block No
reference
Garbag
Class e
Loader AnObject.class
tURBOPLUS
Object Oriented Design Implementation
According to above diagram you can predict the output of the following program.
class AnObject{
static{
System.out.println("Class Loaded");
}
AnObject(){
System.out.println("Object Created");
}
}
Output:
Class Loaded
Object Created
Object Created
Encapsulation of entity
The above table list information about various products. Each row provides
necessary details about a product. We can say that one row represent a real world object –
entity. Various diagrams can be used to represent an entity.
descripti
code UoM
tURBOPLUS
Object Oriented Design Implementation
The above picture shows some important properties of a product we discuss (our
abstraction). As you know, object reveals about itself through methods. Following is class
diagram that combine both method and properties.
Product
Code
Description
UoM
Price
Qty
Vendor
getCode()
getVendor()
getQty()
Object communication
Object uses its interface to communicate with other objects. For example
collection of product objects forms an Inventory system. Inventory object can be
represented as follows.
Inventory Inventory
Product-A
Product []
stock
Product-B
getTotalCost() Product-C
getTotalQty()
Product-D
The above figure shows that inventory object is composed of several product
objects. Inventory object can use Product object method, getQty() to get quantity
information, that will be stored in the “stock” property of Inventory object, so the
process of iteration accumulate all quantity information in “stock” and finally we invoke
Inventory object method getTotalQty() to get total stock status of the inventory.
tURBOPLUS
Object Oriented Design Implementation
Abstract class
Class concepts and class key word in java provide mechanism for encapsulating
abstraction, however there is one more key word, interface to achieve this. Before going
to that part, let us examine what is abstract class.
Java allows using abstract key word as prefix to class key word. When you
include this key word compiler add few more information to resulting bytecode. One of
the important information is “can not create object”.
So, if it is not possible to create object what is the purpose of it?
Abstract class provides facility to define general structure for class where, one can
differ implementation of business rules to another point of time or can specify a general
rule that will be utilized by implementing class.
The above abstract class declares few properties and methods, among one is
abstract. The abstract method will be given body (implementation of the method) in the in
the subclass. Compiler ensure implementation of all abstract method otherwise shows a
compile time error.
The error can be fixed either by implementing all abstract methods or by making
the subclass an abstract class.
The following is implementation of abstract class Emp.
tURBOPLUS
Object Oriented Design Implementation
Interface
In java interface key word is used to describe your abstraction without any
method implementation. Thus, an interface defines standard abstract methods, and final
properties. This is very helpful to describe high level view of an object without describing
inner details.
String getName();
void setName(String name);
double computeSalary(double basic,String category);
}
Another use of interface is creating object with general class of action. For
example, the word draw can be used in various contexts, meaning is context specific.
interface Shape{
void draw();
}
Here object of Line and Circle can be assigned with interface Shape, for instance,
tURBOPLUS
Object Oriented Design Implementation
In the above example all objects class implements Shape interface, so we can say
objects belonging to the same class/group, even though they are in same class, the
method draw give different output.
Here output is the result of organizing (writing) code different way in each
implementation of draw; this is also known as dynamic polymorphism or dynamic
method dispatch.
Java permits to implements more than one interface with same class, this facility
could be used to achieve multiple inheritances.
Wrapper classes
Some times you may want to perform some operations on primitives besides
arithmetic. For example how many 1 bits are there in 533? Java provides group of classes
to perform many such operations is known as wrapper classes.
Wrapper class wraps a primitive in an object. The wrapping could be done by
invoking constructor method.
java.lang package
Lang package contain several classes required for frequent use. Few examples are
wrapper classes, classes for Exception, System properties, Thread creation, reflection etc.
tURBOPLUS