Login    New User    Forgot Password    Help      

Java Interface

Java Tutorials

As you've already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object's interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off.

Diagram showing interface

Example

// Defining interface
interface common
{
int c=12;
void line();
}

// Extending interface
interface ex extends common
{
int e=13;

}

// Defining base class
class base implements common
{
int b=11;
int i;
public void line()
        {
        for(i=0;i<=10;i++)
                {
                System.out.print("* *");
                }
        System.out.println();
        }

public void base()
        {
        line();
        System.out.println("Calling through base class");
        System.out.println("Accessing of drive class is not possible  ");
        System.out.println("Calling variable b of base class base  "+b);
        System.out.println("Calling variable c of interface  "+c);
        System.out.println("Accessing of ex interface is not possible ");
        }

 

}

// Interface implementation
class drive extends base implements ex
{
int d;

public void drive()
        {
        d=10;
        System.out.println("Calling through drive class");
        System.out.println("Calling variable d of own class  drive  "+d);
        System.out.println("Calling variable b of base class base  "+b);
        System.out.println("Calling variable c of interface  "+c);
        System.out.println("Calling variable e of extended interface  "+e);
        }

}

 

class callface
{
public static void main (String args[])
{

drive obd= new drive();
// Calling constructors
obd.drive();

 

base obb= new base();
obb.base();
}
}

h2>Output

Java Tutorials