Polymorphism in Java

class Vehicle
{
public int doors; public int seats;
public int wheels;
Vehicle()
{
wheels=4; doors=4; seats=4;
}
}
//This class inherits Vehicle.java
class Car extends Vehicle
{
public String toString()
{
return "This car has "+seats+" Seats, "+doors+" Doors "+"and "+wheels+" wheels.";
}
}
//This class inherits Vehicle.java
class MotorCycle extends Vehicle
{
MotorCycle()
{
wheels=2; doors=0; seats=1;
}
void setSeats(int num)
{
seats=num;
}
public String toString()
{
return "This motorcycle has "+seats+" Seats, "+doors+" Doors "+"and "+wheels+" wheels.";
}
}
//This class inherits Vehicle.java public class Truck extends Vehicle
class Truck extends Vehicle
{
boolean isPickup;
Truck()
{
isPickup=true;
}
Truck(boolean aPickup)
{
this();
isPickup=aPickup;
}
Truck(int doors, int seats, int inWheels, boolean isPickup)
{
this.doors=doors; this.seats=seats;
wheels=inWheels;
this.isPickup=isPickup;
}
public String toString()
{
return "This "+(isPickup?"pickup":"truck")+" has "+seats+" Seats, "+doors+" Doors "+"and "+wheels+"wheels.";
}
}
//This class tests the classes that inherit Vehicle.java
public class VehiclesTest
{
public static void main(String args[])
{
MotorCycle mine = new MotorCycle();
System.out.println(mine);
Car mine2 = new Car();
System.out.println(mine2);
mine2.doors=2;
System.out.println(mine2); 
Truck mine3 = new Truck(); 
System.out.println(mine3);
Truck mine4 = new Truck(false);
mine4.doors=2;
System.out.println(mine4);
 
}
}
 


Output:
This motorcycle has 1 Seats, 0 Doors and 2 wheels.
This car has 4 Seats, 4 Doors and 4 wheels.
This car has 4 Seats, 2 Doors and 4 wheels.
This pickup has 4 Seats, 4 Doors and 4wheels.
This truck has 4 Seats, 2 Doors and 4wheels.