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
polymorphism
Started by Siten0308, Mar 18 2009 03:38 PM
13 replies to this topic
#1
Posted 18 March 2009 - 03:38 PM
|
|
|
#2
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.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#3
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.
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.
#4
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 : )
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 : )
#5
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,
class1 (base class/parent 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?
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?
#6
Posted 19 March 2009 - 10:57 AM
Hello everyone,
Please correct me if I am wrong, after further investigating, I would use:
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)
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)
#7
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?
#8
Posted 19 March 2009 - 05:41 PM
Siten0308 said:
Also can someone let me know if anyone ever uses interface for their project? why would you use interface and not class?
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
#9
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
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:
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
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
#10
Posted 20 March 2009 - 08:02 AM
Quote
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,
Quote
1. Why would you use two constructors?
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,
};
}
Quote
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?
Best regards,
Arek Bulski.
#11
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.
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.
#12
Posted 20 March 2009 - 08:24 AM
Siten0308 said:
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.


Sign In
Create Account


Back to top









