0% found this document useful (0 votes)
19 views

Lab4 PP

This document implements inheritance in Java by creating a vehicle class with attributes like speed and color. It then creates subclasses bicycle and motorcycle that inherit from vehicle and add their own attributes like make and cc. The main method creates objects of these subclasses, stores them in a vehicle array, and calls the disp() method to display the attributes of each vehicle.

Uploaded by

satheesh_balaji
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lab4 PP

This document implements inheritance in Java by creating a vehicle class with attributes like speed and color. It then creates subclasses bicycle and motorcycle that inherit from vehicle and add their own attributes like make and cc. The main method creates objects of these subclasses, stores them in a vehicle array, and calls the disp() method to display the attributes of each vehicle.

Uploaded by

satheesh_balaji
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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