inheritance'
// Base Class Vehicle
class Vehicle {
// Private Fields
private String vehicle;
private int year;
private String model;
// Parameterized Constructor
public Vehicle(String vehicle, int year, String model) {
this.vehicle = vehicle;
this.year = year;
this.model = model;
}
// public method to print details
public void printDetails() {
System.out.println("Vehicle Type: " + vehicle);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
// Derived Class Car
class Car extends Vehicle {
// Private field
// Parameterized Constructor
public Car(String vehicle, int year, String model) {
super(vehicle, year, model); // calling parent class constructor
}
public void carDetails() { // details of car
printDetails(); // calling method from parent class
}
}
class Sample {
public static void main(String[] args) {
Car elantraSedan = new Car("Car", 2019, "Elantra"); // creation of car Object
elantraSedan.carDetails(); // calling method to print details
}
}