0

With the code below:

public class Animal {
   ....// class stuff here
}

public class Cat extends Animal {
   ....// class stuff here
}

When trying to create a new Cat object, what is the difference between?:

    Cat myCat = new Cat(); //and
    Animal myCat = new Cat();

I've read previously with lists that you should declare the most generic on the left (i.e.

List<String> = new LinkedList<String>();) so that if ever you want to change the implementation to use an ArrayList instead, there's less code to change.

Does it follow in ALL cases that you should always declare the most generic (least specific) on the laft (in this case Animal myCat = new Cat();)?

Jonny
  • 3,807
  • 8
  • 31
  • 48
  • 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) – Rohit Jain Oct 10 '12 at 11:43
  • Nothing is ever always right or always wrong... common sense should be applied in each case (experience helps form "common sense") – Yaneeve Oct 10 '12 at 11:44

5 Answers5

2

There is no difference in what is created. Only difference is in the type of the reference. If you have reference to Animal, and try to use a method of Cat which is not in Animal, compiler will give an error. If you call a method which is overriden (note: not same as overloading) in Cat, it will correctly call the method of the Cat, even though reference is to Animal.

hyde
  • 60,639
  • 21
  • 115
  • 176
1

The most specific that is relevant. Otherwise, all of the variables would be Object.

As a rule of thumb, if your logic needs to call a method specific to Cat, then you should declare the variable as Cat. If it does not, then it should be an Animal if you need any method of Animal, and so on.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
1

lets say we have the following code

public class Animal {
  public void doSomething() {
       // ...
  }
}

public class Cat extends Animal {
  public void miau() {
       // ...
  }
}

the difference is

Cat myCat1 = new Cat(); //and
Animal myCat2 = new Cat();
myCat1.miau(); //ok
myCat1.doSomething(); //ok
myCat2.miau(); // doesn't compile
myCat2.doSomething(); // ok

It depends on the sitation which one you sould use. An example would be a list. If you know you only put Cat's into this list declare it as:

ArrayList<Cat> list = new ArrayList<Cat>();

But if you know you put all sorts of animals in there you need to make the declaration as follows

ArrayList<Animal> list = new ArrayList<Animal>();

You could also do the following:

ArrayList<Object> list = new ArrayList<Object>();

But that is not good coding style. Because if you wanted to call a method like miau you first need to check if the element you want to call it on is a Cat. And then you even need to cast it.

MarcDefiant
  • 6,649
  • 6
  • 29
  • 49
1

The difference between

Cat myCat = new Cat(); //and 
Animal myCat = new Cat();

is that the former is programming to an concrete implementation while the latter is programming to an interface/supertype.

This latter approach is preferred so that the actual runtime object is not bound in your code. And the class using your animal class doesn't have to know about the actual animal objects (which can be quite a large number apart from just a Cat).

So you can do animal.makeSound() with the latter which makes you reference the animal polymorphically(A Cat/any other animal in your case)

Anurag Sen
  • 75
  • 5
0

Just to explain in Layman's language, that it all depends upon what you want, I'll give a very simple example, because the rest of the explanation you can get from the link I provided..

  • Suppost you want to calculate how many Cats are there in your Basket, then of course you cannot go with the 2nd case.. You will have to create a List<Cat> and store Cat reference into them.. Because an Animal reference can also point to Dog right?? And thus a List<Animal> can hold a Dog.. So, you may not get a correct count of Cat.. Or, you can get, but a little bit of more work..

  • And if you just want to have information about Animal in general, like how many Animals of various types in total you have, you can happily go with the 1st one.. Create an object using Animal reference.. That can hold all your Animals..

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525