CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Polymorphism

  1. #1
    Join Date
    May 2005
    Posts
    178

    Polymorphism

    hello,
    i know polymorphism gives you the ability to use the same object for several different types , but it can be done without polymorphism (with casting and inheritance)...
    so can someone explain to me what can be done with polymorphism that cant be done in any other way (like with only inheritance and casting)?
    an example would be nice
    thanks in advanced.

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Polymorphism

    Quote Originally Posted by ppl1
    hello,
    i know polymorphism gives you the ability to use the same object for several different types , but it can be done without polymorphism (with casting and inheritance)...
    Could you show any example of this case? In JAVA everything is polymorphic unless method called is final, so I am not sure how you could do any method call 'without polymorphism, but with inheritance'.

    Regards,
    Hob
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    May 2005
    Posts
    178

    Re: Polymorphism

    example-

    Code:
    class animal
    { public void func() {...}
    }
    
    class dog extends animal
    { public void func() {...}
    }
    
    class cat extends animal
    { public void func() {...}
    }
    
    cat c=new cat();
    c.func();
    ((animal)c).func();  //with casting i can call func of animal
    so in this example i can call a method of cat and aminal using casting
    what i am missing?

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Polymorphism

    Quote Originally Posted by ppl1
    so in this example i can call a method of cat and aminal using casting
    what i am missing?
    You are missing the fact, that in properly designed application, above construct shoud never be neccessary. Besides, following code:
    Code:
    public class Test {
    	public static void main(String[] args) 
    		cat c=new cat();
    		c.func();
    		((animal)c).func(); 
    	}
    }
    
    class animal
    {
    	public void func() {System.out.println("Animal");}
    }
    
    class dog extends animal
    {
    	public void func() {System.out.println("dog");}
    }
    
    class cat extends animal
    {
    	public void func() {System.out.println("cat");}
    }
    Gives output:
    Code:
    cat
    cat
    So, as you can see, you cannot call any method from Animal having reference to Cat, because even after you cast to Animal, method call is still polymorphic. Only way to call overriden method is to call it explicitly in derived class:
    Code:
    class cat extends animal
    {
    	public void func() {
               super.func();
               System.out.println("cat");}
    }
    Regards,
    Hob
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    May 2005
    Posts
    178

    Re: Polymorphism

    ok i get it now
    thanks

  6. #6
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Polymorphism

    Quote Originally Posted by ppl1
    hello,
    i know polymorphism gives you the ability to use the same object for several different types
    You've got it slightly wrong. An object is polymorphic if it has many types (this kind of polymorphism is called subtype polymorphism). Subtype polymorphism is an inherent property of the object and independent of how you use it. You can cast it on the wall and stomp on it, it's still polymorphic.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured