Академический Документы
Профессиональный Документы
Культура Документы
A list of differences between method overloading and method overriding are given below:
o Method overloading is used to increase the readability of the program.
Method overriding is used to provide the specific implementation of the method
that is already provided by its super class.
o Method overloading is performed within class.
Method overriding occurs in two classes that have IS-A (inheritance) relationship.
o In case of method overloading, parameter must be different.
In case of method overriding, parameter must be same.
o Method overloading is the example of compile time polymorphism.
Method overriding is the example of run time polymorphism.
o In java, method overloading can't be performed by changing return type of the
method only. Return type can be same or different in method overloading. But
you must have to change the parameter.
Return type must be same or covariant in method overriding.
o Static methods can be overloaded which means a class can have more than one
static method of same name. Static methods cannot be overridden, even if you
declare a same static method in child class it has nothing to do with the same
method of parent class.
o The most basic difference is that overloading is being done in the same class while
for overriding base and child classes are required. Overriding is all about giving a
specific implementation to the inherited method of parent class.
o Static binding is being used for overloaded methods and dynamic binding is
being used for overridden/overriding methods.
o Performance: Overloading gives better performance compared to overriding. The
reason is that the binding of overridden methods is being done at runtime.
o private and final methods can be overloaded but they cannot be overridden. It
means a class can have more than one private/final methods of same name but a
child class cannot override the private/final methods of their base class.
class OverloadingExample{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
}
Abstract and Final class are different basically in their nature and usage in Java.
Final Classes
final classes are the way we can prevent class being extended
we can instantiate final class and immutable objects can be created
these cannot contain abstract methods
must have all the method implementations in it
Example
final class Bike{}
Concrete class
A concrete class in java is any such class which has implementation of all of its inherited
members either from interface or abstract class. Lets take an example:
interface B {
public void printB();
}