For of data implementation by restricting access

For this
project, I must develop some classes. The UML diagram of it included in the
file UML.png.

As
we can see, there we have ten classes:

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

1.       Main.java

2.       FileUtils.java

3.       User.java

4.       AccessRights.java
(enumeration)

5.       PurchaseOrder.java

6.       PurchaseRequisition.java

7.       Item.java

8.       Supplier.java

9.       DailyItemWiseSales.java

10.   TSB.java

So, what is Object-oriented programming (OOP)? It
is a programming paradigm based on the concept of objects. Objects may contain
data, in the form of fields, often known as attributes. Also, they may contain code,
in the form of procedures, often known as methods. A feature of objects is that
an object’s procedures can access and often modify the data fields of the
object with which they are associated. In OOP, computer programs are designed
by making them out of objects that interact with one another.

So, every
class of my program describes some object, like User, Item, Supplier, etc. Only
two classes are used for providing only methods. They are Main.java and FileUtils.java.
The first of them is used for interacting with a user with the help of
Terminal. The second is used for writing and reading information from files.

There are 4
main principles of OOP.

1.       Encapsulation:

 

It
means that the internal representation of an object is generally hidden from
view outside of the definition of the object. Typically, only the object’s own
methods can directly work with its fields.

Encapsulation
is the hiding of data implementation by restricting access to accessors and
mutators.

An
accessor is a method that is used to ask an object about itself. In OOP, these
are usually in the form of properties, which have a get method, which is an
accessor method. However, accessor methods are not restricted to properties and
can be any public method that gives information about the state of the object.

A
Mutator is a public method that is used to modify the state of an object while
hiding the implementation of exactly how the data gets modified. It’s the set
method that lets the caller modify the member data behind the scenes.

Hiding
the internals of the object protects its integrity by preventing users from
setting the internal data of the component into an invalid or inconsistent
state. This type of data protection and implementation protection is called
Encapsulation.

A benefit
of encapsulation is that it can reduce system complexity.

Java
provides a number of access modifiers to set access levels for classes,
variables, methods, and constructors. The four access levels are ?

·        
Visible to the package, the default. No modifiers are
needed.

·        
Visible to the class only (private).

·        
Visible to the world (public).

·        
Visible to the package and all subclasses (protected).

All classes
of my project, that describes objects, all variables are private. So they may
be accessed only with the help of methods of this class. So, we cannot change
any field of object of classes for Item, Supplier, Purchase Requisition,
Purchase Order, Daily Item-Wise Sales, or User from TBS class, which contains
this objects. In Purchase Order class, for example, I have next private fields:

 

/**

     * List of
items

     */

    private ArrayList itemCodes;

   

    /**

     * Quantity
of items

     */

    private HashMap quantity;

   

    /**

     * Date

     */

    private String dateRequired;

   

    /**

     * ID of
Purchase Order

     */

    private String id;

   

    /**

     * Sales
Manager, who adds this Purchase Order

     */

    private User salesManager;

 

It is
possible to change their value only with the help of special method of this
classes, they are called mutators (for example

/**

     * Setter

     * @param
code

     */

    public void setCode(String code) {

        this.code
= code;

    }

method in
Item class). For getting values from private variables there are special
methods, which are called accessors (for example

/**

     * Getter

     * @return

     */

    public ArrayList getItems() {

        return items;

    }

 

which gives
access to the Items list from class, that represents Daily Item-Wise Sales). Also, there are some private methods,
like

/**

     * Adds
users from file to list

     * @throws
IOException

     */

    private void addUsersFromFile() throws IOException {

        for (String s :
FileUtils.readFile(“Users.txt”)) {

           
String values = s.split(“,”);

           
users.add(new User(values0, values1,
AccessRights.valueOf(values2)));

        }

    }

in TSB.java
class. So we can use this method only in this class. With the help of
encapsulation, I protect object’s variables from unauthorized changes,
which may crash the program.

2.       Abstraction

 

Data
abstraction and encapsulation are closely tied together, because a simple
definition of data abstraction is the development of classes, objects, types in
terms of their interfaces and functionality, instead of their implementation
details. Abstraction denotes a model, a view, or some other focused
representation for an actual item.

“An
abstraction denotes the essential characteristics of an object that distinguish
it from all other kinds of object and thus provide crisply defined conceptual
boundaries, relative to the perspective of the viewer.” — G. Booch

In short,
data abstraction is nothing more than the implementation of an object that
contains the same essential properties and actions we can find in the original
object we are representing.

In my
project, there is no abstraction because we don’t have any standard interface for
objects.

3.       Inheritance

 

The
process by which one class acquires the properties(data members) and functionalities(methods)
of another class is called inheritance. The aim of inheritance is to provide
the reusability of code so that a class has to write only the unique features
and rest of the common properties and functionalities can be extended from the another
class.

Child Class:

The
class that extends the features of another class is known as child class, sub
class or derived class.

 

Parent Class:

The class
whose properties and functionalities are used(inherited) by another class is
known as parent class, super class or Base class.

Child and Parent
classes is a modular, derivative class that inherits one or more properties
from another class (called the superclass). The properties commonly include
class data variables, properties, and methods or functions. The superclass
establishes a common interface and foundational functionality, which
specialized subclasses can inherit, modify, and supplement. The software
inherited by a subclass is considered reused in the subclass. In some cases, a
subclass may customize or redefine a method inherited from the superclass. A
superclass method which can be redefined in this way is called a virtual
method.

So, there are
not any similar classes in my project, that is why I did not use inheritance.
It is possible to create parent class like Document.java for PurchaseOrder.java
and PurchaseRequisition.java classes, they have similar fields and methods, but
this is different documents and we don’t need to incorporate them. But all
classes in Java are inherited from Object class. So, I can say, that I use
inheritance, but it is invisible.

4.       Polymorphism

 

Polymorphism
means one name, many forms. Polymorphism manifests itself by having multiple
methods all with the same name, but slightly different functionality.

There
are 2 basic types of polymorphism. Overriding also called run-time
polymorphism. For method overloading, the compiler determines which method will
be executed, and this decision is made when the code gets compiled.
Overloading, which is referred to as compile-time polymorphism. The method will
be used for method overriding is determined at runtime based on the dynamic
type of an object.

If you can
grasp these four principles, OOP can be much of a breeze for you. It might take
more than one read, I encourage you to practically try it.

Polymorphism
is closely connected with inheritance. We are able to Override methods of the parent
class. As all classes in Java are inherited from Object class, that is why we
can override Object’s methods. In my project, I override toString() method.
This method returns a string representation of the object. In general, the
toString method returns a string that “textually represents” this
object. The result should be a concise but informative representation that is
easy for a person to read. It is recommended that all subclasses override this
method.

The
toString method for class Object returns a string consisting of the name of the
class of which the object is an instance, the at-sign character `@’, and the unsigned
hexadecimal representation of the hash code of the object. In other words, this
method returns a string equal to the value of:

 

 getClass().getName() + ‘@’ +
Integer.toHexString(hashCode())

 

But we need
a more convenient string representation of the object. For example, in Purchase
Order class toString() method is overridden in a next way.

    @Override

    public String toString() {

        String
itemsIDs = “”;

        String
total = “”;

        for (String item : itemCodes)
{

           
itemsIDs += item + “,”;

           
total += quantity.get(item) + “,”;

        }

        itemsIDs
= itemsIDs.substring(0, itemsIDs.length() – 1);

        total =
total.substring(0, total.length() – 1);

        return dateRequired +


+ purchaseManager.getLogin() + ”
” +
itemsIDs + ”
” + total;

    }

 

So, the string, which represents the object of this
class in a next way. The first element is the date when this Order is needed.
The next, which is after the symbol of the new line, is login of Purchase
manager, who has added this order. Then there are IDs of items, which are
needed and the quantity of every item.

Go Top
x

Hi!
I'm Rita!

Would you like to get a custom essay? How about receiving a customized one?

Check it out