child class need to define parent property in constructor java code example
Example: java child class constructor with parents attributes
public class Car {
private String name;
private String manufacturerName;
public Car(String name, String man) {
this.name = name;
this.manufacturerName = man;
}
public String getName() {
return name;
}
public String getManufacturerName() {
return manufacturerName;
}
}
public class ElectricCar extends Car {
public ElectricCar(String name, String man) {
super(name, man);
}
public void charge() {
System.out.println("Charging ...");
}
}
ElectricCar modelS = new ElectricCar("Model S","Tesla");
System.out.println(modelS.getManufacturerName());
modelS.charge();