Interfaces in Java Programming
Previous | Home | Next |
In the Java programming language, 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.
Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
Interfaces are defined with the following syntax :-
(visibility) interface InterfaceName (extends other interfaces) {
constant declarations
member type declarations
abstract method declarations
}
The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the abstract keyword is not required.
simple interface may be:public interface Animal
{
boolean chasePrey(Prey p);
void eatPrey(Prey p);
}
The member type declarations in an interface are implicitly static and public,
but otherwise they can be any type of class or interface.
Syntax for implementing interface
... implements InterfaceName[, another interface, another, ...] ...
Classes may implement one or no of interfaces
by the following way classes can implement interfaces.
Public class Lion implements Animal
public boolean chasePrey(Prey p)
{
// programming to chase prey p (specifically for a lion)
}
public void eatPrey (Prey p)
{
// programming to eat prey p (specifically for a lion)
}
}
Previous | Home | Next |