Ask Question
31 March, 06:54

In Java, write a constructor in such a way that a Vehicle object with color and type can be constructed directly via, say:

Vehicle ("Pink", "Car");

+5
Answers (1)
  1. 31 March, 09:58
    0
    In Java, a Constructor is similar to that of a method which will be called when creating a new object for the class. There is no return type for a Constructor. public Vehicle (String color, String type)

    {

    colourName = color;

    typeName = type;

    }

    In Java, you can use new operator to create an object of a class. Here, the object for the class Vehicle is created like Vehicle vehicle = new Vehicle ("Pink", "Car"); When creating the object of the class, the new operator calls the constructor. If there is no constructor defined explicitly, a default constructor is created by the compiler.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “In Java, write a constructor in such a way that a Vehicle object with color and type can be constructed directly via, say: Vehicle ("Pink", ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers