Results 1 to 14 of 14

Thread: overriding and overloading method

  1. #1
    Junior Member
    Join Date
    Dec 2007
    Answers
    2

    overriding and overloading method

    method of overriding and overloading


  2. #2
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: overriding and overloading method

    Overloading
    The signature of a method is the name of the method; the number of, order of, and types of arguments of the method, as well as the return type.

    The argument signature of a method is the number of, order of, and types of the arguments to a method.

    In Java a method is overloaded if it has the same name as another method but different argument signature. Two methods may NOT have the same name and argument signature but differ only in return type and/or exception specifications.

    Overriding
    Overriding occurs only with respect to inheritance and occurs when a child has a method with the same signature as a parent's method.

    Java ALWAYS invokes the most derived VIEWABLE version of a method based on the scope of the present reference. The super reference access forces the method resolution system to not consider any classes below the superclass when finding the most derived method to invoke.


  3. #3
    Junior Member
    Join Date
    Aug 2007
    Answers
    5

    Smile Re: overriding and overloading method

    Method overloading:
    Overloading is a powerful feature in java. In method overloading the methods can have same name but different parameters.
    Ex. sample(int a)
    sample(int a, int b)

    Method overriding:
    Overriding is used in inheritance.Method Overriding is achieved when a subclass overrides non-static methods defined in the superclass, following which the new method implementation in the subclass that is executed.


  4. #4
    Junior Member
    Join Date
    Dec 2007
    Answers
    4

    Re: overriding and overloading method

    if u want to add two numbers..may be int,float,double.
    u can't use same methods for different datatypes...
    so what u have to do is write the methods which is accepted to take different arguments.
    void add(int a,int b) {}//1
    void add(float a,float b) { }//2
    void add(double a,double c) { }//3

    in the above steps,, u wrote three methods in which one takes two int,one take two float,one take two double numbers as arguments.

    so when u call this method by supplying two integer numbers,
    first functon would be called.
    so in this method overloading based on the arguments u supplied the method which is defined to take the arguments you passes would be called.
    so for a method to be overloaded...
    it should not have the same type of arguments in same number.
    conditions for method overloading:
    1>different number of arguments
    2>different type of arguments
    3>return type is not considered.

    Last edited by onelastman; 12-14-2007 at 02:57 AM.

  5. #5
    Junior Member
    Join Date
    Dec 2007
    Answers
    4

    Re: overriding and overloading method

    method overriding is a different scenario.this is done when we go for inheritance.
    conditions for method overriding:
    1>should have same type of arguments in same number.
    2>sould exactly match the same method in it's super class..including throws Exception.


  6. #6
    Junior Member
    Join Date
    Nov 2006
    Answers
    3

    Re: overriding and overloading method

    java programming langauge can distinguish methods with different method signature.

    Two components of method declaration comprise of method signature ie. Method name and parameter list..
    That is it is possible to differentiate methods with same method name if they have different parameter list.

    That means that it is possible to define two methods with the same name with in the same class as long as their parameter declarations are different and the process is defined as Method overloading.


    Coming to method overriding
    In a class hierachy, when a method in the subclass have the same name and signature as the method in the super class then then method in the subclass is said to override the method in the super class.
    I hope this explanation is needy one.


  7. #7
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Re: overriding and overloading method

    Method Overloading:
    1) In side the class you can write multipul method with the same name this is called method overloading.
    2) When you overloading the methods you need to change the parameters as follows
    a)Number of parameter
    b)Type of parameter
    c)Order of parameter

    3)When you overloding the method return type of the method can any things.

    Method overriding

    1)The return type, method name, type and order of arguments must be identical to those of a method in a parent class.
    2)The accessibility must not be more restrictive than original method.

    ex:
    class Superclass {
    void show() { }
    }
    class Subclass extends Superclass {
    void show() { }
    }


  8. #8
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Re: overriding and overloading method

    what abt the exceptions thrown by a method while overriding and overloading ? what rules apply to these exceptions while overriding and overloading such methods?


  9. #9
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Re: overriding and overloading method

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

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

    the best answer is that both overloading and over-riding are different aspects of polymorphism.......;-)

    static/early binding polymorphismverloading
    dynamic/late binding polymorphismverriding

    Also the most important thing is : both overloading and over-riding are different aspects of polymorphism.......;-)

    static/early binding polymorphismverloading
    dynamic/late binding polymorphismverriding

    Overloading is compile time binding, where as overriding is dynamic binding.

    Last edited by saurabh.chande; 05-14-2008 at 08:33 AM.

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

    Smile Re: overriding and overloading method

    Method Overloading refers to two or more methods having the same name
    with different parameters. i.e methods should defer in number of parameters.

    methods having same return type will not help in distinguishing which method to invoke.. for example

    void method( int i, int j)
    void method( int i, int j, int k )

    are overloaded as they differ in number of parameters..

    void method()
    int method()
    are not overloaded as return-type alone is insufficient to distinguish the method having the same name..

    Overriding occurs when you redefine a super class method in subclass..
    while overriding a method the signature of the method must be same...

    hope you understand this.. any one can add more points are welcome..



  11. #11
    Junior Member
    Join Date
    Dec 2009
    Answers
    1

    Re: overriding and overloading method

    what is the differences between method binding and late binding? can any one tell me this?


  12. #12
    Junior Member
    Join Date
    Sep 2008
    Answers
    1

    Re: overriding and overloading method

    Due to late binding only JAVA was able to show polymorphism .i.e the method that has to be executed will not be known until runtime where as if you take a case where there is no overloading ( polymoprhism ) all the binding is at compile time which is called early binding.


  13. #13
    Junior Member
    Join Date
    Apr 2009
    Answers
    1

    Re: overriding and overloading method

    overloading----many methods in the same class with same name ,but with different parameters

    Example: sum(int a, int b)
    sum (int a, float b,double c)

    overriding ---- Related to Inheritenace and redefining the functionality of a method in
    child class that has the same signature as defined in the parent class


  14. #14
    Junior Member
    Join Date
    Sep 2008
    Answers
    1

    Re: overriding and overloading method

    overriding
    in overriding case u must extends super class.

    overloading
    overloading in same class

    overloading. is compile time
    and overriding is run time

    Last edited by mujib_khan86; 12-28-2009 at 12:09 PM.

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