Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

polymorphism

polymorphism

  • Please log in to reply
13 replies to this topic

#1 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 18 March 2009 - 03:38 PM

Hello,

Wondering if someone can easily explain polymorphism as well as show a very simple/basic exmaple of it below, I am looking at some of the examples online and it sorta makes me wonder, why do I need it, inheritance pretty much gives you the chance to work with both classes even though you initiated one (initiated the child class that is...). so again breifly english explanation and example might help me 'click' in the brain : )

Thank you all
  • 0

#2 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 18 March 2009 - 03:47 PM

Hmm. That's a key part of oop. I'm still trying to grasp that topic. It would be nice to see it from another point of view.
  • 0

#3 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 18 March 2009 - 03:58 PM

Quoting wikipedia: "polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface"

Type polymorphism - Wikipedia, the free encyclopedia
Polymorphism in object-oriented programming - Wikipedia, the free encyclopedia

Which means that you make one line of code, despite you handle more-than-one possible types there. I am working on some example, hang on. ;)

For example, using one "Interface" for many "classes" means using a polymorphism feature. Look at C# example at 2nd link.

What do you think about it?

Regards, :cool:
Arek Bulski.
  • 0

#4 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 19 March 2009 - 07:32 AM

Hello ArekBulski,

The wekepedia sorta helped but tell you the truth, it threw me off when it used the list class. I am wondering if I can just see a polymorphism example when some explanation how to implement it or how its being executed and why its good practice to use that may help me click : )
  • 0

#5 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 19 March 2009 - 08:17 AM

Hello,

I think I got it, look at the code below and let me know what am I missing to work with the parent class/base class,
    class Program
    {
        //delegate double delegating(int num1, int num2);
        
        static void Main()
        {
            Class2 classic = new Class2();
            try
            {
                Console.WriteLine(classic.spellthat());
                Console.WriteLine(classic.spellthat());
                
            }
            catch (Exception)
            {
                Console.WriteLine("Do it RIGHT MORON!!");
                Console.ReadKey();
            }
        }

        #region
        /*
        public static int addingmiles(int num1, int num2)
        {
            return num1 + num2;
        }
        */
        #endregion

    }
}

class1 (base class/parent class)
    public class Class1
    {
        string spell;
        public string spellthat()
        {
            return spell = "...this is the parent";

        }
    }
}
child class
    class Class2: Class1
    {
        string child;
        public string spellthat()
        {
            return child = "this is from the child class";
        }
    }
}

I think its now starting to click why poly blah blah is worth knowing, because if you have two of the same methods in different classes and one is a parent and other is a child, the child method will overlap and ignore the parent method with the same name method only, but what if you wanted to use the method from the parent, what do I need to do to get the method from the parent?

Thank you

PS... is that polymorphism what I just explained above?
  • 0

#6 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 19 March 2009 - 10:57 AM

Hello everyone,

Please correct me if I am wrong, after further investigating, I would use:
 
        static void Main()
        {
            Class2 classic = new Class2();
            Class1 classical = classic;
            try
            {
                Console.WriteLine(classical.spellthat());
                Console.WriteLine(classic.spellthat());

is this correct on polymorphism above?

but what is the difference if I just initialize class1 as new instead of using classic to do polymorphism? (hopefully that made sense)
  • 0

#7 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 19 March 2009 - 03:18 PM

Also can someone let me know if anyone ever uses interface for their project? why would you use interface and not class?
  • 0

#8 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 19 March 2009 - 05:41 PM

Also can someone let me know if anyone ever uses interface for their project? why would you use interface and not class?

If I can put my own experience, don't use interfaces untill they are giving some "measurable benefits". For example, in my game I made an interface, so I could swich between Direct3D engine and DirectDraw engine. But in the end, it was more mess than it helped. :blink:

On the other hand, as many so-called exprets say, interfaces are really cool. They make you realize where is a line between "interface and implementation". My advise is: make some nice projects, that you enjoy doing them. At some point you will realize that you started to use interfaces, or you wanna start using them. :)

F1Simulation: Racing game made in .NET
  • 0

#9 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 19 March 2009 - 09:58 PM

Hello ArekBulski,

Thank you again for your insight, I guess I will continue using classes since its easy for me to understand (abstract class, public, static etc etc). Now I just have two questions,

1. Why would you use two constructors?
example
class class1 project1()
{
public project1()
{
//something in here like a variable/field blah blah blah
}
public project1(int i)
{
i = some number...suprise me
}
}
again looking at that example above, why would you use two constructors, I have always used one constructor foreach class I made.

second question:
Is this an example of polymorphasism:
its posted above but wanted to clarify if I did it right, which is here below again, if anyone can answer it that would be great : )

Hello everyone,

Please correct me if I am wrong, after further investigating, I would use:

Code:

static void Main()
        {
            Class2 classic = new Class2();
            Class1 classical = classic;
            try
            {
                Console.WriteLine(classical.spellthat());
                Console.WriteLine(classic.spellthat());

is this correct on polymorphism above?

but what is the difference if I just initialize class1 as new instead of using classic to do polymorphism? (hopefully that made sense)
FYI: Class 2 is the child and class 1 is the parent/base class

After I get these questions done then its off to collections, conversions, and... anything else I should learn or know?

thank you all
  • 0

#10 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 20 March 2009 - 08:02 AM

Thank you again for your insight, I guess I will continue using classes since its easy for me to understand (abstract class, public, static etc etc). Now I just have two questions,

Do what pleases you! Code monkeys are paid for writing *working* and *readable* *maitainable* code,... not for using features, no? :)

1. Why would you use two constructors?

I would create as much constructors, as I use in my code. Not more, and not less. Any constructor that is not being used,... is a waste of space. :001_huh: But if doing that is less handy, then do what pleases you! ;)

I often use those 2 features, they are called "object initializers" and "automatic properties". I found some interesting list of features at C Sharp (programming language) - Wikipedia. :cool: Again, use whatever doesn't make you tired. ;)

class WhateverClass
{
public string FirstLastName { get; set; }
public int Age { get; set; }
}

static void Main(string[] args)
{
WhateverClass me = new WhateverClass()
{
FirstLastName = "Arek",
Age = 21,
};
}


is this correct on polymorphism above? but what is the difference if I just initialize class1 as new instead of using classic to do polymorphism?

This is really funny question. I have no idea. But since your code is working, readable and so on, does it really matter? :mellow: I hope you will keep thrilling this topic and show us some research of yours! :rolleyes:

Best regards,
Arek Bulski.
  • 0

#11 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 20 March 2009 - 08:10 AM

Hello ArekBulski

Thank you for your insight, I just want to know all there is to when it comes to C# but I guess that comes in time, practice and experience. I just want to make sure I do coding correct to industry standard and make it easy, but your right, if its easy to understand than its easy coding, but I just dont want to make coding as complicateed or to much to write when I dont need to such as goto, which is bad coding/practice so I choose to ignore that and stick with looping. And you better believe there will be more questions to come. Thanks again everyone, hope this info helped me as it has helped you.
  • 0

#12 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 20 March 2009 - 08:24 AM

I just want to know all there is to when it comes to C# but I guess that comes in time, practice and experience. I just want to make sure I do coding correct to industry standard and make it easy,


Yare yare, let me thank you for your discussion then! I really envy you, for that interest in exploring good coding. In the past I was full of questions myself, but as you can see I got lazy... :o

Best regards,
Arek Bulski.
  • 0





Also tagged with one or more of these keywords: polymorphism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download