import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Vehicle {
int price,speed,milage;
String name;
void setVehicleDetails (String a,int b,int c,int d){
name = a;
price = b;
speed = c;
milage = d;
}
void getVehicleDetails (){
System.out.println("Speed of "+name+" is "+speed);
System.out.println("price of "+name+" is "+price);
System.out.println("milage of "+name+" is "+milage);
}
}
class ElectricVehicle extends Vehicle{
int capacity,chargingTime,backupDays;
void setBatteryDetails(int a,int b , int c){
capacity =a;
chargingTime=b; backupDays=c;
}
void getBatteryDetails(){
System.out.println("capacity is "+capacity);
System.out.println("chargingTime is "+chargingTime);
System.out.println("backup is "+backupDays);
int price = capacity*chargingTime*backupDays;
System.out.println("Price of vehicle is "+price);
}
}
// we use constructornhere
// void ElectricVehicle(int capacity,int chargingTime,int backupDays){
// int price = capacity*chargingTime*backupDays;
// System.out.println("Price of vehicle is "+price);
//}
// if we have consructor in vehicle class then in electricvehicle class
// first vehicle constructor run hoga then electricvehicle
class Main {
public static void main(String[] args) {
System.out.println("Constructor In Inheritance");
//object for vehicle class
Vehicle car = new Vehicle();
car.setVehicleDetails("Baleno",800000,210,19);
car.getVehicleDetails();
// object for electricvejicle class
ElectricVehicle tesla = new ElectricVehicle();
tesla.setVehicleDetails("tesla",5000000,310,45);
tesla.setBatteryDetails(3000,4,8);
tesla.getBatteryDetails();
tesla.getVehicleDetails();
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: