locked
Polymorphism. RRS feed

  • Question

  • User2133584750 posted

    Hi. I've read and read on polymorphism, but cant seem to grasp it (i think). Apparently there are different types? Can someone explain it?

    e.g. I get overloading a method:

    public sub something()
    end sub

    public sub something(byval thing as string)
    end sub

    public sub something(byval thing as string, byval otherthing as int32)
    end sub

    OR

    public overloadable sub something()
    end sub

    ... am I getting this? Can someone please explain and give an example?!? Thanks!

    Wednesday, September 26, 2007 8:36 AM

Answers

  • User2133584750 posted

    So essentially, if I use inheritance and the same method and or string names with different values or executions (essentially using the same interface) then that is also polymorphism.

    So, there is inherited polymorphism (above) and overloaded polymorphism (same name without inheritance and or use of overloadable and overloaded keywords)?

    Thanks!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2007 10:38 AM
  • User789414417 posted

    Yes, that is correct.

    Function overloading is not such a big deal really. If you use it then really the functions should all be doing the same thing but maybe with different data (PrintValue(String), PrintValue(int), PrintValue(int[]) etc).

     Inherited polymorphism is much more powerful and useful because it removes the need to know the type of object before calling a function or property on it. This type of inheritance can often make use of interfaces rather than actual classes because an object can implement many interfaces (IDisposable, ITemplate, IEnumerable etc). Only inherit a class if you need common functionality shared by all child classes.

    They say that class inheritance is like an "is a" relationship (A Square is a Shape)
     Whereas interface inheritance is more like "behaves like" (A Square behaves like IDisposable)

    Luke 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2007 11:06 AM

All replies

  • User789414417 posted

     I'm a C# coder so will not be able to give any specific examples in VB.

    Polymorphism means "many shapes" and it can indeed apply to function overloading (as you have described above) as well as accessing objects via a common base class. You need to be careful when using the word to be clear what you mean.

    If you have a class:

    public abstract class Shape
    {
    public abstract String Name { get; }
    }
    You can inherit this class, maybe like:
    public class Square : Shape
    {
    public String Name { get{ return"Square"; }
    }
    And maybe like this:
    public class Circle : Shape
    {
    public String Name { get{ return"Circle"; }
    }
    You can then access a collection of Shape objects without knowing what type they are:
    foreach ( Shape s in MyShapes )
    {
    System.Out.WriteLine( s.Name );
    }
    Therefore you are polymorphically accessing a set of classes.
    Wednesday, September 26, 2007 9:57 AM
  • User2133584750 posted

    So essentially, if I use inheritance and the same method and or string names with different values or executions (essentially using the same interface) then that is also polymorphism.

    So, there is inherited polymorphism (above) and overloaded polymorphism (same name without inheritance and or use of overloadable and overloaded keywords)?

    Thanks!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2007 10:38 AM
  • User789414417 posted

    Yes, that is correct.

    Function overloading is not such a big deal really. If you use it then really the functions should all be doing the same thing but maybe with different data (PrintValue(String), PrintValue(int), PrintValue(int[]) etc).

     Inherited polymorphism is much more powerful and useful because it removes the need to know the type of object before calling a function or property on it. This type of inheritance can often make use of interfaces rather than actual classes because an object can implement many interfaces (IDisposable, ITemplate, IEnumerable etc). Only inherit a class if you need common functionality shared by all child classes.

    They say that class inheritance is like an "is a" relationship (A Square is a Shape)
     Whereas interface inheritance is more like "behaves like" (A Square behaves like IDisposable)

    Luke 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2007 11:06 AM
  • User-952121411 posted

    These MSDN links also have some nice documentation on Polymorphism is regards to VB.NET and its usage; there are some good links explaining the difference between Inheritance-Based Polymorphism and Interface-Based Polymorphism:

    Visual Basic Language Concepts: Polymorphism 

    http://msdn.microsoft.com/en-us/library/z165t2xk(v=VS.71).aspx

    Visual Basic Language Specification: Polymorphism

    http://msdn.microsoft.com/en-us/library/aa711874(VS.71).aspx

     

    Monday, June 7, 2010 8:55 AM