Sie sind auf Seite 1von 3

 There are many differences between method overloading and method overriding in java.

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.

Java Method Overloading example

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;}
}

Java Method Overriding example

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.

I will list out the difference between the both.

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{}

class Honda1 extends Bike{


void run(){System.out.println("running safely with 100kmph");}

public static void main(String args[]){


Honda1 honda= new Honda();
honda.run();
}
}
Test it Now
Output:Compile Time Error

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:

public abstract class A {


public abstract void methodA();
}

interface B {
public void printB();
}

public class C extends A implements B {


public void methodA() {
System.out.print("I am abstract implementation");
}

public void printB() {


System.out.print("I am interface implementation");
}
}

In the above example class C is a concrete class.

Das könnte Ihnen auch gefallen