0

I have been reading up on inheritance in java, and I have a fairly simple problem that I couldn't find on the manual pages.

Take this example.

I want define a class first:

Bycicle bike = new Bicycle();

Later on I want to say that it is a mountain bike, so that it belongs to a certain subclass and set a property speficit to mountain bike (say numberOfGears).

Is that possible in this order? Or do I need to say it is a mountainbike straight away?

4
  • What @Marco ment was MountainBike mbike = (MountainBike) bike; Commented Nov 10, 2014 at 14:36
  • How would that work exactly, so Mountainbike bike = new Mountainbike()? I would keep the info in Bike? Commented Nov 10, 2014 at 14:37
  • Maybe some of the basic tutorials from Oracle might help you even more to get started: docs.oracle.com/javase/tutorial. Commented Nov 10, 2014 at 14:43
  • Oh your link points to those tutorials.. uhm Commented Nov 10, 2014 at 14:45

3 Answers 3

5

If you know you will only use Bycicle's methods, then declare and initialize the variable as Bycicle:

Bycicle bike = new Bicycle();

If you want to change the class implementation but still use only Bycicle methods, then declare the variable as Bycicle and initialize it as the specific class implementation, in this case, MountainBike:

Bycicle bike = new MountainBike();

If you know you will use specific methods of the class implementation, then you have two choices:

  • Initialize as the class implementation and in the specific part you need to call a method from this class implementation, use downcasting:

    ((MountainBike)bike).mountainBikeMethod();
    

    I don't recommend this approach since if you change how you initialize the class then you should be careful when changing these downcastings as well, otherwise you will get RuntimeException, specifically ClassCastException for downcasting to a wrong class.

  • Declare and initialize as the class implementation, then forget about using the super class type since that's not what you need:

    MountainBike bike = new MountainBike();
    
Sign up to request clarification or add additional context in comments.

3 Comments

This is the way to go
Be careful with casting though, as its a common source for runtime exceptions
@MatiCicero note added when you were adding the comment.
0

If you'd like to say it is a mountain bike you need to create a MountainBike instance, not just Bicycle. Then you can just downcast it. But this seems a bit unnatural so either refer to that bicycle as to the MountainBicycle in the first place or adapt it to be a mountain bicycle using adapter design pattern.

Comments

0

An instanciated object from a class can't be changed to other class later.

Anyway a MounteinBike is still a bike. Then you can do:

Bycicle bike = new MountainBike();

or

MountainBike mountainBike = new MountainBike();

When it is possible is preffered to use the higher level of abstraction, In this case the first example.

Comments

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.