This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Developer Career Masterplan: Build your path to senior level and beyond with practical insights from industry experts and have Heather VanCura and Bruno Souza on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Question about polymorphism.

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is an example of polymorphism ?

A. Inner class
B. Anonymous class
c. method overloding
D. method overriding.

I think ans should be B,D but ans is C. so please tell me what is correct ans.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think "method overloding" is all right, as:

# In computer science, polymorphism is the idea of allowing the same code to be used with different classes of data (which classes in typed languages correspond to types), resulting in more general and abstract implementations.
en.wikipedia.org/wiki/Polymorphism_(computer_science)

# In object-oriented programming theory, polymorphism is the ability of objects belonging to different types to respond to methods of the same name, each one according to the right type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding).
en.wikipedia.org/wiki/Polymorphism_(object-oriented_programming)

The only confusion may be on overloading and overriding, i would like to hear more clarifications.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
overlading?? noop because :

that's an overloading...it's not an example of polymorphism
Only anonymous class is a real exemple of polymorphism...because you reach the anonymous object throug a superclass reference.
Overiding is about inheritance...
i hope it help a bit
arno
[ April 17, 2006: Message edited by: Arno Reper ]
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a tip: you shouldn't use wikipedia to study for SCJP, its not complete enough and it could create misunderstanding.
The best way for learning scjp is the ...well known and famous K&B books lol
arno
ps : i like wikipedia but not for studying a certification like scjp
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would go for B C and D. B because anon classes will subclass an existing class or implement an interface and C , D because inheritance is a form of polymorphism and both overloading and overriding can be considered as forms of polymorphism.
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you sure? b,c,d?
i 'd go for the B only...
arno
ps : read k&b books pg 154
" Polymorphism applies to overriding not to overloading"...c is not ok
d maybe but...
[ April 17, 2006: Message edited by: Arno Reper ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
visit this link. It also has gr8 discussion of this que. I think both C & D. http://www.javaranch.com
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can google overloading and polymorphism.

Here is a wiki link.

http://en.wikipedia.org/wiki/Overloading
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think that must be "D. method overriding".

Polymorphism is provided by method overriding, when a method is invoked using a super class reference variable (or interface). So any different implementations, provided by sub classes, can be invoked like a unique method. It is closely related with inheritance.
Unlike, overloading is not dependent of inheritance and is not related with Polymorphism. It allows us to use the same method name with different arguments.

Sorry for my poor English.

Leonardo Luiz
 
Amisha Shah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think answers are B and D.

overrinding and anonymous claas. because polymorphysm is in play when anonymous classe are involved.
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at least,i 'll say ( write ) its certainly not a and c ... b for sure, d its about inheritance...who polymorphism is about so maybe d also.
arno
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

i have read this in one of the textbooks..

there are two types of polymorphism:

1)static polymorphism
2)dynamic or runtime polymorphism

overloading comes under staic polymorphism while overriding comes under runtime polymorphism...

polymorphism means having more than one form so overloading and overriding are part of polymorphism for sure...

please give your comments.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from jsl4


polymorphism, meaning that an object of a given class can have multiple forms, either as its own class or as any class it extends. The new class is a subclass or extended class of the class it extends; the class that is extended is its superclass.


hence i think polymorphism effect on object other than methods or others
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is 2 type of polymorphism
1: early binding
2: late binding
thease are achived through
method overloading , method overriding
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is c & d.
method overloading and method overriding are the types of polymorphism.
Method Overloading is static polymorphism and Method Overriding is Dynamic Polymorphism.
Polymorphism is nothing but one thing existing in different forms.
That's what we do with method overloading i.e same function name with different types and/or number of arguments and in case of method overriding it's the same function name(present in both base as well as derived class) but execution depends on who(object of base class or derived class) called the function(object reference used).
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by swapnil paranjape:

i have read this in one of the textbooks..

there are two types of polymorphism:

1)static polymorphism
2)dynamic or runtime polymorphism



hello swapnil ,

I agree with you polymorphism are of two types static(compile time) and dynamic (run time), function overloading is an example of static polymorphism , where as overriding functions of base class in a derived class is an example of runtime polymorphism.
[ April 18, 2006: Message edited by: faisal usmani ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone finally tell what is the correct answer ? It's bit confusing for me now.

Cheers,
-Biswa
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Both method overloading and overriding are examples of polymorphism
 
Ranch Hand
Posts: 91
Firefox Browser C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B) - because anonymous class overriden methods are invoked using superclass references
C) - compile time polymorphism
D) - runtime polymorphism

What's the issue??

In the end, maybe they won't accept B) for an answer. C) and D) are definitely answers though.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well in the orthodox way B, C and D are correct. Explanation similar to Pritish. But the main point here is what do you understand by "Polymorphism". What exactly is poly-morphed? The behavior of an object is poly-morphed which can happen if the compiler resolves it or its resolved at run time. I wouldn't say compile time resolution is a great example of polymorphism as the behaviour is bound and cannot change after that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic