You are on page 1of 3

Program to implement INHERITANCE:

import java.io.*;
class vehicle
{
int speed;
String color;
public vehicle()
{
speed=0;
color=”White";
}
public vehicle(int s, String c)
{
speed=s;
color=c;
}
public void disp()
{
System.out.println("Speed : "+speed+ "km/hr color: "+color);
}
}
class bicycle extends vehicle
{
String make;
public bicycle()
{
super();
make="Lady Bird ";
}
public bicycle(int s, String c, String m)
{
super(s,c);
make=m;
}
public void disp()
{
System.out.print("The Bicycle is : "+make+" and of ");
super.disp();
}
}
class motorcycle extends vehicle
{
int cc;
String make;
public motorcycle()
{
super();
cc=0;
make="Scooty pep";
}
public motorcycle(int s, String c,int cu, String m)
{
super(s,c);
cc=cu;
make=m;
}
public void disp()
{
System.out.print("The Motorcycle is : "+cc+"cc "+make+" of ");
super.disp();
}
}
class inheritvehicle
{
public static void main(String args[])
{
vehicle v[]=new vehicle[2];
bicycle b=new bicycle(10,"Black","Herculus");
v[0]=b;
motorcycle m=new motorcycle(80,"Black",180,"Herohonda");
v[1]=m;
for(int i=0;i<2;i++)
{
System.out.println("Vehicle "+(i+1));
v[i].disp();
}
}
}
OUTPUT:

D:\ravi>javac inheritvehicle.java

D:\ravi>java inheritvehicle

Vehicle 1

The Bicycle is : Herculus and of Speed : 10km/hr color: Black

Vehicle 2

The Motorcycle is : 180cc Herohonda of Speed : 80km/hr color: Black

You might also like