Page 1 of 2 12 LastLast
Results 1 to 20 of 33

Thread: Difference between method overriding and overloading

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Contributing Member
    Join Date
    Jul 2006
    Answers
    76

    Difference between method overriding and overloading

    Is the concept of method overriding different from the concept of overloading. If so how they both differ.


  2. #2
    Junior Member
    Join Date
    Jun 2006
    Answers
    4

    Re: Difference between method overriding and overloading

    Hi nancy,there is big difference between both:
    see following example

    When overriding, you change the method behavior for a derived class.
    e.g Clas A
    {
    Virtual void hi(int a)
    {
    }
    }

    Class B:A
    {
    public overrid void hi(int a)
    {

    }
    }

    Overloading simply involves having a method with the same name within the class.

    Example for Over loading

    Class A
    {
    class a()

    {

    }
    class a(int a)
    {
    }
    }


  3. #3
    Contributing Member
    Join Date
    May 2006
    Answers
    85

    Re: Difference between method overriding and overloading

    Method overriding is a feature in Object Oriented programming language. This is used to implement a method for subclass which overrides in other words replaces the implementation of the super class. Overloading the concept of providing different meaning to a object based on the context of its presence. Overloading is one type of polymorphism and this is also a feature in Object Oriented programming language.


  4. #4
    Expert Member
    Join Date
    Nov 2006
    Answers
    518

    Re: Difference between method overriding and overloading

    In overriding,
    The method of a sub class takes priority over its counterpart in the super-class.

    where as in Overloading,
    2 or more methods with same name are available (have no priority over each other) but differ in their declaration and/or definition. Either of them may be executed depending on the number and/or type of arguments passed.

    Any further clarifications? Feel free to ask


  5. #5
    Expert Member
    Join Date
    Dec 2006
    Answers
    204

    Re: Difference between method overriding and overloading

    Overriding is the concept of having functions of same name and signature in different classes. one in the super class can be made virtual and other can override the functionality of virtual one.

    Overloading is the concept of having functions of same name, but different signature in same class. They are differentiated by the compiler by their signatures.


  6. #6
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Smile Re: Difference between method overriding and overloading

    Using overloading and overridding, you can acheive the concept of polymorphism.

    Polymorphism means "one name, multiple forms". Using one name u can do multiple of actions...

    Method overloading is a compile time polymorphism and Method Overridding is a runtime polymorphism...

    Compile time polymorphism means compiler knows which object assigned to which class at the compiling time....Runtime polymorphism means compiler didn't know at the compile time, it only knows at a run time...

    ---------------------
    suresh


  7. #7
    Contributing Member
    Join Date
    Mar 2007
    Answers
    34

    Re: Difference between method overriding and overloading

    methjod overloading means one method can callto diffrent sub class of base class and in method overriding method in subclass takes priority over it's counterpart.


  8. #8
    Junior Member
    Join Date
    Apr 2007
    Answers
    2

    Re: Difference between method overriding and overloading

    Overloading methods
    1.They appear in the same class or a subclass
    2.They have the same name but, have different parameter lists, and can have different return types.
    An example of an overloaded method is print() in the java.io.PrintStream class

    Overriding methods
    It allows a subclass to re-define a method it inherits from it's superclass
    overriding methods:
    1. It appears in subclasses
    2. They have the same name as a superclass method
    3. They have the same parameter list as a superclass method
    4. They have the same return type as as a superclass method
    5. They have the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method
    ·If the superclass method is public, the overriding method must be public
    ·If the superclass method is protected, the overriding method may be protected or public
    ·If the superclass method is package, the overriding method may be packagage, protected, or public
    ·If the superclass methods is private, it is not inherited and overriding is not an issue


  9. #9
    Junior Member
    Join Date
    Apr 2007
    Answers
    2

    Re: Difference between method overriding and overloading

    What is overloading in java ?
    In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case,the methods are said to be overloaded, and the process is referred to as method
    overloading. Method overloading is one of the ways that Java implements polymorphism.

    What is Overriding in java ?
    when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.


  10. #10
    Junior Member
    Join Date
    May 2007
    Answers
    2

    Thumbs up Re: Difference between method overriding and overloading

    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.


  11. #11

    Re: Difference between method overriding and overloading

    method overloading is designtime polymorphism whereas method overriding is runtime polymorphism..
    In overloading the parameters should be uniqe i.e the no of parameters shud differ or if same no of parameters r there the signature shud differ
    why because the compiler should know the methods in complintime only.
    whereas in overriding we reimplement or change the functionality of the base class method in derived class .the no of parameters & return type shud be same ..


  12. #12
    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.


  13. #13
    Junior Member
    Join Date
    Jul 2007
    Answers
    1

    Re: Difference between method overriding and overloading

    Let me give one example.

    METHOD OVERLOADING

    class human(
    {
    void Hand(two paremeters){
    Eating}
    void Hand(three parameters) {
    Writing }
    void Hand(Five parameters) {
    Fighting }

    METHOD OVERIDING

    class father {

    void Hand(5 parameters){
    Smoking;
    }
    }
    class child extends father{
    void Hand(5 parameters){
    Drinking}
    Thanks


  14. #14
    Junior Member
    Join Date
    Jul 2007
    Answers
    2

    Thumbs up 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.
    OverLoading : when u want extend ur feature then u will use OverLoading.
    OverRiding : When u dont want to use the existing feature then u will use OverRiding


  15. #15
    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..


  16. #16
    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.

  17. #17

    method overriding and ovverloading

    Overriding method definitions
    In a derived class, if you include a method definition that has the same name and exactly the same number and types of parameters as a method already defined in the base class, this new definition replaces the old definition of the method.

    Explanation
    A subclass inherits methods from a superclass. Sometimes, it is necessary for the subclass to modify the methods defined in the superclass. This is referred to as method overriding. The following example demonstrates method overriding.

    Step 1
    In this example we will define a base class called Circle

    class Circle {

    //declaring the instance variable
    protected double radius;

    public Circle(double radius) {
    this.radius = radius;
    }

    // other method definitions here

    public double getArea() {
    return Math.PI*radius*radius;
    }//this method returns the area of the circle

    }// end of class circle

    When the getArea method is invoked from an instance of the Circle class, the method returns the area of the circle.

    Step 2
    The next step is to define a subclass to override the getArea() method in the Circle class. The derived class will be the Cylinder class. The getArea() method in the Circle class computes the area of a circle, while the getArea method in the Cylinder class computes the surface area of a cylinder.

    The Cylinder class is defined below.

    class Cylinder extends Circle {

    //declaring the instance variable
    protected double length;

    public Cylinder(double radius, double length) {
    super(radius);
    this.length = length;
    }

    // other method definitions here

    public double getArea() { // method overriden here
    return 2*super.getArea()+2*Math.PI*radius*length;
    }//this method returns the cylinder surface area

    }// end of class Cylinder

    When the overriden method (getArea) is invoked for an object of the Cylinder class, the new definition of the method is called and not the old definition from the superclass(Circle).

    Example code
    This is the code to instantiate the above two classes

    Circle myCircle;
    myCircle = new Circle(1.20);

    Cylinder myCylinder;
    myCylinder = new Cylinder(1.20,2.50);

    Please let me know if you need anything more


  18. #18
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: Difference between method overriding and overloading

    Some of the threads are merged for better management of the site.

    MODERATOR


  19. #19
    Junior Member
    Join Date
    Mar 2006
    Answers
    5

    Re: Difference between method overriding and overloading

    Overriding is the Functionality by witch a programmer can change the old functionality of any function even if he does`t know that what was the old one.
    But in Overloading you have to change the parameter or the return type. this is the Conceptual difference


  20. #20
    Contributing Member
    Join Date
    Nov 2007
    Answers
    46

    Thumbs up Re: Difference between method overriding and overloading

    according to overriding:

    mean - overriding the previous text with new text
    accessing:

    if u want to access this method from the super class to sub class that must be same return type and same method name.

    example

    class Superclass{
    void display()
    {
    System.out.println("this is super class display");
    }
    }
    class Subclass extends Superclass{
    void display()
    {
    System.out.println("this is sub class display");
    }
    }
    class Myclass{
    public static void main(String args[])
    {
    subclass s=new subclass();
    s.display();
    }
    output:
    this is subclass display
    explanation:
    the subclass method display() simply overrided the method in superclass.

    problems:these steps must be follow for overriding the method

    -return type must be same
    -access specifier must be same
    -method name is equal in both classes.

    according to overloading:

    one name with different arguments and the return type may be any one.
    overloading the methods in the same class possible.

    example:
    void display();
    int display(int, int);
    double display(double,double);


Page 1 of 2 12 LastLast

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