Which one is overriding and which one is overloading
-
1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????
-
1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????
Arun kumar Gautam wrote:
it overloading or overriding????
According to the code you have neither. Polymorphism (search for overridden methods)[^] Overloading[^] The first would require one class to subclass and have a method with the same name AND input parameters (and the virtual/override keywords in c#). The second requires two methods with the same name but different input parameters (these can be in the same class or inheritance hierarchy). If b subclassed a or vice versa add would be overloaded, but there would be no overriding. Perhaps you copied the code from the question down incorrectly?
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
“One of the greatest problems of our time is that many are schooled but few are educated”Sir Thomas More (1478 – 1535)
-
1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????
Override is when a class will "override" or implement it's own version of a method in a parent class. Overload is when the same named function has different signatures. In your example, if both add functions were in the same class it would be overloaded.
There are only 10 types of people in the world, those who understand binary and those who don't.
-
1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????
-
1. class a{ public int add(int x) { return x; } } 2. class b { public double add(int x, int y, int z) { return x+y+z; } } m little confused about overriding and overloading can anyone tells wht happn in the above methods is it overloading or overriding????
but according to the concept of overloading the number of parameters and data type of parameters in two functions must have to be different and according to the concept of overriding the number of parameters and datatype of parameters must have to be same in two functions so i think if the number of parameters and data type of parameters are different of two functions than it must be the overloading so it doesnt matter whether these two function are in same class or in derived class
-
but according to the concept of overloading the number of parameters and data type of parameters in two functions must have to be different and according to the concept of overriding the number of parameters and datatype of parameters must have to be same in two functions so i think if the number of parameters and data type of parameters are different of two functions than it must be the overloading so it doesnt matter whether these two function are in same class or in derived class
Arun kumar Gautam wrote:
but according to the concept of overloading the number of parameters and / or data type of parameters in two functions must have to be different
You've missed other important characteristics: The method must be have the same name (for both overloading and overriding). Overloading happen when you have methods with different parameters, with or without a subclass/superclass but both methods need to be in the same class hierarchy (including the same class - which has a hierarchy of 1). Neither of your
add
methods do this as they they are in unrelated classes, so you don't have overloading. Overriding requires class hierarchy where a method supersedes (overrides) it's equivalent (i.e. having same name, parameters) in class higher up in the hierarchy. As you don't have any subclassing/super classing you can't have overriding in your example. So that takes us back to your original question. Either you have copied the code across incorrectly, or the questioner doesn't know what they are doing/has made a mistake (which is a real possibility, I'm not being facetious).“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
“One of the greatest problems of our time is that many are schooled but few are educated”Sir Thomas More (1478 – 1535)
-
Arun kumar Gautam wrote:
but according to the concept of overloading the number of parameters and / or data type of parameters in two functions must have to be different
You've missed other important characteristics: The method must be have the same name (for both overloading and overriding). Overloading happen when you have methods with different parameters, with or without a subclass/superclass but both methods need to be in the same class hierarchy (including the same class - which has a hierarchy of 1). Neither of your
add
methods do this as they they are in unrelated classes, so you don't have overloading. Overriding requires class hierarchy where a method supersedes (overrides) it's equivalent (i.e. having same name, parameters) in class higher up in the hierarchy. As you don't have any subclassing/super classing you can't have overriding in your example. So that takes us back to your original question. Either you have copied the code across incorrectly, or the questioner doesn't know what they are doing/has made a mistake (which is a real possibility, I'm not being facetious).“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
“One of the greatest problems of our time is that many are schooled but few are educated”Sir Thomas More (1478 – 1535)
1. class a{ public int add(int x) { return x; } } 2. class b:a { public double add(int x, int y, int z) { return x+y+z; } } sorry now can u help me for clearing my doubt.... is it overloading or overriding...????
-
1. class a{ public int add(int x) { return x; } } 2. class b:a { public double add(int x, int y, int z) { return x+y+z; } } sorry now can u help me for clearing my doubt.... is it overloading or overriding...????
Arun kumar Gautam wrote:
class b:a
That makes all the difference.
b.Add
overloadsa.add
as:- Both methods are available from type
b
. - Both methods have the same name
b.add
has a different input signature
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
“One of the greatest problems of our time is that many are schooled but few are educated”Sir Thomas More (1478 – 1535)
- Both methods are available from type
-
but according to the concept of overloading the number of parameters and data type of parameters in two functions must have to be different and according to the concept of overriding the number of parameters and datatype of parameters must have to be same in two functions so i think if the number of parameters and data type of parameters are different of two functions than it must be the overloading so it doesnt matter whether these two function are in same class or in derived class
Overloadings can be overridden, but that requires inheriting a class/interface that already has the overloadings defined. You can also overload without inheriting from another class. However to Override you need to inherit from another class that already has the method with a signature defined.
-
Arun kumar Gautam wrote:
class b:a
That makes all the difference.
b.Add
overloadsa.add
as:- Both methods are available from type
b
. - Both methods have the same name
b.add
has a different input signature
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
“One of the greatest problems of our time is that many are schooled but few are educated”Sir Thomas More (1478 – 1535)
thnks for answer it make my concept completely clear.... thnks for this
- Both methods are available from type
-
thnks for answer it make my concept completely clear.... thnks for this
No Problem.
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
“One of the greatest problems of our time is that many are schooled but few are educated”Sir Thomas More (1478 – 1535)