2

I have studied from our mentors and books that polymorphism means having many shapes and in java it is achieved by two ways.

  • overloading
  • overriding.

in oracle docs polymorphism in defined very well. but i don't understand why they have not defined overloading under polymorphism they have discussed only overriding? I guess by the means many shapes overloading should also come under polymorphism.

I have already gone through all the questions but i m not clear from these answers because everyone have stated their views without any reference, everyone have different views some are saying yes others are saying no without justification.

Java The Complete reference 8th ed

I think the highlighted portion (referenced from Java The Complete reference 8th ed) is written example of overloading. Then how everyone says overloading has nothing todo with ovrloading but overriding?

that how polymorphism is not achieved by overloading?
Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29
  • 1
    Check http://stackoverflow.com/questions/12893907/is-polymorphism-overloading-and-overriding-are-same-concepts – sam Oct 17 '15 at 19:13
  • Overloading has nothing to do with polymorphism. – plalx Oct 17 '15 at 19:45
  • comon @all who have marked it duplicate, I am not satisfied with the answers that's why i asked it here. – Sindhoo Oad Oct 17 '15 at 19:59
  • 1
    If you are not satisfied with the answers or the suggested duplicate then you need to explain what else you want to known. The people who might be able to answer are not mind readers and are unlikely to guess what else you want. – AdrianHHH Oct 17 '15 at 20:23
  • @AdrianHHH my question is clear enough if you have read it completely not only the title, – Sindhoo Oad Oct 17 '15 at 20:31
  • Your question may be clear but it has been marked as a duplicate by people who understand the issues. I just commented to say that if you wish those experts to look again at your question then you need to explain which aspects of the question have not been covered by the suggested duplicate. My comment was intended to help you to get the information you want. The edit you made to say your question has not been answered gives no guidance to the experts on why you say your question has not been answered. My knowledge of the topic is not enough to add any extra answers. – AdrianHHH Oct 18 '15 at 09:23

2 Answers2

0

"Overloading" is the term for having multiple variants of a method in the same class. For example:

public class Foo
{
    public String doStuff()
    { return this.doStuff( "stuff" ) ; }

    public String doStuff( String sStuff )
    { return this.doStuff( sStuff, "things" ) ; }

    public String doStuff( String sStuff, String sWith )
    { return "Do " + sStuff + " with " + sWith + "." ; }
}

The Foo class provides three variants of the doStuff() method: one with no arguments, one with a single argument, and one with two arguments. It's common for overloading to look like this, where most variants of the method actually just call the next-higher-complexity method with some default value.

This allows you to have predictable behavior with any of zero, one, or two arguments:

Foo foo = new Foo() ;
System.out.println( foo.doStuff() ) ;                    // "Do stuff with things."
System.out.println( foo.doStuff( "stuff" ) ) ;           // "Do stuff with things."
System.out.println( foo.doStuff( "more stuff" ) ) ;      // "Do more stuff with things."
System.out.println( foo.doStuff( "stuff", "things" ) ) ; // "Do stuff with things."
System.out.println( foo.doStuff( "things", "stuff" ) ) ; // "Do things with stuff."

By contrast, "overriding" is a principle of polymorphism in which a descendant class changes the behavior of a method from the superclass.

public class Bar extends Foo
{
    @Override
    public String doStuff( String sStuff )
    { return this.doStuff( sStuff, "thingamabobs" ) ; }
}
zerobandwidth
  • 1,213
  • 11
  • 18
  • @OAD Also, for the sake of comparison, PHP, unlike Java, does not allow overloading, but it does allow default parameter values, which can give you similar behavior. – zerobandwidth Oct 17 '15 at 19:23
  • You shouldn't write your code like `{ /* stuff */ }` in one line. This is harder to read and not worth the few lines you save with that. – Tom Oct 17 '15 at 19:23
  • @zerobandwidth see update of question – Sindhoo Oad Oct 17 '15 at 20:00
  • @OAD That still describes overriding and class hierarchy in general, not overloading. The plain word "polymorphism" applies to anything that can take different forms, but in OOP (esp. Java) it refers to the relationship between descendant classes. In the above example `Foo` is a single class that provides different ways to access the basic function `doStuff()`, whereas `Bar` "is a `Foo`" but is also something else. It's _that_ variability of form that's described by the technical "polymorphism". – zerobandwidth Oct 18 '15 at 04:49
0

Polymorphism is a very general term, it can be achieved in many ways in Java and can be comprised of few or many components.

The link you provided lists polymorphism as an aspect of inheritance in Java. While overloading falls under the polymorphism notion, overriding is much more relevant in achieving polymorphism through inheritance in Java.

Zakk L.
  • 208
  • 2
  • 5