Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Monday, February 16, 2009

Program for Interface Demonstration

/**

* write another prog in which interface K1 declares method named methodK()

* and a variable intK which is intialised to 1.

* interface K2 extends K1 and declares method methodK().

* interface K3 extends K2 and declares method methodK().

* the return type of methodK() is void in all interfaces.

* class U implements K3.its version of methodK() displays the value of intK.

* instantiate U and invoke its method

*

*

*/

/**

* first base interface

*/

interface K1 {

// that would be printed

int intK = 1;

// will be used to display value of intK

void methodK();

}

/**

* extends K1

*

*

*/

interface K2 extends K1 {

// overrides K1 method

void methodK();

}

/**

* extends K2

*

*

*/

interface K3 extends K2 {

// overrides K2 method

void methodK();

}

/**

* implements interface K3 and displays value of intK

*

*

*/

class U implements K3 {

/**

* displays value of intK

*/

public void methodK() {

System.out.println("Value of intK : " + intK);

}

}

/**

* a class that will be executed

*

*

*/

public class InterfaceTest {

/**

* execution begn from here

*

* @param args

* commandline arguments

*/

public static void main(String[] args) {

// create instance of U

new U().methodK();

}

}

No comments:

Post a Comment

Followers