Wednesday, 15 February 2012

interface in java

there are some conditions when it is important that whenever a particular entity is created it must have some methods . Means that methods are mandatory for that entity.
Like if we created a bike then it must have a break , race, petrol tank etc. these things are mandatory for a bike.

So if we say that a class must have some particular methods in it then we used interface.
interface is like a class similar to a class, that can contain only constants, method signatures, and nested types.
There are no method bodies. interface cannot be instantiated they can only be implemented by classes or extended by other interface.
which class impement the interface, all the methods which declared in interface will be define in that class .

Example of interface

interface is declared similer as class only interface keyword is used insted of class
no braces for methods and method is terminated by semicolon(;)

public interface TestInterface {
void frontBreak(String bikename);
void backBreak(String bikename);

//class implements interface
To use an interface, you write a class that implements the interface.
When class implements an interface, it provides a method body for each of the methods declared in the interface.

public class bikeClass implements TestInterface {

public void frontBreak(String bikename) {
//write method body here


public void backBreak(String bikename) {
//write method body here


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...