1

I'm recently learning java, i came from PHP background. I'm stuck with a concept that i didn't quite understand, so we have a parent class called (Animal) and a child class called (Wolf), from what i know, when you want to create a new instance of the class (Wolf) for example, this is the code you write:

Wolf wolf1 = new Wolf();

But in the books i found this second way that also get you same results:

Animal wolf1 = new Wolf();

Second thing, is when you have an interface (let's call it (myInterface)) and the class (Wolf) is implementing it, i found a third way of instanciating the class (Wolf):

myInterface wolf1 = new Wolf();

The questions are: why this works? and why doing it in the first place? is there a simple example that makes it clear why you should instanciate your class using the second and the third way?

  • 1
    possible duplicate of [What does it mean to "program to an interface"?](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Tom Jul 28 '15 at 18:17
  • A `Wolf` is an `Animal` (as a subclass of `Animal`) that is why the first one works. The second one is similar, when `Wolf` implements and interface, `Wolf` becomes also that interface `myInterface`. – innoSPG Jul 28 '15 at 18:18

2 Answers2

3

This is the benefit of object oriented programming. If you have Animal class and Cat that extends Animal, Dog that extends Animal, then you can write code that is less duplicated:

public void beAnnoying(Animal animal) {
    animal.makeSound();
}

As far as why does this work? In real life you can use an "Is-A" relationship for inheritance. A dog "Is-A" Animal, and a cat "Is-A" Animal also. So if all anmials can make a sound, then why can't you make a method that calls this generically?

Edit: Because i can't answer in comment i'll answer here. What if you want to say Wolf wolf = new Wolf(); then assign the varible a dog later? You can't with this (you'd have to create another variable), but if you had Animal Wolf = new Wolf(); then did wolf = new Dog(); this works fine. I'm sure there are other reasons I just am unsure.

Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
  • From the example you gave i can clearly understand why you gave (animal) the data type of (Animal), so you can pass all the objects derived from the (Animal) class using polymorphism. but when you simply want to instanciate the class (Wolf) why bother doing it with (Animal) and (myInterface) notation ? that's what i can't get my head around. – أسامة الزاهد Jul 28 '15 at 18:34
  • When you don't NEED polymorphism, just don't use it :) – Henrique Barcelos Jul 29 '15 at 18:06
1

As the others have said, you're describing polymorphism. It's the idea that one parent class can be used to describe many different subclasses. You have an Animal parent class and a Wolf subclass already. Let's also say that you want to make a Dog subclass as well. A Dog is also an Animal, so any functions (methods in Java) or variables (fields in Java) found in Animal can be used in Dog. So if you have a method eat in your Animal class,

public void eat()
{
   hunger_counter++;
}

you can call that method in your Dog class without an error.

dog.eat();

Another way you can look at it that really helped me: Think of having an array of Animals.

Animal[] animals = new Animal[4];

We want the first animal to be a Wolf and the rest of them to be a dog. Rather than having one array of Wolf objects and another array of Dog objects, we can use our Animals[] array directly.

animals[0] = new Wolf();
animals[1] = new Dog();
animals[2] = new Dog();
animals[3] = new Dog();

They're all of the type Animal, but they'll also have the added fields and methods found within the Wolf/Dog classes.

Lee Presswood
  • 210
  • 2
  • 15