Object Oriented Design Implementation

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Object Oriented Design Implementation as PDF for free.

More details

  • Words: 1,320
  • Pages: 8
Object Oriented Design Implementation

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; } However, object orientation is not simply grouping up of properties. For e.g. if you want know which bank is issuing US currency? You may ask about this to your friend. If it is so, above described currency class object should be capable to answer to your question. Let us modify the class to include that feature. class Currency{ private String country; private String bank; private String value; public String getBankInfo(){ return bank;

tURBOPLUS

Object Oriented Design Implementation } public void setBankInfo(String bank){ this.bank=bank; } }

Anatomy of a java class Clss name

class AJavaClass{ ------------------------Constructor method

AJavaClass(){} aJavaMethod(){ --------------}

Instance variable/static (class)variable

Static/non-static methods

Local variable

aJavaMethod(int x){} } Instance variable Exists when object instantiated. Each object has its own copy. Class variable (Static variable). Exists with class. All objects share. Local variable. Exits when the method being called. Static method

tURBOPLUS

Overloaded method

Object Oriented Design Implementation Does not require object reference to invoke. Non-static method Requires object reference to invoke Constructor Method A method having the name of class Overloaded method Constructor method/static or non-static method can not be differentiated by their name but from their parameter count or parameter type.

Constructor • • • •

Constructor or constructor method ensures its execution before executing any other method. Constructor will be called at the time of object instantiation. Constructor also provide a pattern that determines how an object will be created Constructor is the best place where one can place some code that has to be executed before executing any other methods.

Object Life Cycle 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

Class Loader

AnObject.class

Object Life Cycle

tURBOPLUS

Garbag e

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"); } public static void main(String args[]){ AnObject o1 = new AnObject(); AnObject o2 = new AnObject(); }

}

Output: Class Loaded Object Created Object Created Encapsulation of entity Code 100 355 400

Description Cartridge Ribbon CD

UoM Unit Doz Doz

Price 350 50 8

Vendor HP Epson MMX

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

price

tURBOPLUS

UoM

Product

vendor

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 Product [] stock getTotalCost() getTotalQty()

Inventory Product-A Product-B Product-C 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. An abstract class contains two types of methods abstract and non-abstract methods. An abstract method use abstract key word. abstract class Emp{ protected String name; protected String dept;

}

public abstract float computeSalary(float basic); public void getName(){ return name; }

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. class AnEmp extends Emp{

}

public float computeSalary(float basic){ float salary=0; salary=basic+(basic*10/100); return salary; }

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. public interface Employee{ String[] category={“analyst”,”programmer”,”designer”};

}

String getName(); void setName(String name); double computeSalary(double basic,String category);

When you implement methods of an interface, should be given access privilege, public, otherwise compiler shows an error following one. Weaker access privilege. The above interface is an example of generalization of object Employee, because various types of employee exist and business rule could be different for different organization for processing employee information, such as pay calculation. Interface for general class of action. 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(); } class Line implements Shape{ public void draw(){ //code for drawing a line } } class Circle implements Shape{ public void draw(){ //code for drawing a circle } }

Here object of Line and Circle can be assigned with interface Shape, for instance, Shape[] shapes={new Line(),new Circle(),new Line()}; shapes[0].draw(), produce line shapes[1].draw(), produce circle shapes[2].draw(), produce line

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. Primitive byte short int long float double void char

Wrapper class Byte Short Integer Long Float Double Void Character

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

Related Documents