Program Definition 18 ::
Interface LuminousObject declares lightOff() and lightOn() methods. Class SolidObject is extended by Cone and Cube. Class LuminousCone extends Cone and implements LuminousObject. Class LuminousCube extends Cube and implements LuminousObject. Instantiate the LuminousCone and LuminousCube classes. Use interface reference to refer to those objects. Invoke the methods of the LuminousObject interface via the interface reference.
interface LuminousObject
{
public void lightOn();
public void lightOff();
}
class SolidObject
{
}
class Cube extends SolidObject
{
}
class Cone extends SolidObject
{
}
class LuminousCone extends Cone implements LuminousObject
{
public void lightOn()
{
System.out.println( " lightOn() from LuminousCone class " );
}
public void lightOff()
{
System.out.println( " lightOff() from LuminousCone class " );
}
}
class LuminousCube extends Cube implements LuminousObject
{
public void lightOn()
{
System.out.println( " lightOn() from LuminousCube class " );
}
public void lightOff()
{
System.out.println( " lightOff() from LuminousCone class " );
}
}
class Prog18
{
public static void main( String args[] )
{
LuminousObject lObj[] = new LuminousObject[2];
lObj[ 0 ] = new LuminousCone();
lObj[ 1 ] = new LuminousCube();
for ( LuminousObject l : lObj )
{
l.lightOn();
}
for ( LuminousObject l : lObj )
{
l.lightOff();
}
}
}
/*
D:\MCA\JAVA >javac Prog18.java
D:\MCA\JAVA >java Prog18
lightOn() from LuminousCone class
lightOn() from LuminousCube class
lightOff() from LuminousCone class
lightOff() from LuminousCone class
*/
0 comments :