F
F
Fortage2020-02-12 11:04:23
Java
Fortage, 2020-02-12 11:04:23

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

1 answer(s)
A
Andrey Ryabov, 2020-02-12
@Fortage

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 question

Ask a Question

731 491 924 answers to any question