Welcome to Dream.In.Code
Become a Java Expert!

Join 150,406 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 933 people online right now. Registration is fast and FREE... Join Now!




Polymorphism

 
Reply to this topicStart new topic

Polymorphism

soccerizzy26
14 Mar, 2008 - 10:19 AM
Post #1

D.I.C Head
**

Joined: 23 Jan, 2008
Posts: 65

I want to start off by saying I do have information about each of these questions in a text book I was given but like most text books the examples they give are a lot easier to see how they are solved then actually solving the problems they give. But I gave each one a shot.
In question 1 I was supposed to figure out whether or not each action was legal.

1.
Suppose the class Sandwich implements the Edible interface, and you are given the variable definitions
Sandwich sub = new Sandwich();
Rectangle cerealBox = new Rectangle(5, 10, 20, 30);
Edible e = null;
Which of the following assignment statements are legal?
a. e = sub;
b. sub = e;
c. sub = (Sandwich) e;
d. sub = (Sandwich) cerealBox;
e. e = cerealBox;
f. e = (Edible) cerealBox;
g. e = (Rectangle) cerealBox;
h. e = (Rectangle) null

My answers are below with reasoning.

a. e = sub; is not legal because not everything that is edible is going to be a sub
b. sub = e; is legal because every sub is edible
c. sub = (Sandwich) e; is legal because each sub is going to be a sandwich which is edible
d. sub = (Sandwich) cerealBox; is not legal because a sub does not make a sandwich that is in class cerealBox
(From here on is where I started to get confused)
e. e = cerealBox; is not legal because cerealBox is not edible
f. e = (Edible) cerealBox; is legal because in this case cerealBox is edible
g. e = (Rectangle) cerealBox; is not legal becuase a rectangular cerealBox is not edible
h. e = (Rectangle) null; is legal because edible is whatever class null is going to be legal

Note: For my assignment I do not need the reasoning why one is legal or not but wanted to show that I at least attempted to use reasoning on why one was legal or not. Once again if you think my reasoning makes no sense it most likely won't the examples I was given didn't really give me any help so I'm trying to figure out exactly how this would work.

2.
Suppose r contains a reference to a new Rectangle(5, 10, 20, 30). Which of the following assignments is legal?
a. Rectangle a = r; legal because this is just stating that their is a rectangle called Rectangle a and it has the same coordinates as the new Rectangle
b. Shape b = r; legal because Rectangle is under the Shape hierarchy
c. String c = r; legal because the String would have the same values as Rectangle r
d. ActionListener d = r; I could not find anything about ActionListener but I'm guessing not legal?
e. Measurable e = r; not legal I'm pretty sure that measureable refers to text?
f. Serializable f = r; I was looking the the java.sun website and I think from what I saw that this would be legal
g. Object g = r; legal because Rectangle is under the hierarchy of class Object

3.
Classes such as Rectangle2D.Double, Ellipse2D.Double and Line2D.Double implement the Shape interface. The Shape interface has a method
Rectangle getBounds()
that returns a rectangle completely enclosing the shape. Consider the method call:
Shape s = . . .;
Rectangle r = s.getBounds();
Explain why this is an example of polymorphism.

I think this is an example of polymorphism because it allows the values of shape s, rectangle r. , and s.getBounds to be handled using a
single interface.



User is offlineProfile CardPM
+Quote Post

bhandari
RE: Polymorphism
15 Mar, 2008 - 10:21 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
I think this is an example of polymorphism because it allows the values of shape s, rectangle r. , and s.getBounds to be handled using a
single interface.


egjactly!!!

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month