Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the example for polymorphism?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one tell me the real example for polymorphism.

Real example in the sense, we have to tell the example to some non computer science persons.

Encapsulation is nothing but wrapping of data and method into single unit. This definition for only us. But i wanna explain, what is meant by encapsulation to someone who is not in a computer science background.

How can i tell ??
Here this is explanation
Encapsulation is like, Tube Tablets (capsules). Tablets which covered by tube. It contains some medicines, which wraps some medicine.
Did you got that what am trying to say.

Think of a class as a vehicle. An object as a lorry or car or rocket.
A class is like a collection of objects. A lorry is an object, so is a car and a rocket too. But all of them are vehicles.

Likewise how can we give these kind of explanations for polymorphism ???
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*I* don't think this is an intermediate level question.

Anyway, you may say that polymorphism is like offering services. It is like a computer can offer many services like internet surfing, coding, multimedia and others. Now, these different services of the same object are basically polymorphism. Now, what service you select at runtime e.g. coding is runtime polymorphism.

So, we may say polymorphism says I have these many features choose what you may like.

pretty weird . Couldn't think of a better example.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure about your description of what a class is. A class is not a collection of objects.

A class is a blueprint, or a plan that tells you how to build objects. A class named House for example tells you how to build houses (objects). A subclass of class House is a blueprint for a more specific type of house. You might for example have a class Apartment that extends House, that tells you how to build apartments (which are, again, objects). The Apartment plan is the plan for a House plus special instructions.

For polymorphism, we often see the example of class (or interface) Animal and subclasses Cat and Dog. An animal can make sound. But what the exact sound is, depends on what kind of animal it is. Polymorphism is this: You have an animal in a cage (you can't see what it is, you only know that it's an animal) and you poke it to make a sound. You don't need to know what kind of animal it is to have it make a sound, and what kind of sound you get depends on what the actual animal (at runtime) is.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you have some GUI objects on a web page or swing app (button, list box, check box, etc). .
Perhaps you want each one to be able to do something:

widget.draw

or

widget.disable

So buttons can draw themselves, and so can checkboxes.
Listboxes can disable themselves, and so can every other widget.

Now this is a true example of where polymorphism can be useful.
I can loop through the widgets on a page and ask them to draw themselves.
 
Do you pee on your compost? Does this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic