Write a Java program to Constructor Overriding and Superclass Initialization


In this Java program, you have a parent class called Vehicle and a child class called Car. The Car class inherits from the Vehicle class, and both classes have constructors to initialize their properties. The program demonstrates how to initialize the properties of a child class while ensuring that the properties of the parent class are properly set. Here's an explanation of the program:

  • public class Initialization: This is the main class that contains the main method, the entry point of the program.
  • public static void main(String[] args) : The main method is where the program execution starts. It creates an object of the Car class and initializes its brand and model properties. Then, it prints these properties to the console.
  • Car car = new Car("Ford", "Porsche");: This line creates an object of the Car class with the brand set to "Ford" and the model set to "Porsche." When you create an instance of the Car class, the constructor of the Car class is called, and it, in turn, calls the constructor of the Vehicle class using super(brand) to initialize the brand property of the parent class.
  • System.out.println("Brand : " + car.brand); : This line prints the brand property of the car object to the console.
  • System.out.println("Model : " + car.model); : This line prints the model property of the car object to the console.
  • class Vehicle: This is the parent class, which has a property named brand.
  • Vehicle(String brand): The Vehicle class constructor accepts a brand parameter and initializes the brand property.
  • class Car extends Vehicle: This is the child class, which inherits from the Vehicle class and adds a property named model.
  • Car(String brand, String model): The Car class constructor accepts both brand and model parameters. It uses super(brand) to call the constructor of the parent class, initializing the brand property, and then initializes its own model property.

This program demonstrates how to initialize properties in a class hierarchy. The child class Car inherits the brand property from the parent class Vehicle and adds its own property, model. The constructors of both classes are used to ensure that the properties are properly initialized when an object is created. When you print the brand and model properties of the car object, you get "Ford" and "Porsche," respectively, demonstrating successful property initialization.


Source Code

public class Initialization	 // Main class
{
	public static void main(String[] args)
	{
		Car car = new Car("Ford", "Porsche");
		System.out.println("Brand : " + car.brand);
		System.out.println("Model : " + car.model);
	}
}
 
class Vehicle	// Parent class
{
	String brand;
 
	Vehicle(String brand)
	{
		this.brand = brand;
	}
}
 
class Car extends Vehicle	// Child class
{
	String model;
 
	Car(String brand, String model)
	{
		super(brand);
		this.model = model;
	}
}

Output

Brand : Ford
Model : Porsche