In: Computer Science
JAVA
1. Create an Interface Vehicle. Declare one public void method in it paint( ).
2. Create a class Car. Implements Vehicle. It's paint() method prints "Car Painted". It's drive( ) method prints "Car Driven".
3. Create a class Bus. Implements Vehicle. It's paint() method prints "Bus Painted" . It's drive( ) method prints "Bus Driven".
4. Create a method AutoWork which accepts an object of type Vehicle. The method makes a call to the vehicle's paint( ) and drive() methods.
5. In the main method of your Main class, create a reference and object of Car class. Pass it to the AutoWork method.
6. In the main method of your Main class, create a reference and object of Bus class. Pass it to the AutoWork method.
------
7. In the main method of your Main class, create a reference v of Vehicle interface and object of Car class. Pass it to the AutoWork method. (You may have to remove/comment the call to the drive() method in AutoWork)
8. Using the same reference v, create object of Bus. Pass it to the AutoWork method. (Again you may have to remove/comment the call to the drive() method in AutoWork)
CODE :
import java.util.*;
interface Vehicle
{
public void paint();
}
class Car implements Vehicle
{
public void paint()
{
System.out.println("Car
painted");
}
public void drive()
{
System.out.println("Car
Driven");
}
}
class Bus implements Vehicle
{
public void paint()
{
System.out.println("Bus
painted");
}
public void drive()
{
System.out.println("Bus
Driven");
}
}
class JavProg
{
public void AutoWork(Vehicle v)
{
v.paint();
//v.drive();
}
public static void main(String args[])
{
JavProg tester =new
JavProg();
Vehicle v;//reference of vehicle
interface
Car c = new Car();
v = c;//holding car object
tester.AutoWork(v);//calling the
auto work passing car object
Bus b = new Bus();
v = b;
tester.AutoWork(v);
}
}
OUTPUT :