1

I'm learning Java from ebooks etc and quite a few times I have come across something like this:

Example 1.

public interface TravelOptions()
{
    ...
}

public class Hovercraft implements TravelOptions
{
    ...
}

Main()
{
    TravelOptions speedy = new Hovercraft();

    ...
}

Example 2.

public class Animal
{
    ...
}

public class Dog extends Animal
{
    ...
}

Main()
{
    Animal Husky = new Dog();

    ...
}

My question is why do we bother assigning reference variable from another type say an Interface or a Superclass? Doesn't assigning to the original class instantion do the trick? What is the rationale behind all these?

Just to be more specific. Why not do these?

Hovercraft speedy = new Hovercraft();

or

Dog husky = new Dog();

Thanks in advance!

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
Scorpiorian83
  • 469
  • 1
  • 4
  • 17

0 Answers0