|
-
November 2nd, 2006, 05:32 AM
#1
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.
-
November 2nd, 2006, 05:49 AM
#2
Re: Polymorphism
 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 ?
-
November 2nd, 2006, 06:03 AM
#3
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?
-
November 2nd, 2006, 06:19 AM
#4
Re: Polymorphism
 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: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 ?
-
November 2nd, 2006, 06:33 AM
#5
Re: Polymorphism
ok i get it now
thanks
-
November 5th, 2006, 06:44 AM
#6
Re: Polymorphism
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|