?
Solved

Polymorphism is confusing me

Posted on 2011-04-22
3
Medium Priority
?
284 Views
Last Modified: 2012-05-11
I have two classes one extending the other. In the parent class(Mammal), I have a variable with value assigned to it and a method. In the sub class(Zebra) I have a parameter with the same name and a method with the same name. When I said Mammal m = new Zebra(); and called m.name and m.makeNoise(); for m.name it is displaying the value in mammal and for m.makeNoise(), it is displaying the value in zebra. I have pasted my code below.
package ch1;

public class ZooKeeper {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new ZooKeeper().go();
	}
	
	public void go()
	{
		Mammal m = new Zebra();
		System.out.println("Name: "+m.name+" "+"Noise: "+m.makeNoise());
	}

}

class Mammal
{
	String name = "furry";
	String makeNoise()
	{
		return "Generic Noise";
	}
}

class Zebra extends Mammal
{
	String name = "Stripes";
	String makeNoise()
	{
		return "bray";
	}
}

Open in new window

0
Comment
Question by:prsubject
  • 3
3 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35446506


This is expected behavior.
Method makeNoise() in Zebra ovrrides the
same method in Mammal
and JVM at runtime determines that this is instance of Zebra
and uses its overriden method

The fileds are not overriden in this way
If this is instance of Mamma it print name as in Mammal
if you cast it to Zebra iot will print name as in Zebra




0
 
LVL 47

Expert Comment

by:for_yan
ID: 35446512

This is your program with subsequent cast to Zebra:

package chl;

public class ZooKeeper {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new ZooKeeper().go();
	}

	public void go()
	{
		Mammal m = new Zebra();
		System.out.println("Name: "+m.name+" "+"Noise: "+m.makeNoise());

        	Zebra z = (Zebra)m;
		System.out.println("Name: "+z.name+" "+"Noise: "+z.makeNoise());
	}

}

class Mammal
{
	String name = "furry";
	String makeNoise()
	{
		return "Generic Noise";
	}
}

class Zebra extends Mammal
{
	String name = "Stripes";
	String makeNoise()
	{
		return "bray";
	}
}

Open in new window


output:

Name: furry Noise: bray
Name: Stripes Noise: bray

Open in new window

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 35446524

Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon.
from:
http://home.cogeco.ca/~ve3ll/jatutor5.htm

So it applies to methods, i.e. to behaviors of the objects
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question