Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33

Thread: Difference between method overriding and overloading

  1. #21
    Junior Member
    Join Date
    Jun 2007
    Answers
    17

    Re: Difference between method overriding and overloading

    Quote Originally Posted by nancyphilips View Post
    Is the concept of method overriding different from the concept of overloading. If so how they both differ.
    Function Overriding: In a child class, redefining the default behavior of an inherited function (default behavior of an inherited function is the behavior defined in the parent class for this function). For example,
    class MyParent:
    {
    virtual Func() { print "Hello World"; }
    };
    class MyChild : public MyParent
    { // Default behavior of Func is (parent supplied) "Hello World"
    //Redefining parent supplied behavior
    Func() { Calculate equations; print equations;}
    }
    ---------------------
    Function Overloading: Has nothing to do with inheritance. This is the ability that the programming language offers you, by which you can reuse the lable name of the function to perform different tasks at different times. For example:
    class MyClass
    {
    int MyOLFunction () { print "Hello World"; return 0;}
    int MyOLFunction (int x) { print "Hello World" x number of times; return 0;}
    int MyOLFunction (char a) { print "Hello World"; print a; return 0;}

    //Note: The following are not function overriding, and will generate compile time error
    long MyOLFunction () { print "Hello World"; return 0;} // Error
    char MyOLFunction () { print "Hello World"; return 0;} // Error
    };


  2. #22
    Junior Member
    Join Date
    May 2008
    Answers
    2

    Re: Difference between method overriding and overloading

    Overriding means modifying or enhancing the method implementation in derived class.
    Overloading means class have more than one method having same name but different implementation or different number of input parameters with different return type.


  3. #23
    Contributing Member
    Join Date
    Oct 2008
    Answers
    72

    Re: Difference between method overriding and overloading

    * Overloading deals with multiple methods in the same class with the same name but different signatures

    * Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature

    * Overloading lets you define a similar operation in different ways for different data

    * Overriding lets you define a similar operation in different ways for different object types


  4. #24
    Junior Member
    Join Date
    Nov 2008
    Answers
    3

    Re: Difference between method overriding and overloading

    when ever we want code refinement(means some changes in method body) then that time we r using overloding

    when ever we want cod replacement(means totally changes body) then that time we r useing overriding


  5. #25
    Junior Member
    Join Date
    Sep 2009
    Answers
    2

    Re: define primitive varible?

    i need the answer for the title


  6. #26
    Junior Member
    Join Date
    Oct 2007
    Answers
    3

    Re: Difference between method overriding and overloading

    differences are as follows:
    OVERLOADED OVERRIDING
    1.)arguments- can change must not change

    2.)exceptions-can change cant change except for covariant returns

    3.)invocation-reference type determines which overloaded version is selected at compile time .the actual method tht is invoked is still svirtual method invocation that happens at runtime but the compiler will already know the signature of the method to be invoked .so at the runtime the argument match will already have been nailed down
    but in overriding object type determines which methjod is selcted happens at runtime


  7. #27
    Junior Member
    Join Date
    Dec 2010
    Answers
    1

    Difference between method overriding and overloading

    I have one confusion..

    If we have a method in superclass having signature int abc(int a, int b)

    and we also defined a method in the subclass having method signature int abc(int c)
    then what this phenomenon will be called. This is not overriding. Is this method overloading for subclass.

    any help will be appreciable..


  8. #28
    Junior Member
    Join Date
    Jun 2007
    Answers
    17

    Re: Difference between method overriding and overloading

    Hi there... PPPreeti :-) ... Yes, this is called Function Overloading.

    Overriding is the process of "redefining" the behavior of a parent class' method in the child class.
    Overloading is the process of "creating" a new method with a lable that is already existing within the "scope" of the method.

    As you may already be aware, a function or method is identified by its entire signature - not just its lable name. Lable ("abc" in your example) is only a part of the entire signature.

    Therefore, when you change the signature and keep the lable name intact within a given scope, then you are overloading - regardless of whether you are doing it in a parent or child class. When you are keeping the entire signature (including the lable) intact and rewriting just the body of the function (or behavior of a method) within a child class, then you are overriding the pre-defined (or parent's) behavior of the method.

    In your example, what you are doing is - "creating" a brand new signature. It so happens that one of the components that make up this signature (the lable name) is also used in a different signature (parent class' method) within the same scope.

    By the way, overriding or overloading of a function occurs within the same scope of that function. If done outside the scope, it's neither overloading nor overriding. Say for example, there is a class MyClass with a function func1

    class MyClass {
    public:
    void func1 (int a, int b)
    { for (int i =0; i++ < a; cout << b); }

    };

    and I define another function in a whole different class or even in a main() as
    void func1 (int a, int b) { for (int i =0; i++ < a; cout << b); }

    this is neither overriding (no way related to class MyClass) nor overloading (not within the scope of MyClass::func1 - so no conflict).


    And a couple more things...
    1. For overriding to work, the parent method must be virtual.
    2. For overloading to work, the signature must change, not just the return type. Return type is not a part of the function signature. Whether the return type changes or not does not matter.

    Does it confuse you further???

    --Som G

    Quote Originally Posted by pppreeti View Post
    I have one confusion..

    If we have a method in superclass having signature int abc(int a, int b)

    and we also defined a method in the subclass having method signature int abc(int c)
    then what this phenomenon will be called. This is not overriding. Is this method overloading for subclass.

    any help will be appreciable..


    Last edited by SomGollakota; 12-14-2010 at 11:09 AM.

  9. #29

    Re: Difference between method overriding and overloading

    In java,c++ there are two type of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).

    When you override methods, JVM determines the proper methods to call at the programís run time, not at the compile time. Overriding occurs when a class method has the same name and signature as a method in parent class.

    Overloading occurs when several methods have same names with

    * Overloading is determined at the compile time.
    * Different method signature and different number or type of parameters.
    * Same method signature but different number of parameters.
    * Same method signature and same number of parameters but of different type


    Example of Overloading
    int add(int a,int b)
    float add(float a,int b)
    float add(int a ,float b)
    void add(float a)
    int add(int a)
    void add(int a) //error conflict with the method int add(int a)

    Example: Overloading


    Class BookDetails{
    String title;
    String publisher;
    float price;

    setBook(String title){
    }
    setBook(String title, String publisher){
    }
    setBook(String title, String publisher,float price){
    }


    }

    Example: Overriding

    class BookDetails{
    String title;

    setBook(String title){ }
    }

    class ScienceBook extends BookDetails{

    setBook(String title){} //overriding

    setBook(String title, String publisher,float price){ } //overloading

    }


  10. #30
    Junior Member
    Join Date
    Jul 2011
    Answers
    1

    Re: Difference between method overriding and overloading

    Overriding - same method names with same arguments and same return types associated in a class and its subclass.

    Overloading - same method name with different arguments, may or may not be same return type written in the same class itself.

    _____________________________
    Cegonsoft


  11. #31

    Re: Difference between method overriding and overloading

    if the super class and sub class have the same name and signature then this is called METHOD OVERRIDING.


  12. #32
    Junior Member
    Join Date
    Nov 2011
    Answers
    1

    Re: Difference between method overriding and overloading

    Method overriding and overloading is the different types of polymorphism.
    Method overloading is compile time polymorphism. In overloading function name is same but signature is different.we can say that return type & parameter list should be different.
    Method overriding is runtime polymorphism. In overriding function name & signature are same.It is implemented using virtual & override kyword


  13. #33
    Junior Member
    Join Date
    Jun 2015
    Answers
    1

    Re: Difference between method overriding and overloading

    Quote Originally Posted by sudhansulenka View Post
    overlaoding:functions having same name and same signature.It is a run time polymorphism.Eg: virtual function
    overlaoding:function having same name but different signature.It is a compile time polymorphism.Eg: operator overloading and function overloading.
    In the above explanation, u wrote function overloading. While any1 is function overriding.
    So, pls correct the error where function overloading will come and where function overriding.


Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact