background preloader

Java Class Reference

Facebook Twitter

Collections Framework. Prior to Java 2, Java provided ad hoc classes such as Dictionary, Vector, Stack, and Properties to store and manipulate groups of objects.

Collections Framework

Although these classes were quite useful, they lacked a central, unifying theme. Thus, the way that you used Vector was different from the way that you used Properties. The collections framework was designed to meet several goals. The framework had to be high-performance. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hashtables) are highly efficient.The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.Extending and/or adapting a collection had to be easy.

Data Structures. The data structures provided by the Java utility package are very powerful and perform a wide range of functions.

Data Structures

These data structures consist of the following interface and classes: EnumerationBitSetVectorStackDictionaryHashtableProperties All these classes are now legacy and Java-2 has introduced a new framework called Collections Framework, which is discussed in next tutorial: The Properties Class. The Hashtable Class. Hashtable was part of the original java.util and is a concrete implementation of a Dictionary.

The Hashtable Class

However, Java 2 re-engineered Hashtable so that it also implements the Map interface. Thus, Hashtable is now integrated into the collections framework. It is similar to HashMap, but is synchronized. Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The Dictionary Class. The Stack Class. Stack is a subclass of Vector that implements a standard last-in, first-out stack.

The Stack Class

Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own. Stack( ) Apart from the methods inherited from its parent class Vector, Stack defines following methods: The following program illustrates several of the methods supported by this collection: This would produce the following result: stack: [ ] push(42) stack: [42] push(66) stack: [42, 66] push(99) stack: [42, 66, 99] pop -> 99 stack: [42, 66] pop -> 66 stack: [42] pop -> 42 stack: [ ] pop -> empty stack. The Vector Class. Vector implements a dynamic array.

The Vector Class

It is similar to ArrayList, but with two differences: The Enumeration Interface. The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.

The Enumeration Interface

This legacy interface has been superceded by Iterator. Although not deprecated, Enumeration is considered obsolete for new code. However, it is used by several methods defined by the legacy classes such as Vector and Properties, is used by several other API classes, and is currently in widespread use in application code. The methods declared by Enumeration are summarized in the following table: Following is the example showing usage of Enumeration. This would produce the following result: SundayMondayTuesdayWednesdayThursdayFridaySaturday. The BitSet Class. Packages. Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.

Packages

A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management. Some of the existing packages in Java are:: java.lang - bundles the fundamental classesjava.io - classes for input , output functions are bundled in this package Programmers can define their own packages to bundle group of classes/interfaces, etc. It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations, annotations are related. Since the package creates a new namespace there won't be any name conflicts with names in other packages. Interfaces. Abstraction. Abstraction refers to the ability to make a class abstract in OOP.

Abstraction

An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. Encapsulation. Overriding. In the previous chapter, we talked about super classes and sub classes.

Overriding

If a class inherits a method from its super class, then there is a chance to override the method provided that it is not marked final. The benefit of overriding is: ability to define a behaviour that's specific to the subclass type which means a subclass can implement a parent class method based on its requirement. In object-oriented terms, overriding means to override the functionality of an existing method. Example: Let us look at an example. class Animal{ public void move(){ System.out.println("Animals can move"); }} class Dog extends Animal{ public void move(){ System.out.println("Dogs can walk and run"); }} public class TestDog{ public static void main(String args[]){ Animal a = new Animal(); // Animal reference and object Animal b = new Dog(); // Animal reference but Dog object a.move();// runs the method in Animal class b.move();//Runs the method in Dog class }} This would produce the following result:

Polymorphism. Polymorphism is the ability of an object to take on many forms.

Polymorphism

The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. Inheritance. Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order. When we talk about inheritance, the most commonly used keyword would be extends and implements. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object. Quick Reference Guide. Streams, Files and I/O. The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. Exceptions Handling.