fork download
  1. class Car {
  2. String model;
  3. int year;
  4.  
  5. // Default Constructor
  6. public Car() {
  7. model = "Unknown";
  8. year = 0;
  9. }
  10.  
  11. // Parameterized Constructor
  12. public Car(String m, int y) {
  13. model = m;
  14. year = y;
  15. }
  16.  
  17. // Method to display car details
  18. public void display() {
  19. System.out.println("Model: " + model + ", Year: " + year);
  20. }
  21.  
  22. public static void main(String[] args) {
  23. // Creating objects using both constructors
  24. Car car1 = new Car();
  25. Car car2 = new Car("Toyota", 2020);
  26.  
  27. car1.display(); // Output: Model: Unknown, Year: 0
  28. car2.display(); // Output: Model: Toyota, Year: 2020
  29. }
  30. }
  31.  
Success #stdin #stdout 0.19s 55548KB
stdin
Standard input is empty
stdout
Model: Unknown, Year: 0
Model: Toyota, Year: 2020