A Java interface is a bit like a
class, except you can only declare methods and variables in the
interface. You cannot actually implement the methods.
Syntax for declaring interface:
public interface interfacename{
//variable declaration here
//methods declaration here
public int 1 =10;
public void methodnmae();
}
As you can see, an interface is declared using the Java keyword "interface".
The interface contains variables and methods.
The variable can be accessed directly from the interface, like
System.out.println(interfacename.i);
The method, however, needs to be implemented by some class, before you can access it.
To use a interface in your class, append the keyword “implements” after your class name followed by the interface name
Example: class Car implements Vehicle
Implementing an Interface:
Before you can really use an interface, you must implement that interface in some class. Here is a class that implements the MyInterface interface:
*********************************************************
Interface definition:
public interface MyInterface {
public String h = "Hello";
public void sayHello();
}
**********************************************************
Class definition:
public class MyInterfaceImpl implements MyInterface {
public void sayHello() {
System.out.println(MyInterface.hello);
}
}
**********************************************************
Notice the "implements MyInterface" part of the above class declaration. This signals to the Java compiler that the MyInterfaceImpl class implements the MyInterface interface.
A class that implements an interface must implement all the methods declared in the interface. The methods must have the exact same signature (name + parameters) as declared in the interface. The class does not need to implement (declare) the variables of an interface. Only the methods.
Rules for Interfaces:
Interface Instances: we can not create instance for interfaces. Instead we can follow the below steps.
Once a class implements an interface you can use an instance of that class as an reference of that interface. Here is an example:
MyInterface myInterface = new MyInterfaceImpl();
myInterface.sayHello();
Notice how the variable is declared to be of the interface type MyInterface while the object created is of type MyInterfaceImpl. This is possible because the class MyInterfaceImpl implements the MyInterface interface. You can then reference instances of the MyInterfaceImpl class as instances of the MyInterface interface.
You cannot create instances of an interface by itself. You must always create an instance of some class that implements the interface, and reference that instance as an instance of the interface.
Example:
************************************************
public interface InterfaceSample {
/**
* We can not create the instance for an interface. So class must
* implement the interface to give implementation for the method.
*/
public void getName();
int i = 10;
}
==================================
public class InterFaceDemo implements InterfaceSample {
// Interface method
public void getName() {
System.out.println("getName of class");
}
// class own method
public void getName1() {
System.out.println("getName1 of class");
}
public static void main(String[] a) {
/**
* Following line creates object of class InterfaceDemo. So we can
* access all methods of class using this object.
*/
InterFaceDemo demo = new InterFaceDemo();
demo.getName1();
demo.getName();
/**
* As we could not create instance for an interface we can assign
* the object of class which implements the interface to reference
* of an interface as shown below. Following line creates the
* object of class but assigns it to reference of an interface. So
* here using this reference we can not access the method
* getName1(as it is class's own method not interface's method).
* So i commented the line of code.But we are able to access
* getName() method as it is interface's method.
*/
// It assigns the instance of class to reference of an interface.
// JUST FYI: This is way to achieve polymorphism. But here do not
// concentrate on polymorphism. Understand the assigning instance of
// class to reference of an interface.
InterfaceSample interfaceSample = new InterFaceDemo();
interfaceSample.getName();
// interfaceSample.getName1()
System.out.println(InterfaceSample.i);
}
}
************************************************
Note:-1: A class can implements interface and one interface can extends the another interface.
Example:
***************************************************
public interface CarInterface {
public void getModel();
}
====================================
public interface BikeInterface extends CarInterface {
public void getBikeModel();
}
=====================================
/**
* Here class implements BikeInterface where as BikeInterface extends
* CarInterfcae. So here we need to provide implementation for both the
@Override
public void getModel() {
System.out.println("get car model");
}
@Override
public void getBikeModel() {
System.out.println("get bike model");
}
public static void main(String a[]) {
ImplementClass class1 = new ImplementClass();
class1.getBikeModel();
class1.getModel();
CarInterface carInterface = new ImplementClass();
carInterface.getModel();
BikeInterface bikeInterface = new ImplementClass();
bikeInterface.getBikeModel();
/**
* getmodel1() is method of CarInterface but here we can acess
* this using BikeInterface reference because BikeInterfcae extends the
* CarInterface
*/
bikeInterface.getModel();
}
}
***************************************************
Example: Serializable is marker interface.
public interface Serializable {
}
Uses: Marker interfaces are used to provide some essential information to the JVM so that JVM may perform some useful operation.
Syntax for declaring interface:
public interface interfacename{
//variable declaration here
//methods declaration here
public int 1 =10;
public void methodnmae();
}
As you can see, an interface is declared using the Java keyword "interface".
The interface contains variables and methods.
The variable can be accessed directly from the interface, like
System.out.println(interfacename.i);
The method, however, needs to be implemented by some class, before you can access it.
To use a interface in your class, append the keyword “implements” after your class name followed by the interface name
Example: class Car implements Vehicle
Implementing an Interface:
Before you can really use an interface, you must implement that interface in some class. Here is a class that implements the MyInterface interface:
*********************************************************
Interface definition:
public interface MyInterface {
public String h = "Hello";
public void sayHello();
}
**********************************************************
Class definition:
public class MyInterfaceImpl implements MyInterface {
public void sayHello() {
System.out.println(MyInterface.hello);
}
}
**********************************************************
Notice the "implements MyInterface" part of the above class declaration. This signals to the Java compiler that the MyInterfaceImpl class implements the MyInterface interface.
A class that implements an interface must implement all the methods declared in the interface. The methods must have the exact same signature (name + parameters) as declared in the interface. The class does not need to implement (declare) the variables of an interface. Only the methods.
Rules for Interfaces:
- The class which implements the interface needs to provide functionality for the methods declared in the interface
- All methods in an interface are implicitly public and abstract
- Variables in interfaces are static and final by default in Java
- An interface cannot be instantiated
- An interface reference can point to objects of its implementing classes
- An interface can extend from one or many interfaces. A class can extend only one class but implement any number of interfaces
Interface Instances: we can not create instance for interfaces. Instead we can follow the below steps.
Once a class implements an interface you can use an instance of that class as an reference of that interface. Here is an example:
MyInterface myInterface = new MyInterfaceImpl();
myInterface.sayHello();
Notice how the variable is declared to be of the interface type MyInterface while the object created is of type MyInterfaceImpl. This is possible because the class MyInterfaceImpl implements the MyInterface interface. You can then reference instances of the MyInterfaceImpl class as instances of the MyInterface interface.
You cannot create instances of an interface by itself. You must always create an instance of some class that implements the interface, and reference that instance as an instance of the interface.
Example:
************************************************
public interface InterfaceSample {
/**
* We can not create the instance for an interface. So class must
* implement the interface to give implementation for the method.
*/
public void getName();
int i = 10;
}
==================================
public class InterFaceDemo implements InterfaceSample {
// Interface method
public void getName() {
System.out.println("getName of class");
}
// class own method
public void getName1() {
System.out.println("getName1 of class");
}
public static void main(String[] a) {
/**
* Following line creates object of class InterfaceDemo. So we can
* access all methods of class using this object.
*/
InterFaceDemo demo = new InterFaceDemo();
demo.getName1();
demo.getName();
/**
* As we could not create instance for an interface we can assign
* the object of class which implements the interface to reference
* of an interface as shown below. Following line creates the
* object of class but assigns it to reference of an interface. So
* here using this reference we can not access the method
* getName1(as it is class's own method not interface's method).
* So i commented the line of code.But we are able to access
* getName() method as it is interface's method.
*/
// It assigns the instance of class to reference of an interface.
// JUST FYI: This is way to achieve polymorphism. But here do not
// concentrate on polymorphism. Understand the assigning instance of
// class to reference of an interface.
InterfaceSample interfaceSample = new InterFaceDemo();
interfaceSample.getName();
// interfaceSample.getName1()
System.out.println(InterfaceSample.i);
}
}
************************************************
Note:-1: A class can implements interface and one interface can extends the another interface.
Example:
***************************************************
public interface CarInterface {
public void getModel();
}
====================================
public interface BikeInterface extends CarInterface {
public void getBikeModel();
}
=====================================
/**
* Here class implements BikeInterface where as BikeInterface extends
* CarInterfcae. So here we need to provide implementation for both the
* the methods in interfaces.
*/
public class ImplementClass implements BikeInterface {*/
@Override
public void getModel() {
System.out.println("get car model");
}
@Override
public void getBikeModel() {
System.out.println("get bike model");
}
public static void main(String a[]) {
ImplementClass class1 = new ImplementClass();
class1.getBikeModel();
class1.getModel();
CarInterface carInterface = new ImplementClass();
carInterface.getModel();
BikeInterface bikeInterface = new ImplementClass();
bikeInterface.getBikeModel();
/**
* getmodel1() is method of CarInterface but here we can acess
* this using BikeInterface reference because BikeInterfcae extends the
* CarInterface
*/
bikeInterface.getModel();
}
}
***************************************************
What is marker or tagged interface?
An interface that have no member is known as marker/tagged interface.Example: Serializable is marker interface.
public interface Serializable {
}
Uses: Marker interfaces are used to provide some essential information to the JVM so that JVM may perform some useful operation.
No comments:
Post a Comment