Software Reuse, Collections & Libraries

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

OOP Concepts

SOFTWARE REUSE; COLLECTION FRAMEWORK; CLASS LIBRARIES


I. Software Reuse

Software reuse is a term used for developing the software by using the existing software
components. Some of the components that can be reused are as follows:

 Source code (Code reuse)


 Design and architecture
 Software Documentation such as user manuals, requirements’ specifications and test
cases
 Prototypes

COTS (Commercial-off-the-shelf), a ready-made software, is an example of software reuse.

Advantages of software reuse

 Less effort
 Time-saving
 Reduce software development and maintenance costs
 Increase software productivity
 Utilise fewer resources
 Leads to a better quality software
 Improve software system interoperability
 Produce more standardised software

Software Reuse in OOP

In OOP, reusability can be achieved through several techniques such as:

 Inheritance / Class hierarchies


 Polymorphism
 Abstraction and Encapsulation
 Abstract Data Types
 Class libraries
 Application frameworks
 Design Patterns
 Interface
 Template classes
II. Collection Framework

What is a Collection Framework?

Although we can use an array to store a group of elements of the same type (either primitives
or objects), it does not support so-called dynamic allocation. Many applications may require
more complex data structure such as linked list, stack, hash table, sets, or trees.

In Java, dynamically allocated data structures (such


as ArrayList, LinkedList, Vector, Stack, HashSet, HashMap, Hashtable) are supported in
a unified architecture called the Collection Framework, which mandates the common
behaviours of all the classes.

A collection, as its name implied, is simply an object that holds a group / container of objects.
Each item in a collection is called an element. A framework, by definition, is a set of interfaces
that force you to adopt some design practices. A well-designed framework can improve your
productivity and provide ease of maintenance.

The collection framework provides a unified interface to store, retrieve and manipulate the
elements of a collection, regardless of the underlying and actual implementation. This allows
the programmers to program at the interfaces, instead of the actual implementation.

Java Collection Framework

The Java collection framework is a set of classes and interfaces that implement commonly
reusable collection data structures and are used to store and manipulate the data in the form
of objects. It provides various classes such as ArrayList, Vector, Stack, and HashSet, etc. and
interfaces such as List, Queue, Set, etc. for this purpose.
The Java Collection Framework package (java.util) contains:

1. A set of interfaces,

2. Implementation classes, and

3. Algorithms (such as sorting and searching).

Advantages of Collection Framework

The primary advantages of a collections framework are that it:

 Reduces programming effort by providing data structures and algorithms so you don't
have to write them yourself.
 Increases performance by providing high-performance implementations of data
structures and algorithms. Because the various implementations of each interface are
interchangeable, programs can be tuned by switching implementations.
 Provides interoperability between unrelated APIs by establishing a common language to
pass collections back and forth.
 Reduces the effort required to learn APIs by requiring you to learn multiple ad hoc
collection APIs.
 Reduces the effort required to design and implement APIs by not requiring you to
produce ad hoc collections APIs.
 Fosters software reuse by providing a standard interface for collections and algorithms
with which to manipulate them.
Collection vs Collections

Both Collection and Collections are part of the Java collection framework, but each serves a
different purpose.

Collection is a top level interface of the Java Collection Framework. Most of the classes in Java
Collection Framework inherit from this interface. List, Set and Queue are main sub interfaces
of this interface. JDK doesn’t provide any direct implementations of this interface. But, JDK
provides direct implementations of its sub
interfaces. ArrayList, Vector, HashSet, LinkedHashSet, PriorityQueue are some indirect
implementations of Collection interface.

Collections is a utility class in java.util package. It consists of only static methods which are
used to operate on objects of type Collection. It can achieve all the operations that you
perform on a data such as searching, sorting, insertion, manipulation and deletion. For
example, it has a method to find the maximum element in a collection, another method to
sort the collection, another method to search for a particular element in a collection and so
on.

Below are examples of the syntax used to define both Collection and Collections:

Collection: public interface Collection<E> extends Iterable<E>

Collections: public class Collections extends Object


Collection Classes

Collection classes, also known as Collections, are an OOP replacement for the traditional array
data structure.

Java collection class is used exclusively with static methods that operate on or return
collections. It inherits the Object class.

The important points about Java Collections class are:

 Java Collection class supports the polymorphic algorithms that operate on


collections.
 Java Collection class throws a NullPointerException if the collections or class objects
provided to them are null.

Below is a list of some important methods of Collections class.

Method Name Purpose of method


Collections.max() This method returns maximum element in the
specified collection.
Collections.min() This method returns minimum element in the given
collection.
Collections.sort() This method sorts the specified collection.
Collections.shuffle() This method randomly shuffles the elements in the
specified collection.
Collections.synchronizedCollection() This method returns synchronized collection backed
by the specified collection.
Collections.binarySearch() This method searches the specified collection for the
specified object using binary search algorithm.
Collections.disjoint() This method returns true if two specified collections
have no elements in common.
Collections.copy() This method copies all elements from one
collection to another collection.
Collections.reverse() This method reverses the order of elements in the
specified collection.
Collection

1. Types of Collection

There are three generic types of collection:

 Ordered lists allow the programmer to insert items in a certain order and retrieve
those items in the same order. An example is a waiting list. The base interfaces for
ordered lists are called List and Queue.

 Dictionaries / Maps store references to objects with a lookup key to access the
object's values. One example of a key is an identification card. The base interface for
dictionaries / maps is called Map.

 Sets are unordered collections that can be iterated and contain each element at most
once. The base interface for sets is called Set.

2. Collection Interface

The Collection interface, also referred to as Collection, is the interface which is implemented
by all the classes in the collection framework. It declares the methods that every collection
will have. In other words, we can say that the Collection interface builds the foundation on
which the collection framework depends.

Some of the methods of Collection interface are Boolean add (Object obj), Boolean addAll
(Collection c), void clear(), etc. which are implemented by all the subclasses of Collection
interface.
Below is the hierarchy of the Collection Interface:
Below are the different interfaces included in the Collection Interface:

 Iterator<E> interface

The Iterator<E> interface, declares the following three abstract methods:

boolean hasNext() // Returns true if it has more elements


E next() // Returns the next element of generic type E
void remove() // Removes the last element returned by the iterator

 Iterable<E> interface

The Iterable interface is the root interface for all the collection classes. The Collection
interface extends the Iterable interface and therefore all the subclasses of Collection interface
also implement the Iterable interface.

It contains only one abstract method. i.e., Iterator<T> iterator()

It returns the iterator over the elements of type T. All implementations of the collection
(e.g., ArrayList, LinkedList, Vector) must implement this method, which returns an object that
implements Iterator interface.

 Collection<E>

The Collection<E>, which takes a generic type E and read as Collection of element of type E,
is the root interface of the Collection Framework. It defines the common behaviours expected
of all classes, such as how to add or remove an element, via the following abstract methods:

// Basic Operations
int size() // Returns the number of elements of this
Collection
void clear() // Removes all the elements of this Collection
boolean isEmpty() // Returns true if there is no element in this
Collection
boolean add(E element) // Ensures that this Collection contains the given
element
boolean remove(Object element) // Removes the given element, if present
boolean contains(Object element)// Returns true if this Collection contains the
given element
 List<E> Interface

List interface is the child interface of Collection interface. It inhibits a list type data structure
in which we can store the ordered collection of objects. It can have duplicate values.

List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.

To instantiate the List interface, we must use:

1. List <data-type> list1= new ArrayList();


2. List <data-type> list2 = new LinkedList();
3. List <data-type> list3 = new Vector();
4. List <data-type> list4 = new Stack();

There are various methods in List interface that can be used to insert, delete, and access the
elements from the list.

 Set<E> Interface

Set Interface in Java is present in java.util package. It extends the Collection interface. It
represents the unordered set of elements which doesn't allow us to store the duplicate items.
We can store at most one null value in Set. Set is implemented by HashSet, LinkedHashSet,
and TreeSet.

Set can be instantiated as:

1. Set<data-type> s1 = new HashSet<data-type>();


2. Set<data-type> s2 = new LinkedHashSet<data-type>();
3. Set<data-type> s3 = new TreeSet<data-type>();
 Queue<E> Interface

Queue interface maintains the first-in-first-out order. It can be defined as an ordered list that
is used to hold the elements which are about to be processed. There are various classes like
PriorityQueue, Deque, and ArrayDeque which implements the Queue interface.

Queue interface can be instantiated as:

1. Queue<String> q1 = new PriorityQueue();


2. Queue<String> q2 = new ArrayDeque();

 Comparable<T> Interface

A java.lang.Comparable<T> interface specifies how two objects are to be compared for


ordering. It defines one abstract method:

int compareTo(T o) // Returns a negative integer, zero, or a positive integer

This ordering is referred to as the class's natural ordering, and the


class's compareTo() method is referred to as its natural comparison method.

It is strongly recommended that compareTo() be consistent


with equals() and hashCode() (inherited from java.lang.Object):

 If compareTo() returns a zero, equals() should return true.


 If equals() returns true, hashCode() shall produce the same int.

 Comparator<T> Interface

Besides the Comparable (or the natural ordering), you can pass a Comparator object into the
sorting methods (Collections.sort() or Arrays.sort()) to provide precise control over the
ordering. The Comparator will override the Comparable, if available.

The java.util.Comparator interface declares:

int compare(T o1, T o2) // Returns a negative integer, zero, or a positive


integer
III. Class Libraries

In object-oriented programming , a class library is a collection of prewritten classes or coded


templates, any of which can be specified and used by a programmer when developing an
application program.

The programmer specifies which classes are being used and furnishes data that instantiates
each class as an object that can be called when the program is executed. Access to and use
of a class library greatly simplifies the job of the programmer since standard, pretested code
is available that the programmer doesn't have to write.

Both network and desktop applications use class libraries. Class libraries contain code for
graphical user interface (GUI) elements such as buttons, icons, scroll bars and windows as
well as other non-GUI components.

All OOP languages have class libraries. Languages with .NET framework use Base Class Library
(BCL). Java languages use Java Class Library (JCL).

Advantages of class libraries

 Class libraries enhance code reuse by providing implementations of repetitive jobs.


Writing program applications from scratch can be an extremely detailed and
expensive process.
 Class libraries include all essential classes in a previously written coded format, which
not only simplifies programming but also increases code quality. Class template
customization is implemented according to specific programming requirements.
 Class libraries are continuously updated and time tested for reliability to reduce
programming language limitations. This is especially true in platform-independent
languages like Java.
Java Class Library

The Java Class Library (JCL) is a set of dynamically loadable libraries that Java applications can
call at run time. Because the Java Platform is not dependent on a specific operating system,
applications cannot rely on any of the platform-native libraries. Instead, the Java Platform
provides a comprehensive set of standard class libraries, containing the functions common to
modern operating systems.

JCL serves three purposes within the Java Platform:

 Like other standard code libraries, they provide the programmer a well-known set of
useful facilities, such as container classes and regular expression processing.
 The library provides an abstract interface to tasks that would normally depend heavily on
the hardware and operating system, such as network access and file access.
 Some underlying platforms may not support all of the features a Java application expects.
In these cases, the library implementation can either emulate those features or provide a
consistent way to check for the presence of a specific feature.

List of Library Classes in Java

Library classes Purpose of the class


Java.io Use for input and output functions.
Java.lang Use for character and string operation.
Java.text Use to deal with text, dates, numbers and messages
Java.awt Use for windows interface.
Java.util Use for develop utility programming.
Java.applet Use for applet.
Java.net Used for network communication.
Java.math Used for various mathematical calculations like power, square root
etc.
Java.nio / Java.net Use for networking access
Java.security Use to provide security and encryption services
Java.sql Use to provide access to SQL databases
Javax.script Use for access to scripting languages
Java.beans Use to provide ways to manipulate reusable components

You might also like