Master the four pillars of OOP, design patterns, and SOLID principles essential for technical interviews.
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
public class Car {
private String model;
private int speed;
private boolean engineStarted;
// Default constructor (no parameters)
public Car() {
this.model = "Unknown";
this.speed = 0;
this.engineStarted = false;
System.out.println("Car created with default constructor");
}
// Parameterized constructor
public Car(String model) {
this.model = model;
this.speed = 0;
this.engineStarted = false;
System.out.println("Car created: " + model);
}
// Constructor with multiple parameters
public Car(String model, int initialSpeed) {
this.model = model;
this.speed = initialSpeed;
this.engineStarted = true;
System.out.println("Car created: " + model + " at speed " + initialSpeed);
}
// Constructor overloading
public Car(String model, String color) {
this.model = model;
this.speed = 0;
this.engineStarted = false;
System.out.println("Car created: " + color + " " + model);
}
}
public class ConstructorDemo {
public static void main(String[] args) {
Car car1 = new Car(); // Default constructor
Car car2 = new Car("Toyota"); // One parameter
Car car3 = new Car("Honda", 50); // Two parameters
Car car4 = new Car("BMW", "Red"); // Different parameters
}
}Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts
Click to explore the answer and key concepts