Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Monday, February 16, 2009

Second Program for Interface Demonstration

package day7;

/**

* Write a program which illustrates how to declare interfaces and implement

* them in various classes. interfaces AntiLockBrakes,CruiseControl and

* PowerSteering declare optional functionality for an automobile. Each

* interface declares one method which has the same name as its interface. the

* abstract Auto class is extended by the Model1,Model2 and Model3 classes.

* PowerSteering is available for Model1 objects. AntiLockBrakes and

* CruiseControl are available for Model2 objects. CruiseControl is available

* for Model3 objects. Instantiate each of these classes and invoke methods

*

*

*/

/**

* base interface with AntiLockBrackes facilities

*

*/

interface AntiLockBrakes {

void AntiLockBrakes();

}

/**

* base interface with CruiseControl facilities

*

*

*/

interface CruiseControl {

void CruiseControl();

}

/**

* base interface with PowerSteering functionalities

*

*

*/

interface PowerSteering {

void PowerSteering();

}

/**

* abstract class that would be inherited

*

*

*/

abstract class Auto {

abstract public void display();

}

/**

* it implements PowerStreeing and extends Auto

*

*

*/

class Model1 extends Auto implements PowerSteering {

/**

* overrides the method of PowerSteering interface

*/

public void PowerSteering() {

System.out.println("\nDisplay from Model1:PowerSteering()!!");

}

/**

* display PowerStreeing()

*/

public void display() {

PowerSteering();

}

}

/**

* implements AntiLockBrakes, CruiseControl and extends Auto

*

*

*/

class Model2 extends Auto implements AntiLockBrakes, CruiseControl {

/**

* overrides AntiLockBrakes

*/

public void AntiLockBrakes() {

System.out.println("\nDisplay from Model2:AntiLockBrakes()!!");

}

/**

* overrides CruiseControl

*/

public void CruiseControl() {

System.out.println("\nDisplay from Model2:CruiseControl()!!");

}

/**

* displays AntiLockBrakes()

*/

public void display() {

AntiLockBrakes();

CruiseControl();

}

}

/**

* Implements cruisecontrol and extends Auto class

*

*

*/

class Model3 extends Auto implements CruiseControl {

/**

* implements CruiseControl

*/

public void CruiseControl() {

System.out.println("\nDisplay from Model3:CruiseControl()!!");

}

// displays CruiseControl

public void display() {

CruiseControl();

}

}

/**

* class where execution starts

*

*

*/

public class InterfaceTest1 {

/**

* exectuion begin from here

*

* @param args

* commandline arguemtns

*/

public static void main(String[] args) {

// create instance of classes and call methods

Model1 m1Obj = new Model1();

m1Obj.display();

Model2 m2Obj = new Model2();

m2Obj.display();

Model3 m3Obj = new Model3();

m3Obj.display();

}

}

1 comment:

Followers