ComenzarEmpieza gratis

Constructor overloading

We can overload the constructor with Polymorphism since it is like any other method. You will edit the Tesla class to have a second constructor, which takes a car's insurance status and allows you to create multiple object instances for the Tesla class using different constructors.

Este ejercicio forma parte del curso

Introduction to Object-Oriented Programming in Java

Ver curso

Instrucciones del ejercicio

  • Complete the second constructor method for the Tesla class to take an extra boolean isInsured as a parameter.
  • Inside the new constructor method for the Tesla class, set the isInsured class properties to the corresponding constructor parameter.
  • Create the second object instance of the Tesla class, called mySecondTesla, using the new constructor method passing the parameters "modelY", "black", and false as values.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

public class Main {
    
    static class Tesla {
        public String model;
        public String color;
        public boolean isInsured;
        
       public Tesla(String model, String color){
           this.model = model;
           this.color = color;
       }
       
       // Complete second constructor
        ____ ____(String model, String color,____ ____){
           this.model = model;
           this.color = color;
           // Set the isInsured property
           ____.____ = ____;
       }
       
    }

    public static void main(String[] args) {
        Tesla myFirstTesla = new Tesla("modelX", "red");
        // Create object instance with second constructor
        ____ ____ = new ____(____, ____, ____);
    }
}
Editar y ejecutar código