5

Here's a thing: I have some basic class, for example:

abstract class Car(private val model: String = "no name")

and I want to have class extends Car, for example:

class Mazda(model: String) extends Car(model)

My question is: how to create Mazda instance without given model like that: new Mazda and have object with name no name?

5 Answers 5

5

To achieve your goal, you will have to define the default in the subclass:

class Mazda(model: String = "no name") extends Car(model)
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that, but imagine that I created one hundred subclasses and I don't wan't to write in every of them String = "no name".That's why I created supersclass constructor to avoid replicating my code. Now I want to use it.
I thought that might be your ultimate goal. If you expect to have many different classes you might want to take a different approach entirely and not rely on inheritance. For instance, if you're not adding distinct behavior or data to each of the subclasses, you might want make Car concrete and add a make property.
The way I'm doing it now is creating Car constructor without default argument and checking in class if(name = "") name = "no name" when in every class default value in constructor is "". I didn't find better solution.
2

I don't think your question has been fully answered yet, partly because you haven't accepted yakshaver's answer. That answer suggests you do this:

abstract class Car(private val model: String = "no name")
class Mazda(model: String = "no name") extends Car(model)

In this scenario, calling new Mazda gives you the result you want. But it is not entirely satisfying, since the magic string "no name" occurs in two different places. You might hope that you could work out a way to call the Car constructor with no arguments from inside the Mazda constructor, maybe something like this:

// does not compile!
class Mazda(model: String) extends Car(model) {
    def this() { super() } // attempt at an auxiliary constructor
}

Unfortunately, any auxiliary constructor has to end up calling, either directly or indirectly, the primary constructor for the class. So if you want to be able to specify the model in some cases for the construction of a Mazda, there is no way that you can sometimes call the Car constructor with no arguments.

If you want to avoid the duplication of the magic "no name" string, the best thing to do is to pull it out into a constant somewhere. E.g., like this:

object Car {
  val DefaultCarModel = "no name"
}
abstract class Car(private val model: String = DefaultCarModel)
class Mazda(model: String = DefaultCarModel) extends Car(model)

Comments

0

Not sure this is what you want, but with

abstract class Car(private val model: String = "no name")
class Mazda extends Car

you can now create a Mazda simply with

val car = new Mazda

and the value of model will be "no name"

2 Comments

I think he also wants to be able to do new Mazda("modelName") that calls Car("modelName")...
@Kane - that exactly what I need: new Mazda("3") when model is known but also new Mazda when I don't know model yet.
0

I feel that it is impossible and try to reason why:

  1. You essentially want an auxiliary constructor for Mazda that calls superclass constructor in a different way
  2. Auxiliary constructors will eventually call the default one, where superclass constructor is invoked.
  3. Superclass constructor only appears in one form in default constructor. (?)

Some languages like c++ can give you what you want, since constructors are independent of each other and call superclass constructors by themselves there. In java you can do both ways, dependent and independent. But then you have to write auxiliary constructor for every subclass, which is even more work.

Comments

0

How about extending something that has no name?

trait DefaultNameIsNoName extends Car {
  override name = "no name"
}

class Fiat extends Car with DefaultNameIsNoName

Or have another no-name class you can inherit from:

class NoNameCar extends Car with DefaultNameIsNoName

class Packard extends NoNameCar

But: what does it mean to have a car with no model name? Are you sure you're modeling a real thing?

1 Comment

Actually I just have trait which my abstract class Car extends. And there's real need to sometimes just create engine for Mazda and not for specific type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.