0% found this document useful (0 votes)
11 views2 pages

Import Java

The document contains a Java program that demonstrates serialization and deserialization of vehicle objects. It defines a parent class 'Vehicles' and two child classes 'Car' and 'Bike', each with attributes for color and name. The main class 'Test' creates instances of 'Car' and 'Bike', serializes them to a file, and then deserializes them to display their properties.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Import Java

The document contains a Java program that demonstrates serialization and deserialization of vehicle objects. It defines a parent class 'Vehicles' and two child classes 'Car' and 'Bike', each with attributes for color and name. The main class 'Test' creates instances of 'Car' and 'Bike', serializes them to a file, and then deserializes them to display their properties.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;

// Parent class
class Vehicles implements Serializable {
String color;

Vehicles(String color) {
[Link] = color;
}
}

// Child class Car


class Car extends Vehicles {
String name;

Car(String color, String name) {


super(color);
[Link] = name;
}

void display() {
[Link]("Color of Car: " + color);
[Link]("Name of the Car: " + name);
}
}

// Child class Bike


class Bike extends Vehicles {
String name;

Bike(String color, String name) {


super(color);
[Link] = name;
}

void display() {
[Link]("Color of the Bike: " + color);
[Link]("Name of the Bike: " + name);
}
}

// Main class
public class Test {
public static void main(String[] args) throws Exception {

Car c = new Car("Black", "BMW");


Bike b = new Bike("Green", "Royal Enfield");
// Serialization
FileOutputStream fos = new FileOutputStream("[Link]");
ObjectOutputStream oos = new ObjectOutputStream(fos);

[Link](c);
[Link](b);

[Link]();
[Link]();

// Deserialization
FileInputStream fis = new FileInputStream("[Link]");
ObjectInputStream ois = new ObjectInputStream(fis);

Car c1 = (Car) [Link]();


Bike b1 = (Bike) [Link]();

[Link]();
[Link]();

[Link]();
[Link]();
}
}

Output :

You might also like