Monday, November 07, 2005

In preparation for my comp sci midterm, this is what they give me.

Inheritance
First... a fun explanation of polymorphism:


Polymorphism is when a class implements an interface or extends a class and it is treated like the class or interface it is like, rather than itself.
If you think of the Harry Potter movies or books. Professor Snape hates Harry Potter. If you read into it, he really hates Harry's father. So Professor Snape is using polymorphism to take out his hatred on Harry's father even though the object he is treating so badly is really the son of Mr. Potter -- that is Harry, not Harry's father. Harry is being treated like someone he is not, but someone he shares many traits with (such as looks).
Polymorphism is valuable to programmers, because they often know what they wish to do with a class that hasn't been written yet. So they make an interface and demand that the class or classes that they use in the future implement that interface. That way they may use any of the classes that they or anyone else write without changing their code.


Potters


Lets try to model the relationship described above. James Potter and Harry Potter are both Potters. Professor Snape has a list of people he Hates. Both Potters are in that list along with Ron, who is part of the Weasely family. The Weasely family has Arthur, Molly, Bill, Charley, Percy, Fred, George, Ginny. Out of that family, Snape Hates only Fred, George, Ron, and Ginny.
Think of how you would implement this relationship using interfaces, abstract, and regular classes. Each bold faced word above is a class in itself.


Harry Potter and computer science? That's like trying to mix water and oil. One you can't live without, and the other just clots your arteries.

2 Comments:

Blogger Cynda said...

HAHA I like the analogy!!

3:02 PM  
Blogger magda. said...

That is so cool.

8:59 PM  

Post a Comment

<< Home