Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

does anyone here know java?

OpenStudy (harsha19111999):

Yes

OpenStudy (anonymous):

great. i need help with polymorphism

OpenStudy (woodrow73):

I know some java.. but haven't reached the chapter on polymorphism yet.

OpenStudy (e.mccormick):

In general, polymorphism is when something can work with more than one type. Like the + operator. 1 + 1 works on ints. 1.1+1.1 works on floats In both I used + but it worked without me making all sorts of noise about what types were being sent, etc.

OpenStudy (e.mccormick):

Now, that is just a very general example. What specific issue are you having?

OpenStudy (woodrow73):

I suppose using the + operator like: ``` System.out.println("Woodro" + "w"); ``` is also polymorphism :p

OpenStudy (e.mccormick):

Yes, because it is a different thing with the same operator. In truth, they call this specific type "Operator overloadong" but it is a decent way to introduce the concepts related to polymorphism.

OpenStudy (anonymous):

Polymorphism refers to a certain object or entity being able to have multiple forms. Polymorphism in Java can be achieved in 2 different ways: ``` * Overriding * Overloading ``` *Override* is used when a subclass of a class uses a different implementation for a method in the class it extends. The signature of the method remains the same in the subclass as it is in the class it extends. Basically it changes a behavior from that class it extends. For example: ``` public class Dog { public void bark() { System.out.println("The dog barks"); } } public class GoldenRetriever extends Dog { @Override public void bark() { System.out.println("The Golden Retriever barks"); } } ``` If you do this: ``` Dog dog = new GoldenRetriever(); dog.bark(); ``` You will have as an output `The Golden Retriever barks` and not `The dog barks`, because dog is an instance of GoldenRetriever. If you wouldn't have overriden the method `bark()` then for the same thing you would get `The dog barks`. *Overloading* happens when in the same class you have multiple methods with the same name but with a different number of arguments. If you do this, you basically end up having more ways for doing a behavior, depending on the parameters that define it. For example: ``` public class Dog { public void bark() { System.out.println("The dog barks"); } public void bark(String dogName) { System.out.println(dogName + " barks"); } public void bark(String dogName, int numberOfBarks) { System.out.println(dogName + " barks " + numberOfBarks + " times"); } } ``` In this case, if you have `Dog dog = new Dog();` and you use `dog.bark();` then you'll get `The dog barks`. If you use, instead `dog.bark("Spot");` you'll get `Spot barks`. And, lastly, if you use `dog.bark("Spot", 5);` you get `Spot barks 5 times`. Hope this clears up some questions :) Good luck with learning!

OpenStudy (e.mccormick):

And what ellora03 did is a longer explanation that showing operator overloading leads to. Sadly, we have not seen hajrahar reply in a while. They are missing out!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!