...
/Solution Review: Calling a Constructor from a Constructor
Solution Review: Calling a Constructor from a Constructor
This review provides a detailed analysis to solve the 'Calling a Constructor from a Constructor' challenge.
We'll cover the following...
Solution
Press + to interact
// Car classclass Car {// Private Fieldsprivate String carName;private String carModel;private String carCapacity;// Default Constructorpublic Car() {this.carName = "";this.carModel = "";this.carCapacity = "";}// Parameterized Constructor 1public Car(String name, String model) {this.carName = name;this.carModel = model;}// Parameterized Constructor 2public Car(String name, String model, String capacity) {this(name, model); // calling parameterized Constructorthis.carCapacity = capacity; // Assigning capacity}// Method to return car detailspublic String getDetails() {return this.carName + ", " + this.carModel + ", " + this.carCapacity;}}class Demo {public static void main(String args[]) {Car car = new Car("Ferrari", "F8", "100");System.out.println(car.getDetails());}}
...