Question
Design a Vehicle class hierarchy in Java. Write a test program to demonstrate
polymorphism.
(Program written by Santhosh Kumar S)
Program
import java.util.*;
abstract class Vehicle
{
boolean engstatus=false;
abstract void start();
abstract void moveForward();
abstract void moveBackward();
abstract void applyBrake();
abstract void turnLeft();
abstract void turnRight();
abstract void stop();
}
class Porsche extends Vehicle
{
void truckManufacture()
{
System.out.println("Porsche World Wide Car manufactures ");
}
void gearSystem()
{
System.out.println("\n automatic gearing system.. So don't worry !!");
}
void start()
{
engstatus=true;
System.out.println("\n engine is ON ");
}
void moveForward()
{
if(engstatus==true)
System.out.println("\nCar is moving in forward direction .. Slowly gathering speed ");
else
System.out.println("\n Car is off...Start engine to move");
}
void moveBackward()
{ if(engstatus=true)
System.out.println("\n Car is moving in reverse direction ...");
else
System.out.println("\n Car is off...Start engine to move");
}
void applyBrake()
{
System.out.println("\n Speed is decreasing gradually");
}
void turnLeft()
{
System.out.println("\n Taking a left turn.. Look in the mirror and turn ");
}
void turnRight()
{
System.out.println("\n Taking a right turn.. Look in the mirror and turn ");
}
void stop()
{
engstatus=false;
System.out.println("\n Car is off ");
}
}
class Volvo extends Vehicle
{
void truckManufacture()
{
System.out.println("Volvo Truck Manufactures");
}
void gearSystem()
{
System.out.println("\nManual Gear system...ooops....take care while driving ") ;
}
void start()
{
engstatus=true;
System.out.println("\n engine is ON ");
}
void moveForward()
{
if(engstatus==true)
System.out.println("\n truck is moving in forward direction .. Slowly gathering speed ");
else
System.out.println("\n truck is off...Start engine to move");
}
void moveBackward()
{ if(engstatus=true)
System.out.println("\n truck is moving in reverse direction ...");
else
System.out.println("\n truck is off...Start engine to move");
}
void applyBrake()
{
System.out.println("\n Speed is decreasing gradually");
}
void turnLeft()
{
System.out.println("\n Taking a left turn.. Look in the mirror and turn ");
}
void turnRight()
{
System.out.println("\n Taking a right turn.. Look in the mirror and turn ");
}
void stop()
{
engstatus=false;
System.out.println("\n Truck is off ");
}
}
class Vehicle1
{
public static void main(String[] args)
{
Porsche v1=new Porsche();
v1.truckManufacture();
v1.start();
v1.gearSystem();
v1.moveForward();
v1.applyBrake();
v1.turnLeft();
v1.moveForward();
v1.applyBrake();
v1.turnRight();
v1.moveBackward();
v1.stop();
v1.moveForward();
Volvo v2= new Volvo();
v2.truckManufacture();
v2.start();
v2.gearSystem();
v2.moveForward();
v2.applyBrake();
v2.turnLeft();
v2.moveForward();
v2.applyBrake();
v2.turnRight();
v2.moveBackward();
v2.stop();
v2.moveForward();
}
}
Download the Source Code
2 comments:
easy prog till now i hav seen in java..:) it will be better if all progs r like this...;)
Ha ha...written by Santhosh Kumar, thank him....But again it is a easy question..dats y...no concept and all...
Post a Comment