Kuro5hin.org: technology and culture, from the trenches
create account | help/FAQ | contact | links | search | IRC | site news
[ Everything | Diaries | Technology | Science | Culture | Politics | Media | News | Internet | Op-Ed | Fiction | Meta | MLP ]
We need your support: buy an ad | premium membership

[P]
OOP Concept explained: Polymorphism (Technology)

By balsamic vinigga in Technology
Thu Mar 16, 2006 at 07:10:44 AM EST
Tags: Software (all tags)
Software

The problem with most computer science texts is the examples aren't interesting. Polymorphism isn't really hard to understand, but the examples are usually boring or difficult to understand. This is an alternative text which attempts to use a problem space that's already familiar to the college student to make the concepts of programming simple.

Pre-requisite skills  You must be comfortable with classes and objects and read basic java/c# to follow this lesson.


Polymorphism shouldn't be a new concept to anybody. You deal with it every day in the real world. There's more than one class of cat to skin, but you skin 'em the same way, even if the specific instance is completely new to you. Let's say for example you want to fuck a hole. You fuck all holes the same. You don't care if that hole happens to be a mouth, an ass, or a pussy, you're gonna fuck it the same way regardless. However, the mouth, pussy, or ass may respond differently to the fucking.

So you have a common abstract class named 'Hole' and 3 concrete classes Pussy, Ass, and Mouth which all extend from Hole:


class Pussy extends Hole {}
class Mouth extends Hole {}
class Ass extends Hole {}

So, now let's say you have a Penis.Fuck(Hole h) method. The Penis class is unconcerned about what the specific Hole instance is, it's gonna fuck it the same regardless. Specificly we thrust the Hole with a Penis until the Penis is spent. Finally, we give the hole the Penis' load.


class Penis {
  public Fuck(Hole h) {
    while(!this.isSpent) {
      h.TakeAThrust(this);
      this.arousal++;
    }
    h.TakeALoad(this.load);
  }
}

Now here's where polymorphism gets fun. The Hole will respond different to the thrusting and load depending on what specific type of Hole we're implementing.

First we must implement an abstract class which defines an abstract interface.


abstract class Hole {
  public abstract void TakeAThrust(Penis p);

  public abstract void TakeALoad(Load l);
}

Now all that's left is the varying implementations of these methods in the seperate concrete classes. For example, an Ass' implementation of TakeAThrust could look something like:


public void TakeAThrust(Penis p) {
  if(!enoughLube && p.Circumference > 6) {
    analFissureCount++;
  }
}

See, the beauty of it is... the Penis doesn't even need to know it's fucking an Ass for the Ass to behave like a proper Ass.

Now, let's see how we might implement TakeALoad differently for Mouth and Pussy:


//in Pussy
public void TakeALoad(Load l) {
  //randomly determine whether to cause a pregnancy with a 10% chance...
  if(Math.RandomNumber() % 10 == 0) {
    this.Woman.EggFactory.GetEgg().Inseminate(l);
  }
}

//in Mouth
public void TakeALoad(Load l) {
  //50-50 chance of spitting or swallowing
  if(Math.RandomNumber() % 1 == 0) {
    this.Spit(l);
  } else {
    this.Swallow(l);
  }
}

Putting it all together with client code

Now that we have our classes well planned out with polymorphism in mind, we can see the kind of luxury it is for the client programmer to work with.


//create an array of 4 women
Woman[] women = new Woman[]{new Woman(), new Woman(), new Woman(), new Woman()};

//create a hole array to reference the holes of all 4 women, plus two additional holes.
Hole[] holes = new Hole[4*3 + 2];

for(int i = 0; i < women.Length; i++) {
  holes[3 * i + 0] = women[i].Mouth;
  holes[3 * i + 1] = women[i].Pussy;
  holes[3 * i + 2] = women[i].Ass;
}

//additional holes (so the faggy programmers don't feel left out)
Man m = new Man();
holes[12] = m.Mouth;
holes[13] = m.Ass;

//now we loop through the holes and fuck them all with the same Penis

Penis p = new Man().Penis;

foreach(Hole h in holes) {
  p.Fuck(h);
}

See how easy it makes it for the client programmer?

Thank you class, any questions?

Sponsors

Voxel dot net
o Managed Hosting
o VoxCAST Content Delivery
o Raw Infrastructure

Login

Poll
This lesson helped me better understand polymorphism in OOP
o yes 27%
o no 12%
o already understood it 60%

Votes: 74
Results | Other Polls

Related Links
o Also by balsamic vinigga


Display: Sort:
OOP Concept explained: Polymorphism (Technology) | 124 comments (109 topical, 15 editorial, 3 hidden)
if fuckers vote up debs stuff, they should +1FP (3.00 / 2) (#3)
by creativedissonance on Tue Mar 14, 2006 at 06:25:23 PM EST

especially whoever is controlling the clone trooper dupe horde.  I mean shit.


ay yo i run linux and word on the street
is that this is where i need to be to get my butt stuffed like a turkey - br14n
Oh goddammit! (3.00 / 4) (#7)
by Lenticular Array on Tue Mar 14, 2006 at 06:48:52 PM EST

I voted -1 before I realized how vulgar your examples are.
ANONYMIZED
Can you write a tutorial for perl? (2.71 / 7) (#9)
by superdiva on Tue Mar 14, 2006 at 07:18:39 PM EST


_____________________________________________

Vote in Front Page poll: Best Scoop Writer for 2005
Naughty but nice £ (none / 0) (#14)
by stuaart on Tue Mar 14, 2006 at 07:43:56 PM EST


Linkwhore: [Hidden stories.] Baldrtainment: Corporate concubines and Baldrson: An Introspective


+1: Boring subject made fun. (none / 1) (#16)
by raaymoose on Tue Mar 14, 2006 at 08:07:07 PM EST

Good stuff.

dude (2.66 / 6) (#17)
by circletimessquare on Tue Mar 14, 2006 at 08:53:00 PM EST

you need to get laid

seriously badly

up here, neurons for coding

down there, hormones for fucking

the hormones need to be flushed on a regular basis, for if they are not flushed, they begin to flood the lower floors of the brain, and when these hormones flood up to the highest echelon of neurons- those having to do with OOP for chirssake, and begin to warp them with stuff like this story, then it's been a SERIOUSLY long time since you had your "get my freak on" hormones flushed

dude, go get laid

QUICK


The tigers of wrath are wiser than the horses of instruction.

This is too hard (1.00 / 3) (#22)
by United Fools on Tue Mar 14, 2006 at 10:31:16 PM EST

We cannot understand it. Can you first create a tutorial for something easier?
We are united, we are fools, and we are America!
+1 FP - fiction [nt] (none / 0) (#23)
by starched shirt collar on Tue Mar 14, 2006 at 11:56:37 PM EST



Virgin or lousy in the sack. (3.00 / 4) (#24)
by killmepleez on Wed Mar 15, 2006 at 12:38:28 AM EST

If you think all holes get fucked the same regardless, I feel sorry for your sexual partners (unless of course they're imaginary which, given the forum, is likely as not).

If you don't realize that different parts of the body respond to different types of stimulation*, then it's very possible that both people aren't getting off. If both people aren't getting off, then you're missing out on great sex, because great sex is all about mutuality.


e.g. amount/type of lubrication present, speed of initial insertion, insertion depth, thrust angle, thrust force, thrust frequency, and especially the degree to which those last three items are varied throughout the encounter, not to mention the many many combinations of peripheral stimulation which make it more intense for you both {yes, newsflash poindexter, great sex has as much to do with what's between your ears as what's between your legs -- and i've found this to be no less true with male partners than with female partners}

__
"I instantly realized that everything in my life that I thought was unfixable was totally fixable - except for having just jumped."
--from "J
-1, didn't explain how polymorph works. (none / 1) (#26)
by lukme on Wed Mar 15, 2006 at 01:18:05 AM EST




-----------------------------------
It's awfully hard to fly with eagles when you're a turkey.
Infantile and obnoxious. (3.00 / 4) (#30)
by daveybaby on Wed Mar 15, 2006 at 05:55:18 AM EST

+1FP

Bug (3.00 / 3) (#31)
by psychologist on Wed Mar 15, 2006 at 06:31:27 AM EST

if(Math.RandomNumber() % 1 == 0)

Does that not always give the same result?

Also, I do not get some part of it. Let's say after you had used the penis for a long time, you later decide to switch to a dildo. Basically, it does the same things, but it does not have the ejaculate functionality. What would you do? Make a baseclass that defines an abstract Rod? So you would have PenisRod and DildoRod, with common functionality, right?

What about when you now want to use Love Balls? I can think of a number of things that are similar between a string of love balls and a dildo, however, the similarities between the love balls and penis are not as great. You have a dilemma there - do you inherit from rod or not? If you do not, do you not condemn yourself to a big mess?

+10 MILLION FP ALL THE WAY (none / 0) (#36)
by ChefSalad on Wed Mar 15, 2006 at 12:29:47 PM EST

I don't get why people are voting this down. It's hilarious AND informative. I think maybe they're just jealous that they didn't think of it first.

I was anonymized for submitting rusty fanfic to the queue in poor taste involving gay sex, aids, and a rusty nail. Let me serve as lesson for all.
It needs an if (8==D) comparison. (3.00 / 11) (#43)
by The Vast Right Wing Conspiracy on Wed Mar 15, 2006 at 04:00:32 PM EST



___
I'm a pompous windbag, I take myself far too seriously, and I single-handedly messed up K5 by causing the fiction section to be created. --localroger

You may not have realized (none / 1) (#46)
by rpresser on Wed Mar 15, 2006 at 04:51:01 PM EST

There is a lot of actual code that uses these concepts (or should use them, anyway). Take a look at this and this and this.
------------
"In terms of both hyperbolic overreaching and eventual wrongness, the Permanent [Republican] Majority has set a new, and truly difficult to beat, standard." --rusty
+1SP, clever and has pedo value (3.00 / 3) (#65)
by nostalgiphile on Thu Mar 16, 2006 at 03:28:47 AM EST

Pedogogical values, I mean.

"Depending on your perspective you are an optimist or a pessimist[,] and a hopeless one too." --trhurler
this is incredibly sexist (none / 1) (#67)
by hardcorejon on Thu Mar 16, 2006 at 07:10:16 AM EST

I mean, seriously:

//create an array of 4 women
Woman[] women = new Woman[]{new Woman(), new Woman(), new Woman(), new Woman()};


Is there really only a default constructor for Woman? Should she at least get a name or something? This article presumes that all women are created anonymously and are indistinguishable and interchangeable.

+1FP

- jonathan.

There's actually a real world use for this (none / 0) (#70)
by ksandstr on Thu Mar 16, 2006 at 09:11:49 AM EST

See the adult interactive fiction (aka AIF) libraries for something like TADS. Well, for sufficiently small amounts of "real" anyway.

Fin.
Is this part of a series? (3.00 / 2) (#80)
by creature on Thu Mar 16, 2006 at 12:28:44 PM EST

I certainly hope so.

other types of polymorphism (none / 1) (#83)
by mrbastard on Thu Mar 16, 2006 at 01:27:35 PM EST

There are other types of polymorphism. In particular the compile time (as opposed to runtime, using a vtable or whatever) polymorphism offered by C++ templates is worth mentioning. Templates let me write dick functions that will not only work with any hole, but don't spend a lot of time fumbling to get it in the right hole during the main event, as it were.

"ohmygod I have a boyfriend" - Wen Jian

Where's LilDebbie's article? (3.00 / 2) (#84)
by bamcquern on Thu Mar 16, 2006 at 02:01:13 PM EST



I think the man just fucked himself in the ass (none / 0) (#85)
by nidarus on Thu Mar 16, 2006 at 03:24:03 PM EST

Was that the point?

Interfaces (3.00 / 5) (#89)
by Baldrson on Thu Mar 16, 2006 at 06:54:19 PM EST

Your example is hardly an adequate introduction to the concept of polymorphism since if focuses simply on inheritance raher than true information hiding. There is nothing about the Fuck method that has to apply only to Holes. You might have an interface that includes the Fuck method and have a variety of classes that implement the Fuck method independently. For example, you might have a class CFO and another class Stockholders and end up with something like:

cfo.Fuck(sh)

-------- Empty the Cities --------


This does not bode well for K5. $ (none / 1) (#95)
by skyknight on Thu Mar 16, 2006 at 10:30:34 PM EST



It's not much fun at the top. I envy the common people, their hearty meals and Bruce Springsteen and voting. --SIGNOR SPAGHETTI
Enhanced OO Design (none / 0) (#101)
by jalava on Fri Mar 17, 2006 at 06:13:18 AM EST

I would enhance that bit further. interface Fuckable { public abstract void TakeAThrust(Penis p); public abstract void TakeALoad(Load l); } interface Fucker { public abstract void Fuck(Fuckable f); } of course, now Hole would implement Fuckable. Why this? Of course, because of the Fist! public class Fist implements Fuckable, Fucker { .. code here } This allows for example lesbian fist fuck action. new Woman().getFist().fuck(new Woman().getAss()); and gay mutual masturbation: Man man1 = new Man(); Man man2 = new Man(); man1.getPenis().fuck(man2.getFist()); man2.getPenis().fuck(man1.getFist()); You can see the limitless possibilities in here!

Brilliant (none / 0) (#103)
by n8f8 on Fri Mar 17, 2006 at 08:07:19 AM EST

I bow to your uber teaching skills.

Sig: (This will get posted after your comments)
50-50 chance ... (none / 0) (#104)
by wejn on Fri Mar 17, 2006 at 09:31:04 AM EST

$ ruby -e '(0..10).each do |i| puts i % 1; end'
0
0
0
0
0
0
0
0
0
0
0

Based on the test above I'd say it's a 100-0 chance. Poor you ;)

Nice but kind of incomplete (none / 1) (#115)
by nakor on Sat Mar 18, 2006 at 04:11:54 PM EST

Where are the nostril and ear subclasses?

Mistake ! (none / 0) (#118)
by javacub on Fri Mar 31, 2006 at 08:49:11 AM EST

//in Mouth public void TakeALoad(Load l) { //50-50 chance of spitting or swallowing if(Math.RandomNumber() % 1 == 0) { this.Spit(l); } else { this.Swallow(l); } } It should be % 2 .... otherwise it will spit everytime :)) by cub

A problem space familiar to the college student? (3.00 / 3) (#120)
by CptPicard on Wed May 24, 2006 at 09:00:05 PM EST

I am a CS major, could you please elaborate what exactly I am supposed to be familiar with in these examples? Your choices of object and method names and their uses seem like greek to me.

I have thought that this article is very good (none / 1) (#123)
by aldara on Fri Apr 06, 2007 at 12:00:51 AM EST

I have thought that this article is very good
¼âÈñʪðà


I have thought that this article is very good (none / 1) (#124)
by aldara on Fri Apr 06, 2007 at 12:02:04 AM EST

I have thought that this article is very good
¼âÈñʪðà

One correction (none / 1) (#125)
by Evandream on Sun May 06, 2007 at 03:37:21 PM EST

i think it has a mistake
abstract class Hole {
  public abstract void TakeAThrust(Penis p);

  public abstract void TakeALoad(Load l);
}

The below script should contain different tags PO and LIT

public void TakeAThrust(Penis p) {
  if(!enoughLube && p.Circumference > 6) {
    analFissureCount++;
  }
 fsbo mls for example this phrase should contain tags void Take and Count ++ = ))) Circumference:

OOP Concept explained: Polymorphism (Technology) | 124 comments (109 topical, 15 editorial, 3 hidden)
Display: Sort:

kuro5hin.org

[XML]
All trademarks and copyrights on this page are owned by their respective companies. The Rest © 2000 - Present Kuro5hin.org Inc.
See our legalese page for copyright policies. Please also read our Privacy Policy.
Kuro5hin.org is powered by Free Software, including Apache, Perl, and Linux, The Scoop Engine that runs this site is freely available, under the terms of the GPL.
Need some help? Email help@kuro5hin.org.
My heart's the long stairs.

Powered by Scoop create account | help/FAQ | mission | links | search | IRC | YOU choose the stories!