• 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

never satisfied with polymorphism

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
many times I faced the question " what is polymorphism / how do you feel polymorphism ?"
I answered but many times I felt that the person felt somthing missing so I got some lower rating .

Can you tell me how should I answer ?
generally what I feel like I will make the answer like this :

1. i will start with the def of polymorphism
2. through a small program I will explan how polymorphism is taking place .


ok ? so that is my strategy . so I start like >>>>>>>



The word polymorphism is Greek and literally means �many forms.� In Java, polymorphism means using a superclass variable to refer to a subclass object. This ability has a wide variety of uses.

imagine you are working with a family of classes, and a certain program requires use of one of the subclasses, but it will not know which subclass to use until runtime. In this case, you can use a superclass variable throughout the program, and point it to the corresponding subclass when the time is right. Polymorphism is also useful when using certain methods throughout different families of classes. In general, polymorphism allows inheritance and interfaces to be used more abstractly.

is that OK ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, nothing here is incorrect, but you don't explicitly mention polymorphic behavior -- the idea that within that family of classes, they all implement some method x() in different ways. The caller can invoke o.x() and get different behaviors depending on the class of "o". Then you might give a concrete example of these "families" and how they implement polymorphic methods in different specialized ways -- the java.io.Writer class and its children, for example.
 
kenji kunoichi
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so that is . And I think I have to demonstrate him/her with a small programming example . can somebody here post a small but very easy & appropriate program to demonstrate polymorphism ?

I have some medium program but I am looking for a small programm.
 
Marshal
Posts: 79468
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[Campbell@queeg applications]$ javac PolyTest.java PolyDemo.java PolyPolyTest.java PolyPolyPolyTest.java
[Campbell@queeg applications]$ java PolyDemo Campbell Ritchie
In class PolyPolyTest for PolyTest: word = Campbell
In class PolyPolyPolyTest for PolyTest: word = Ritchie
PolyPolyTest: word = Campbell
YlpoYlpYlopTest: word = Ritchie

PolyTest prints out its class and the word, and it has another printing method which prints a nonsense word.
PolyPolyTest and PolyPolyPolyTest each have an overridden version of that method.

You see when the print method, which is not overridden is called, it prints the class name and the word passed as a field. It is the same method called on any PolyTest object unchanged.
The printOverride method prints PolyPolyTest in one class and YlopTest in the other. It is called in the version on the subclass, so you get PolyPoly printed, or YlopYlop, not the nonsense word.

Try adding a newPrint method to the PolyPolyTest class:You can only call that by declaring the object to be PolyPolyTest type, or casting to PolyPolyTest.

And of course, you know how dangerous class casting can be!
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the following simple example to understand concept of polymorphism
--------------------------------------------------------------------------
Pay interface has salary() method that is to be defined in the classes which implements the Pay interface

public interface Pay {
public void salary();
}

Now,Teammember has its own implementation of salary() method
public class Teammember implements Pay {
public void salary() {
System.out.println("Teammember's pay is $60K");
}
}

And, Teamlead has its own implementation of salary() method

public class Teamlead implements Pay {
public void salary() {
System.out.println("Teamleader's pay is $100K");
}
}
Now in the main class, we have a single method go(Pay p) which takes interface reference as its arguement. By doing this, we can have go() behave in differnt ways (or can take different forms)===Polymorphism
depending on the instance which is passed. The only (and important)requirement in this example is passed instance should be from a class that implements the required interface( in this case, Pay)
public class Test {
public static void main(String[] args) {
new Test().go(new Teammember());
new Test().go(new Teamlead());
}
void go(Pay p){
p.salary();
}

}
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Don't Wake The Zombies. The last post in this thread was 6 months old.
 
reply
    Bookmark Topic Watch Topic
  • New Topic