Java does not support multiple inheritance. Classes in java cannot have more than one superclass. For instance a definition like,
class A extends B extends C
{
…………………………….
…………………………….
}
The Java programming language does not permit multiple inheritance but interfaces provide an alternative. Interfaces are not part of the class hierarchy, although they work in combination with classes.
In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.
An interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. it is the responsibility of any class that implements the derived interface to define all the methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. Once interface is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces.
An interface serve as a means to define the protocol for a class without worrying about the implementation detail.
Defining Interfaces
Defining an interface is similar to creating a new class.Like classes, interfaces contain methods and variables but with a major difference. The different is that interface is that interfaces define only abstract methods and final fields. This means that interfaces do not specify any code to implement these methods and data fields contain only constants.
Therefore, it is the responsibility of the class that implements an interface to define the code for implementation of these methods.
Syntax for defining an interface is very similar to that for defining a class. The general syntax of an interface definition is as follow.
access interface Inerfacename
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
Here, access is either public or not used. When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared. When it is declared as public, the interface can be used by any other code. interface is the keyword and Interfcename is any valid Java variable(just like class names).
Variables are declared as follows:
static final type VaraibleName= Value;
All variables are declared as constants. Methods declaration will contain only a list of methods without any body statements.
return-type methodName1(parameter_list);
All the methods in an interface are abstract , it is not necessary to precede the methodName with abstract keyword.
Here is an example of an interface definition that contains two variables and one method.
interface item
{
static final int code=1001;
static final String name=”Fan”;
void display();
}
Note that the code for the method is not included in the interface and the method declaration simply ends with a semicolon. The class that implements this interface must define the code for the method.
Another example of an interface is:
interface Area
{
final static float pi= 3.142F;
float compute (float x,float y);
void show ();
}
Extending interfaces
Like classes, interfaces can also be extended. That is, an interface can be subinterfaced from other interfaces. The new subinterface will inherit all the members of the superinterface in the manner similar to subclasses. This achieved using the keyword extends as shown below:
interface Interfacename2 extends Interfacename1
{
Body of name2
}
For ex, put all the constants in one interface and the methods in the other. This will enabled to use the constants in classes where the methods are not required.
Ex,
interface Itemconstants
{
int code= 1001;
String name= “Fan”;
}
interface Item extends ItemConstants
{
Void display();
}
The interface item would inherit both the constants code and name into it. Note that the variables name and code are declared like simple variables. It is allowed because all the variables in an interface are treated as constants although the keywords final and static are not present.
It can be possible to combine several several interfaces together into a single interface.
Ex,
interface ItemConstants
{
Int code =1001;
String name= “fan”;
}
interface ItemMethods
{
Void display();
}
interface Item extends ItemConstants, ItemMethods
{
……………………
……………………
}
While when an interface extends two or more interfaces, they are separated by commas.
It is important to remember that an interface cannot extend classes. This would violate the rule that an interface can have only abstract methods and constants.
Implementing Interfaces
An interfaces is a template for a class which used as “Superclasses” whose properties are inherited by classes. It is therefore necessary to create a class that inherits the given interface. This is done as follows.
class Classname implements Interfacename
{
Body of class name
}
Here the class Classname “ implements” the interface Interfacename. The keyword implements is used. A more general form of implementation such as
class Classname extends superclass implements interface1,interface2,…….
{
body of class name
}
This shows that a class can extend another class while implementing interfaces.
When a class implements more than one interface, they are separated by a comma.
Accessing Interface Variables
Interfaces can be used to declare a set of constants that can be used in different classes. This is similar to creating header files in C++ to contain a large number of constants. Since such interfaces do not contain methods, there is no need to worry about implementing any methods. The constant values will be available to any class that implements the interface. The values can be used in any method, as part of any variable declaration, or anywhere where we can use a final value.
Ex,
interface A
{
int m= 10;
int n= 50;
}
class B implements A
{
int x=m;
void method(int size)
{
…………….
…………….
if (size<n)
…………….
}
}
Accessing implementation through interface reference
It can be possible to declare variables as object references that use an interface rather than a class type.
Any instance of any class that implements the declared interface can be referred to by such a variable. When a method is called through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This is one of the key features of interfaces. The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them. The calling code can dispatch through an interface without having to know anything about the “callee.”
Posted by: Ruchita Pandya
No comments:
Post a Comment