#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    What is Overloading and why we use overloading in programing?


    What is Overloading and why we use overloading in programming?

    Please clear my concept with some example.

    thanxx
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    101
    Rep Power
    27
    Overloading usually refers to defining a method or function multiple times, each time taking different types of parameters, in a strongly typed language syntax. So in Java, for instance, you might have a constructor method to build an object from either a String, or from another object.
    Code:
    public class Exception {
        public Exception(String s) { // construct this Exception from a string }
        public Exception(Throwable t) { // construct this Exception from another thing that's like an Exception }
        public Exception(String s, Throwable t) { // or we can construct from multiple parameters }
    }
    There, the constructor for Exception is overloaded to take either a String, or a Throwable, or both.

    In javascript this concept is a little different since it's a weakly-typed language. But we still use the concept, for example when we pass dynamic numbers of parameters to a function. Let's say you have a function that can add either two numbers, or three numbers, together and return the sum.
    Code:
    function sum(a, b, c) {
        if (arguments.length < 3) c = 0;
        return a+b+c;
    }
    sum(2, 3);  // 5
    sum(2, 3, 4); // 9
    This is a very contrived example but the point is, we can check if the caller passed in two arguments, or three, and act accordingly. So this function is overloaded, in a sense, to take either two or three parameters. Since javascript is weakly-typed and interpreted, you can take this to its logical extreme and define an overloaded sum() function that takes any number of parameters.
    Code:
    function sum() {
        var s=0, i=null;
        for (i in arguments) s += arguments[i];
        return s;
    }
    sum(2, 3); // 5
    sum(1, 2, 3, 4, 5); // 15
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    A single function taking a variable number of arguments has absolutely nothing to do with overloading.

    JavaScript has exactly one form of overloading: operator overloading. For example, you can use the + operator either for numbers or for strings. Adding two numbers yields the mathematical sum. Adding two strings means concatenating them:

    Code:
    var number_sum = 1 + 2;
    alert(number_sum);
    var string_sum = "Hello" + " world";
    alert(string_sum);
    So the same operator behaves differently depending on the type of the operands. This is what's called overloading -- or operator overloading, to be specific.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    22
    Rep Power
    0
    Originally Posted by iftijafar1
    What is Overloading and why we use overloading in programming?

    Please clear my concept with some example.

    thanxx

    mod_speling's answer is correct. But let me try to explain little bit more.

    Here is a CS 101 question

    What is difference between OVERLOADING and OVERRIDING?

    OVERRIDING does not change the number of input parameter (aka signature) but only change the type of parameter in parent class

    For example (in JAVA)
    Code:
    public class testone {
        public static double output(double r) { return r; }
    }
    
    public class testtwo extends testone {
        public static string output(string r) { return r; }
    }
    
    // output in testtwo has overload the testone output function
    Now example of OVERLOADING (in JAVA)
    Code:
    public class test {
        public static double output(double r) { return r; }
    
        public static string output(string r, int i) { return r; }
    }

    Why do we use overloading in programming?
    in strong-type language like Java, C/C++, C#, sometime you don't know what the input type is going to be or even when you know the set of possible input parameters, you don't know to make multiple name functions (ie outputForString, outputForInteger, and etc). By having single name (output), other class that inherit your class, just need to call one function(method) regardless of input type, as long as you overload methods correctly in your class to handle.

    How to do overloading in Javascript?
    Javascript is loose-type language which means no type check for input parameter (even, number of inputs doesn't matter)
    Here is an example of what I mean.
    Code:
    function test(input) {
         alert(input);
    }
    
    test("test");
    test("test", "test2", "test3");    //this is a valid call
    So overloading in javascript is pointless. However you can simulate the overloading.
    mod_speling 's example above is how you can simulate the overloading function. but FYI, to be technically correct, having single function to handle multiple inputs is NOT overloading. By the definition of the overloading, you need to have multiple (separate) functions(methods) in SAME class to handle different inputs(signature).
    That's why in Javascript, you can simulate overloading but NOT accurately implement it.

    I hope this clear your question.

    --- Side Note ----
    In Javascript programming, sometime you might hear "Overwriting", this is different then "Overriding". Do not be confused. "Override" and "Overload" are computer terminologies but "Overwriting" in javascript means literally you over write other function. As you prob know, Javacript is top down design, and if you have same name functions in same scope, the later function OVER WRITE the previous function. Once again, this is just literal expression.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    101
    Rep Power
    27
    a variable number of arguments has absolutely nothing to do with overloading
    Well, my answer didn't exactly come straight from Knuth, no. But in the context of this thread, to relate the topic to something OP will actually use, does it really matter?
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    It's kinda funny how you guys/gals keep making long explanations that overloading isn't possible in JavaScript when I already told you that

    JavaScript has operator overloading

    Just try my example from above:

    Code:
    var number_sum = 1 + 2;
    alert(number_sum);
    var string_sum = "Hello" + " world";
    alert(string_sum);
    The same operator is used either for addition or concatenation, depending on the types of the operands.

    But since the OP hasn't reacted in over a week, maybe we should wait for a reply before we post 10 further explanations.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    22
    Rep Power
    0
    Originally Posted by Jacques1
    It's kinda funny how you guys/gals keep making long explanations that overloading isn't possible in JavaScript when I already told you that

    JavaScript has operator overloading

    Just try my example from above:

    Code:
    var number_sum = 1 + 2;
    alert(number_sum);
    var string_sum = "Hello" + " world";
    alert(string_sum);
    The same operator is used either for addition or concatenation, depending on the types of the operands.
    Your example is the definition of the loose-type language not partially about overloading. And your wiki link is about overloading definition but it does not say anything about Javascript overloading. Overloading in programing is real and very important concept of object-oriented language. Since you don't like long explanation, I found this very short and key-pointed explanation about overloading vs overriding (source from uscu.edu)
    http://users.soe.ucsc.edu/~charlie/b...ap7/sld012.htm

    if you choose to read further, here is my explanation.
    Code:
    //** This is an example of JS is being a loose-type language.
    
    var num = 1;
    var str = "one";
    
    //in strong-type languages, there are invalid, but in JS, these are OK
    var test1 = num + str;  
    var test2 = str + num;
    
    alert(test1); //output "2s"
    alert(test2); //output "s2"
    If you still not clear from the example above, here is another way to explain.

    Code:
    var number_sum = 1 + 2;
    myalert(number_sum);
    var string_sum = "Hello" + " world";
    myalert(string_sum);
    
    function myalert(input) {
        //what is the type of the input? But does it matter?
        alert(typeof(input));
    
        console.log(input);   //let's output to the console;
        document.write(input); //let's output to the HTML page 
    }
    Here is another example of loose-type language, I just rewrote your code in little bit different way. In this example, the single function takes different type of signatures (parameters) but yet this is totally valid in Javascript. However this will throw an error in any strong-type language.

    If you still think you can properly write overloading functions in Javascript that follows all overloading definitions, please show me, write actual code for me. I'd would like to be corrected if I am wrong so I can learn.
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    I told you that JavaScript has operator overloading (not method overloading!). I gave you a link which explains exactly what operator overloading means. You simply dismissed that and wrote down a useless "proof" that JavaScript doesn't support classical method overloading. Who said it does? I didn't.

    If you deny the existence of operator overloading because you're not familiar with it, I don't think I can help you.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    22
    Rep Power
    0
    Originally Posted by Jacques1
    I told you that JavaScript has operator overloading (not method overloading!). I gave you a link which explains exactly what operator overloading means. You simply dismissed that and wrote down a useless "proof" that JavaScript doesn't support classical method overloading. Who said it does? I didn't.

    If you deny the existence of operator overloading because you're not familiar with it, I don't think I can help you.
    I am NOT saying there is no such thing as "operator overloading". But again, I think you are confused with Loose-type vs Strong-type. You might want to re-read your wiki link that you added.
    From you WIKI page, (~ line #10)
    In a language that supports operator overloading, and with the usual assumption[..]

    Javascript does NOT support "operator overloading". You can simulate it but you can NOT operator overloading in Javascript.

    This is because of couple reasons,
    #1 You can NOT accurately write overload functions in Javascript.
    #2 Javascript operators can neither be extended nor over written.

    Once again, if you still think you can overload Javascript operators, please give us the code example like in the wiki page that you made the reference to.
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    *sigh*

    I already gave you an example: The "+" operator. It has three different functionalities depending on the types of the operands. It does integer addition when applied to integers, float addition when applied to floats and string concatenation when applied to strings. This is the very definition of operator overloading.

    No, you cannot define or change operators in JavaScript. No, you cannot do function or method overloading (how often do I have to repeat that?). Nobody said that you can. I said that the built-in "+" operator has built-in overloading. Nothing more, nothing less.

    I don't know why this is so hard to understand. But it seems to be a general problem: People coming from "classical" languages like Java or C++ generally tend to have difficulties understanding JavaScript, because it's so different. For example, I've often heard people claim that JavaScript "isn't object-oriented" because it doesn't have classes.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    22
    Rep Power
    0
    First of all, I do enjoy intelligent technical discussion.
    Having said that, you need to able to prove something, when you make an assumption.

    You assume that javascript native core binary code has operator overloading functions.

    It seems (correct me if I am wrong) you agree that operator overloading is impossible at highlevel (user accessible code) in javascript.

    When people say "operator overloading" (or even general overloading), they mean at user-level (highlevel) code. I never met anyone who refer to lower level code. I don't even know concept of overloading is even possible at low level programming. And even your WIKI page is referring to highlevel programming and it has how to write operator overloading code. But What I am saying is that is impossible in Javascript.

    If I may, technically, Javascript is interpreted language which means, there is an interpreter that turns Javascript to machine language.

    I was going to convince you by example (once again) but I think this might be better.

    http://www.google.com/search?q=how+to+operator+overloading+in+javascript

    If you can find any site that shows operator overloading in javascript, please let me know.

    FYI, people who say Javascript is not object-oriented language is not because of it doesn't have "class" but it is b/c of Javascript does not have all 3 main fundamental functionality of object oriented language which are Encapsulation, Inheritance, and Polymorphism.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    22
    Rep Power
    0
    @Jacques1 If I give an idea that I agree with you on "+" has overloading functions in the native code in the previous post, I did not mean that.

    Although I can see where your argument is coming from, that is (again) loose-type language behavior.

    https://en.wikipedia.org/wiki/Strong...Predictability


    (From the WIKI)
    Predictability
    Some programmers refer to a language as "weakly typed" if simple operations do not behave in a way that they would expect. For example, consider the following program:

    x = "5" + 6

    Different languages will assign a different value to 'x':
    One language might convert 6 to a string, and concatenate the two arguments to produce the string "56".
    Another language might convert "5" to a number, and add the two arguments to produce the number 11.
    Yet another language might convert the string "5" to a pointer representing where the string is stored within memory, and add 6 to that value to produce a semi-random address.
    And yet another language might simply fail to compile this program, saying that the two operands have incompatible type.
    Languages that work like the first three examples have all been called "weakly typed" at various times, even though only one of them (the third) represents a safety violation.
  24. #13
  25. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by jkdev
    FYI, people who say Javascript is not object-oriented language is not because of it doesn't have "class" but it is b/c of Javascript does not have all 3 main fundamental functionality of object oriented language which are Encapsulation, Inheritance, and Polymorphism.
    *lol*

    So which of those three attributes does JavaScript not have? If you tell me it doesn't have inheritance, Douglas Crockford will come after you.



    Originally Posted by jkdev
    Although I can see where your argument is coming from, that is (again) loose-type language behavior.
    What on earth does this have to do with weak typing? The operands have the same type, and there is no type conversion at all. That's the whole point. "string + string" yields a string in JavaScript. And "integer + integer" yields an integer.

    You can actually do the same thing in Java: Try 1 + 2 and "a" + "b". What do you get? 3 and "ab", right? Oops. Does that mean Java is actually a weakly-typed language?

    Now try PHP: 1 + 2 yields 3, "a" + "b" yields 0. Does that mean PHP is not a weakly-typed language?

    As you can see, your definition is nonsense. What you call "weakly-typed behaviour" can be found both in strongly-typed and weakly-typed languages. On the other hand, some weakly-typed language don't have it at all.

    So what do we call this strange behaviour that can be found both in weakly-typed and strongly-typed languages? We call it ... operator overloading.

    You can choose a different name if you want to. Make up your own term (but not "weakly-typed behaviour"). I prefer to call it what it is: built-in operator overloading.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    22
    Rep Power
    0
    Originally Posted by Jacques1
    *lol*
    So which of those three attributes does JavaScript not have? If you tell me it doesn't have inheritance, Douglas Crockford will come after you.
    First of all, I was just stating the reason that for those who argue that JS is not being OO. Because JS is prototype-based language. Again, this is NOT my augment. This is long philosophical debate that I am not going to be in this post. But both sides have valid points. My side on this topic is "IT DOENS'T MATTER". But as a developer, I can respect constructive points on both side.

    You can actually do the same thing in Java: Try 1 + 2 and "a" + "b". What do you get? 3 and "ab", right? Oops. Does that mean Java is actually a weakly-typed language?

    Now try PHP: 1 + 2 yields 3, "a" + "b" yields 0. Does that mean PHP is not a weakly-typed language?
    First, did you even read the WIKI? or are you saying the WIKI is incorrect? or are you just arguing for sake of the argument?

    The WIKI just clearly state and discard your argument. Try to READ this time. Once again, this information is from WIKI, not my word. If you disagree with what it said, you should take it up to WIKI page and try to update yourself.

    Second, what the heck are you talking about in your example? Do you even know PHP? in PHP the plus sign "+" is only reserve for mathematic login, string join is using dot "." Any string that passed to mathematic login will be considered as 0 . What does have anything do operator overloading?

    Furthermore I don't even know what you trying to prove by 1+2 and "a"+"b" in java, if you try to prove something (whatever that is) try to run 1 +"a" on java (or any string-type language) and tell me if that doesn't give you an error.

    PS. did you even click on the google search link that I kindly did for you in previous post? If the whole world is disagree with you, maybe you should think that you might be wrong. Unless you think everyone else is wrong and you are the only one that right.
  28. #15
  29. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    I'll give it one last try.

    The Wiki says that JavaScript doesn't allow you to define your own overloaded operators. I never said that you could do that. Why do you keep arguing against a statement none of us made?

    OK, how would you define function overloading? That the same function call can behave differently depending on the types of the arguments, right? We don't know what the call f(x) does until we know the type of a.

    Operator overloading is the exact same thing for operators. Just replace "function" with "operator" and "arguments" with "operands": The same operator can behave differently depending on the types of the operands. We don't know what a + b does until we know the types of a and b. If they're integers, we'll get the mathematical sum. If they're strings, they'll (probably) be concatenated.

    This has nothing to do with weak typing and strong typing. I just proved that with my example: Some strongly-typed languages (like Java) do have operator overloading, some don't. Some weakly-typed languages (like JavaScript) do have overloading, some don't (like PHP).

    No, you can't define your own overloaded operators in JavaScript. So what? You cannot define custom operators at all. Does that mean JavaScript has no operators?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo