MY mENU


Thursday 9 February 2012

Interface in Java and Cloneable interface


Interface : It is a reference type, similar to a Class that can contain only constants, method signatures, and nested types. There are no method bodies (abstract methods) means anInterface is a specification of method prototype.
- We should implement all the methods in our implementation class other wise if we left any method without implementation we should declare our Class as abstract.


- An Interface may or may not have any methods.

- An Interface can contain variables which are public, static, and final by default.

Interfaces cannot be instantiated—they can only be implemented by classes or extended by other Interfaces.

- We can use Interface reference variable to refer Implementation Class objects.

- An Interface can't implement another Interface.

- An Interface can extend already existing Interface.

- A single Class can implement multiple Interfaces.



Example :

public interface OperateCar {

// method signatures
int turn(Direction direction, double radius, double startSpeed, double endSpeed);
int changeLanes(Direction direction, double startSpeed, double endSpeed);
int signalTurn(Direction direction, boolean signalOn);
int getRadarFront(double distanceToCar, double speedOfCar);
int getRadarRear(double distanceToCar, double speedOfCar);
......
// more method signatures
}

Cloneable Interface:


Cloneable - It's a Tagged/Marked interface.

Tagged/Marked interface means those interfaces don’t have any methods.
- We use these Tagged/Marked interfaces for the special purposes only.

If we want to Clone any object of Class, that Class must implement the Cloneable interface evan though there is no methods in that interface.

No comments:

Post a Comment