Ask Question
11 August, 16:36

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");

+2
Answers (1)
  1. 11 August, 16:43
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers