0

I recently saw this example code and I didn't know how I'd be able to easily find the answer to this question I had. In the code the Dog object, Cow object and Snake object are all declared as Animal objects. So is it valid to declare an object using a more generic class? For instance, could I declare any object as an Object (since all classes are subclasses of the object class)? What are the advantages/disadvantages of declaring specific or more general? Is it all for ease of readability?

class Animal {
  void whoAmI() {
    System.out.println("I am a generic Animal.");
  }
}
class Dog extends Animal {
  void whoAmI() {
    System.out.println("I am a Dog.");
  }
}
class Cow extends Animal {
  void whoAmI() {
    System.out.println("I am a Cow.");
  }
}
class Snake extends Animal {
  void whoAmI() {
    System.out.println("I am a Snake.");
  }
}

class RuntimePolymorphismDemo {

  public static void main(String[] args) {
    Animal ref1 = new Animal();
    Animal ref2 = new Dog();
    Animal ref3 = new Cow();
    Animal ref4 = new Snake();
    ref1.whoAmI();
    ref2.whoAmI();
    ref3.whoAmI();
    ref4.whoAmI();
  }
}
The output is

I am a generic Animal.
I am a Dog.
I am a Cow.
I am a Snake.
Jpaji Rajnish
  • 1,491
  • 4
  • 17
  • 35
  • I found this other question on SO that answers my question: http://stackoverflow.com/questions/14903145/what-is-the-difference-between-list-and-arraylist So the type that you declare an object dictates what methods and fields you can use. – Jpaji Rajnish Jun 20 '14 at 19:08
  • hmmm but now I'm confused again b/c why isn't the output in the code example above "I am a generic Animal" every time for each of the ref# instances? They should only have access to the methods in the Animal class since they were declared as Animal instead of their more specific animal names (Dog, Cow, Snake), right? – Jpaji Rajnish Jun 20 '14 at 19:27
  • No. They have access to their animal class methods AND their own class methods. no matter how you instantiate them. Whatever you put after the "new" determines what they have access to. Dog extends Animal so it has all the attributes of Animal plus dog specific stuff. – jollarvia Jun 20 '14 at 19:44
  • keep looking at docs on inheritance and polymorphism. One after the other until it finally clicks. (http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29) is a good one to start. – jollarvia Jun 20 '14 at 19:49

1 Answers1

0

You could instantiate every other class with object, but not primitives, though even they have wrappers that make them classes.

It's just not good object oriented practice because you want the program to be as fail safe as possible.

Put more varied methods into those classes and that's when you start to see the problems.

lets say Dog also has the method chewButt().

Animal dog = new Dog();
dog.chewButt();

No problem there. But that's not all you're allowed to do.

Animal dog = new Cow();

that compiles...

dog.chewButt();

now we have a problem. Cow can't reach its butt.

You want to be as stringent as possible when declaring the superclass but loose enough to get your job done fast. A reason you may want to be looser is something like this.

dogs and snakes are a little too different. I'mma create a new subclass Mammal that extends from Animal. I put dog and cow and whatever else in it.

now I can be assured I can do:

Mammal mouse = new Mouse();
mouse.suckleYoung();
cat.suckleYoung();
whale.suckleYoung();

but if I accidentally try:

Mammal snake = new Snake();

I want the compiler to complain now before I dig myself into a coding hole.

I hope that helps. Experience will definitely burn the points into your brain soon enough.

Edit: referencing comment: I still don't get what reasons there would be that I wouldn't always just declare my dog object like "Dog dog = new Dog();" vs. how it is in the example code "Animal dog = new Dog();

It's because your function working on dog is also assured to work with all Animals (dogs, cats, lizards). If you went with the other route you would have to go through the trouble of writing a separate function for each class of animal. What's worse is that every function you made would have the same code. Instead write the methods once in Animal and every extended class has that set of methods. Imagine having realized that the method you wrote was slightly wrong, you had to change one line but you had to do it in every animal's module because you didn't take the time to create an Animal superclass.

jollarvia
  • 349
  • 4
  • 9
  • got a chuckle from the methods haha. I still don't get what reasons there would be that I wouldn't always just declare my dog object like "Dog dog = new Dog();" vs. how it is in the example code "Animal dog = new Dog();". What's the point of declaring more general with Animal? Is there some advantage I don't know about? – Jpaji Rajnish Jun 20 '14 at 16:48