Answer the question
In order to leave comments, you need to log in
How to create an object in Java and, depending on the type, declare additional. parameters for the object?
That is, I have a Car object, it is limited to enum'om (sedan, hatchback, SUV, crossover), because there are several types of objects, I need to add additional parameters when creating an object, depending on the choice (sedan, hatchback - number in the doors, SUV - frame type, crossover - another parameter) How to implement this? Or is it somehow possible to implement this without enum?
Answer the question
In order to leave comments, you need to log in
Through class inheritance, you need to write an abstract base class Car, put all common fields there (number of doors, frame type) and inherit from it each type of car (class Sedan extends Car), and explicitly specify the number of doors in the constructor of the Sedan class
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question