Inheritance in Java
Similar entities grouped together as objects in Java
Similar entities are grouped together as objects in Java. Each object definition in Java consists of properties, methods, and constructors which are methods used to create instances or new objects. Properties in Java are referred to as fields. All of this information is held in a class of the object, a sort of blueprint or prototype for the object under consideration. Such an object 'class' may constitute of the sole content of a single .Java program that compiles to a .class file.
Let us consider an object of class 'book'. For simplicity, let us assign this object three properties - title, pages, type, and printType. ‘Printtype’ holds either hardcover or paperback, while ‘type’ holds the broad category such as fiction, non-fiction, biography, encyclopedia and so on. Suppose that the methods of the book class include open, close, and goToPage. A developer may be working with alphabetically indexed dictionaries. He may require more from the object - a property that holds information on the languages of the dictionary, a property that holds information about the convention used to organize the dictionary, and a method ‘goToWord’ which allows a user to browse a given word on a certain page. What would be the best solution? Should a brand new object class be created? If so, the book object's properties and the open, close, and go to page methods must be defined from scratch. What if the 'book' object class was used and regular structured functions (not OO methods) were written to accomplish the extra functions? Such a program would not conform to Java's design guidelines. Fortunately, Java offers class inheritance to solve such problems. In class inheritance, a given class may extend or inherit properties and methods from one direct superclass; and an unlimited number of subclasses may inherit from and extend the given class.
In the 'book' example, the programmer may simply declare 'book' as the superclass of his 'dictionarybook' class. This way, the 'dictionarybook' class would simply inherit all of the fields and methods of the 'book' object, and the developer could still use all of these.
In addition, new specific properties and methods such as 'findWordonPage' and 'browseWord' could be added to the object. In this example, class inheritance is practically accomplished by the following line of code in the Java language.
class dictionaryBook extends book
{
.
.
Java Code
.
.
}
What is the 'book' class a direct subclass of? The object class is at the top of the Java object hierarchy. Every class inherits from the Java Object class. The words 'extends Object' need not be explicitly stated in the declaration; any object declaration without the 'extends' clause is taken to extend the Object class by default:
class book
{
.
.
Java Code
.
.
}
Now, if the developer wanted further language-to-language dictionary and language dictionary classifications, he could declare these two types of dictionaries as subclasses of the 'dictionarybook' class (a class may have many subclasses). Also, a method in a superclass may be overridden by a method in a subclass if the subclass also implements the method. Everytime an object of the subclass's type calls an overridden method 'A', it executes the 'A' within the subclass and not the 'A' in the parent class.
The means by which the outside world interacts with an entity is called an interface. For example, the buttons below the screen and the remote control constitute the interface of a television. Java allows developers to model software interfaces. An object, in addition to inheriting from a superclass or the object superclass, may implement any number of interfaces. An interface may also implement any number of other interfaces. The lines of commands or 'code' within each method in the Java object class is called the implementation of the method. An interface simply contains method declarations followed by semicolons, to merely 'publish' the behavior. The object that uses the interface actually 'implements' the methods within the interface. That is, the object contains all the code that describes the behavior of all the methods of the interface it uses. On the other hand, a class that extends another class need not contain any reference to the superclass's methods. The code for these methods is in the parent class and the methods are all available to the subclass. The subclass should implement the superclass method 'A' (override method 'A') only if additional functions relevant to the subclass should be carried out in method 'A'. The behavior of an object implementing an interface is better defined and formalized, and the object is constrained to implement or contain code that describes the behavior of every method declared in the interface.
interface myInterface
{
method1;
method2;
method3;
}
class myClass implements myInterface
{
method1
{
.
Code for method1
.
}
method2
{
.
Code for method2
.
}
method3
{
.
Code for method3
.
}
}
Related classes and interfaces are stored in a Java package. Packages are used to store related Java classes and interfaces just as cabinets are used to store related files. However, packages are not physically located anywhere, nor do they 'contain' the classes that make them up - package names are only used to refer to and classify related Java programs. While a cabinet occupies a physical 'space' in a specific location and has size dimensions, a 'package' is simply a namespace. The Java language consists of a massive set of packages that together form the API (Application Programming Interface). Programmers may use this vast resource to gain access to useful classes, interfaces and the methods that operate these classes. The most up-to-date version of the Java Platform API Specification contains the complete listing for all packages, interfaces, classes, fields, and methods.
A package’s name is made up of descriptive words separated by periods. Custom packages from third parties may use any naming convention; however, Java packages are usually of the form java.packagename.objectname (core Java) or javax.packagename.objectname (enterprise packages). More than three name levels may be used if appropriate. The following is a sample of frequently used packages:
|
java.awt |
Drawing, graphics, and UI related classes |
|
java.io |
Classes related to file and Stream Input and Output |
|
javax.servlet |
Classes used to process Internet forms and requests |
|
java.util |
Wide range of useful functions and structures that hold data in several different ways |
|
javax.swing |
User Interface Design |
|
java.lang |
Key classes and objects used to hold and perform a wide variety of operations on Java primitive data types (java.lang.Integer, java.lang.Boolean etc.) |
If a developer wishes to use objects from a package in a class, import statements could be used to make all the objects in the package available to the class at run time. Otherwise, the compiler will not be able to identify what references correspond to un-imported objects and what the classes in the program mean, and will throw errors at the spots where such references occur.
import java.util.ArrayList;
import java.io.*;
class myObject extends someOtherObject
{
.
.
Java Code
.
.
.
}
In the above example, the developer specifies the 'import java.util.ArrayList' directive based on the preference to use methods pertaining specifically to the ArrayList object in the java.util package only. 'Import java.io.*' is used to import all the classes and objects that make up the java.io package. Since imported classes and packages are linked together with the current class at runtime, it is best to include, whenever possible, the lowest possible number of packages and specify individual classes rather than packages.. Note that since Java is case sensitive, 'ArrayList' rather than 'arraylist' is used, as the same word in upper case differs in meaning from the same word in lower case.