Tuesday 29 September 2015

Inheritance,Interfaces and Packages

Single and Multiple Inheritance


Java’s form of inheritance, as you learned in the previous sections, is called single inheritance.
Single inheritance means that each Java class can have only one superclass (although any given
superclass can have multiple subclasses).

In other object-oriented programming languages, such as C++ and Smalltalk, classes can have more than one superclass, and they inherit combined variables and methods from all those classes. This is called multiple inheritance. Multiple inheritance can provide enormous power
in terms of being able to create classes that factor just about all imaginable behavior, but it can
also significantly complicate class definitions and the code to produce them. Java makes
inheritance simpler by being only singly inherited.


Interfaces and Packages

Java has two remaining concepts to discuss here: packages and interfaces. Both are advanced
topics for implementing and designing groups of classes and class behavior.

Recall that Java classes have only a single superclass, and they inherit variables and methods from that superclass and all its superclasses. Although single inheritance makes the relationship
between classes and the functionality those classes implement easy to understand and to design,
it can also be somewhat restricting- in particular, when you have similar behavior that needs
to be duplicated across different “branches” of the class hierarchy. Java solves this problem of
shared behavior by using the concept of interfaces.

An interface is a collection of method names, without actual definitions, that indicate that
a class has a set of behaviors in addition to the behaviors the class gets from its superclasses.
Although a single Java class can have only one superclass (due to single inheritance), that class
can also implement any number of interfaces. By implementing an interface, a class provides
method implementations (definitions) for the method names defined by the interface. If two
very disparate classes implement the same interface, they can both respond to the same method
calls (as defined by that interface), although what each class actually does in response to those
method calls may be very different.

The final new Java concept for today is that of packages.

Packages in Java are a way of grouping together related classes and interfaces. Packages
enable modular groups of classes to be available only if they are needed and eliminate
potential conflicts between class names in different groups of classes.


The class libraries in the Java Developer’s Kit are contained in a package called java.
The classes in the java package are guaranteed to be available in any Java implementation,
and are the only classes guaranteed to be available across different implementations.
The java package itself contains other packages for classes that define the
language itself, the input and output classes, some basic networking, and the window
toolkit functions. Classes in other packages (for example, classes in the sun or netscape
packages) may be available only in specific implementations.

By default, your Java classes have access to only the classes in java.lang (the base
language package inside the java package). To use classes from any other package, you
have to either refer to them explicitly by package name or import them in your source
file.

0 comments:

Post a Comment