-3

What is the advantage of writing:

Bicycle bike = new RoadBike(...);

instead of

RoadBike bike = new RoadBike(...);

Assuming RoadBike extends Bicycle of course.

I ask because even if I write

RoadBike bike = new RoadBike(...);

I can still use all the methods in Bicycle because of the extension, so what is the point of writing it the other way?

Thanks!

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
user3412900
  • 3
  • 1
  • 2
  • 1
    See [Java Tutorial on Polymorphism](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html). It describes your example rather well. – PM 77-1 Mar 12 '14 at 22:33

3 Answers3

2

If Bicycle were an interface, it would allow you to just pass around that interface and make your methods more "generic" or "polymorphic".

If you had another class, StreetBike,(and it implemented the Bicycle Interface) you could use that class and RoadBike to say call method 'ride' and each class would ride differently based on the implementation.

Public Interface Bicycle {
   public ride();
}

public StreetBike implements Bicycle{
  public ride(){
    System.out.println("I am riding on the street");
  }
}

public RoadBike implements Bicycle{
  public ride(){
    System.out.println("I am riding on the road");
  }
}

Taking it a step further we can use this SIMPLE example, but I think it gets the point across

//PERSON POJO
public Person {
  //properties
  public rideBike(Bicycle bike){
     bike.ride(); //could be StreetBike or RoadBike, depends on what you pass in.  That's the power of it.
  }
}
Dan
  • 979
  • 1
  • 8
  • 29
1

The clearest way to express polymorphism is via an abstract base class (or interface)

public abstract class Bicycle
{
   ...
   public abstract void ride();
}

This class is abstract because the ride() method is not defined for Bicycle. It will be defined for the subclasses RoadBike and MountainBike. Also, Bicycle is an abstract concept . It’s got to be one or the other.

So we defer the implementation by using the abstract class.

public class RoadBike extends Bicycle
{
...
    @Override
    public void ride()
    {
        System.out.println("RoadBike");
    }
}

and

public class MountainBike extends Bicycle
{
...
    @Override
    public void ride()
    {
        System.out.println("MountainBike");
    }
}

Now we can difference by calling Bicycle at runtime base class Bicycle but due to run time binding respective subclass methods will be invoked .

public static void main(String args)
{
    ArrayList<Bicycle> group = new ArrayList<Bicycle>();
    group.add(new RoadBike());
    group.add(new MountainBike());
    // ... add more...

    // tell the class to take a pee break
    for (Bicycle bicycle : group) bicycle.ride();
}

Running this would yield:

RoadBike
MountainBike
...

Hope this will clear you what the difference is !

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
0

The advantage is that Bicycle reference is more general and can refer to any object which has Bicycle in its inheritance hierarchy for example MountainBike. It offers more flexibility and does not binds you to concrete implementation. This is one of the basic principles in OOP. That said, there are some cases, like when used in short method when using Bicycle or RoadBikemakes no difference. Also there is the aspect of invoking overloaded methods which is determined by reference type at compile time (depending on the reference type, different method might be called even thought they point to the same object) but having those kinds of overlaoded methods is not advised.

You might get better explanation here.

Community
  • 1
  • 1
ps-aux
  • 11,627
  • 25
  • 81
  • 128