interface variables

Share This Topic -

Accessing interface variable :

One can declare variable as object references that uses an interface rather than a class type.

When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to.

 interface AreaCal
	final double pi = 3.14;
	double areacalculation(double r);

class Circle implements AreaCal
	public double areacalculation(double r)
		double ar;
		ar = pi*r*r;
		return ar;

class iface3
	public static void main(String args[])
		double area;
		AreaCal ac = new Circle();
		area = ac.areacalculation(10.25);
		System.out.println("Area of Circle is : " + area);

Output :
Area of Circle is : 329.89625

Here variable ac is declared to be of the interface type AreaCal,

it was assigned an instance of circle. Although ac can be used to access the areacalculation() method,

it cannot access any other members of the client class. An interface reference variable only has knowledge of the method declared by its interface declaration.