Java overloading and overriding
Suppose i have two methods in a class say
public vo开发者_Python百科id eat(int i,String s)
and
public void eat(String s, int i)
then what is it like. Overloading or overriding?
Overloading means two methods or more with the same name but with different parameters just like your example.While Overriding you implement a method from an interface or abstract class so the method in the super class has an implementation and in the subclass has a different one, Still they have the same method name and parameters.
That would be method overloading, as it meets the conditions for method overloading:
- Must have different argument lists
- May have different return types, if argument lists are also different
- May have different access modifiers
- May throw different exceptions
Also overriding can happen only when inheritance is involved. Since both of your methods are in the same class it cannot be overriding.
This is overloading. Overriding is used in inheritance when you give different implementation to the same method signature.
That's overloading. In brief:
Overriding = replacing
Overloading = give more options
Method Overloading simply means providing two separate methods in a class with the same name but different arguments while method return type may or may not be different which allows us to reuse the same method name.
Method Overriding means defining a method in the child class which is already defined in the parent class with same method signature i.e same name, arguments and return type.
Difference Between Method Overloading and Method Overriding
For more details, you can read Everything About Method Overloading Vs Method Overriding.
Kind of rules about overloading and overriding:
- Constructors can be overloaded but not overridden.
- Abstract methods must be overridden by the first concrete sub-class.
- The overriding method:
- must have same argument list,
- must have same return type (it can also be a subclass of parent's class' return type,
- must not have a more restrictive access modifier,
- may have a less restrictive access modifier,
- must not throw new or broader checked exceptions,
- may throw fewer or narrower checked exceptions, or any unchecked exception.
- final methods cannot be overridden.
- Only inherited methods may be overridden, and remember that private methods are not inherited.
- In a subclass use: super.overriddenMethodName() to call superclass' overridden method.
- Overloaded methods:
- must have different argument lists,
- may have different return types, if argument lists are also different,
- may have different access modifiers,
- may throw different exceptions.
- Methods from a superclass can be overloaded in a subclass.
- Polymorphism applies to overriding but not to overloading.
- Object type (not the reference variable's type) determines which overridden method is used at runtime.
- Reference type determines which overloaded method will be used at compile time.
*Taken from Sun Certified Programmer for Java 6 Study Guide by Kathy Seirra, Bert Bates.
Overloading : In Java Overloading is a Process where a class Contains multiple implementation of methods or constructor by differentiating there no of arguments or types of arguments.
public class TestDemo
{
public void disp(String c)
{
System.out.println(c);
}
public void disp(String c, int n)
{
System.out.println(c+" "+n);
} }
class Sample
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
TestDemo obj=new TestDemo();
obj.disp("a");
obj.disp("a",2);
}}
Overriding : Here Methods can be over riding. It must be happen within two class. One is parent & another is child. In this parent class’s method with same name and signature.
public class TestDemo
{
public void eat()
{
System.out.println("Human is Eating");
}
}
class Overriding extends TestDemo
{
public void eat()
{
System.out.println("Human is Eating");
}
public static void main(String[] args)
{
Overriding obj=new Overriding();
obj.eat();
}
}
for more knowledge click on this link : https://arzatechs.com/overloading-and-overriding-in-java/
OVERLOADING:
Two or more methods with same name but different parameters in same class. The same as your question situation.
public void eat(int i,String s)
public void eat(String s, int i)
So, in your case it is method Overloading.
OVERRIDING:
- Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class.
ex.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
}
In this specific case, it's overloading.
Let's differentiate both terms a bit dipper:
Overloading (same function name but different signature):
- Two or more methods having the same name with different arguments in same class.
- Overloading is used when you want to extend class functionality.
- Overloading is is a compile time polymorphism.
Overriding (same function with the same signature):
- Two or more methods having the same method name and same arguments in parent class and child class.
- Overriding is used when you want to reuse the existing functionality.
- Overriding is a run time polymorphism.
Visit to learn more about overloading and overriding.
Property ------------------- OverLoading -------------------- Overriding
Method Names -------------->must be Same--------------------> must be same
Arg Types------------------>must be Different(atleast arg)---->must be same(Including Order)
Method Signature----------->must be Different(atleast arg)---->must be same(Including Order)
Return Type---------------->No restriction------------------->No restriction
Private,Static,Final------->No Restriction-------------------->Must be Same
Access Modifyer------------>No Restriction--------------------Same
try/Catch----------------->No Restriction--------------------Exception Is thrown
Method Resolution-------->Compiler time (Reference)--------(JVM) Run Time Poymorphism
精彩评论