• Post Reply Bookmark Topic Watch Topic
  • New Topic

My dog is smarter than I in regards to polymorphism  RSS feed

 
William Peck
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In regards to http://www.javaranch.com/campfire/StoryPoly.jsp/teaching polymorphism to dogs, I wish I had that dog's smarts . . .

So I can't get past the third line before a big question mark forms over my head, clouding the rest of the article . . .

Ok, so I don't understand why this:
Dog d = new Dog();

is not simply this
d = new Dog();

so therefore I can't comprehend the point or legitimacy of this:
Animal d = new Dog();

basically, what is the point of the first "Dog" ? I don't get it

Also, d is name of a Dog object, right ? Well, why in the word would I name a dog simply "d" ? To me that's cryptic.
- I never have a one-character variable, so why would I have a one-character object name ? Why not name it dogA ? If I later see this: "d.play", wouldn't it be a little more helpful to see dogA.play ??? or perhaps Rover.play ?

Plus I often see Date d = new Date(); so it looks like "d" gets used frequently . . . And again, why the first "Date" ?

So, if you can help me understand this, I might have a fighting chance to keep up with Rover . . .
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there! You might consider buying Head First Java, or searching around the Beginner Java forum.... there's TONS of folks with similar questions.... as for yours......

The first "Dog" is declaring, in basic watered down terms, what the reference 'd' is going to be a reference to.

The reference can hold anything of that type or its subclasses.... if the reference type is an interface, then that means the reference can be for any of the implementing classes.

So...



It's good, when possible, to cram them together into one line:


In reality, Animal would probably be abstract.... meaning you wouldn't be able to instantiate it...


are things becoming clearer??

 
William Peck
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the reply.

>>The first "Dog" is declaring, in basic watered down terms, what the reference 'd' is going to be a reference to.
- but doesn't "new Dog() declare what "d" is ?

>>The reference can hold anything of that type or its subclasses
So "d" can hold an animal or any subclass, such as Dog ?

>>Animal someAnimal = new Animal(); // what the heck do you get?
- I don't know . . . a new Animal ???

>>In reality, Animal would probably be abstract.... meaning you wouldn't be able to instantiate it...
- What makes it abstract ? Why can't I instantiate it ?
 
William Peck
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. I do have Head First Java, but thanks for the suggestion.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Peck wrote:Ok, thanks for the reply.

>>The first "Dog" is declaring, in basic watered down terms, what the reference 'd' is going to be a reference to.
- but doesn't "new Dog() declare what "d" is ?

no.




>>The reference can hold anything of that type or its subclasses
So "d" can hold an animal or any subclass, such as Dog ?

Yes...




>>Animal someAnimal = new Animal(); // what the heck do you get?
- I don't know . . . a new Animal ???

>>In reality, Animal would probably be abstract.... meaning you wouldn't be able to instantiate it...
- What makes it abstract ? Why can't I instantiate it ?


Ha! You can't instantiate something that is abstract. We create abstract methods in order to keep people from instantiating things like:

Here's the deal... an Owl object would have way different BEHAVIOR than a Cheetah object, right? So an Animal... would it fly? would it run? when would it sleep? What would it look like? How many legs would it have?

Concrete classes have enough information to give specific behaviors to the abstract superclasses. All animals may have a breathe() method, with behaviors to apply respiration for the Animal... but would a Fish have the same breathe() as a Cheetah? Nah.

The bottom line is, without getting too complicated, polymorphism is used, partly, to apply specific behaviors.

Say we have 2 classes (excuse the pseudocode):




And let's say that Animal is a meaningful object in this example... and we'll instantiate one of each....


Now let's pop them into a list.... and add our animals


If we were to iterate through the List and call the play method for each animal, the foo reference would call the play method of the Animal object, and fido would call the play method of the Dog object. This allows for making our Collection of Animals, but keeping the specificity of the subclass behavior.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism is crammed into chapter 8 in head first java. I suggest you get through AT LEAST chapters 1 through 4 before delving into that.

Build a good foundation. Understand the syntax and then get into the more advanced concepts.
 
William Peck
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the patient and detail replies, I'll let it sink in and hopefully it will start to make sense.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't a CattleDrive specific question... lemme move us to beginning Java
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding single-letter variable names: if a method is approiately short, there's no issue remembering what something like "d" is. Depending on how I want the code to read, I'd either use d or dog, or name is something contextually useful, like currentDog, newDog, or whatever.
 
William Peck
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David, ok, sounds good. thanks.
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might help if you think of a variable as a container. The container has a name ('d'). The type of things the container can hold is determined by the type of the variable. In this case if 'd' is a Dog, it can only hold Dog objects. If it is an Animal, it can hold Dog objects or Cat objects or ... any type of object that has a parent object of Animal.

The stuff on the right side of the '=' sign is the actual object that you are putting into the container on the left.

Java checks if the stuff on the right can fit into the variable on the left. So if you tried to say

or

or

...
All the above are possible, but

will give you an error because a Knife object can't fit into an Animal variable (unless you know of some animal that I haven't heard of called a Knife).

Some other languages don't care if you try to put a knife into the variable, but Java tries to keep your code from breaking before you run it by checking this sort of thing when you compile.



 
William Peck
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marilyn, ok, that explanation helps too, thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!