Method Overloading in java
Output:
Method Overriding in java:
- Method overloading is nothing but defining multiple methods with same name with
different parameters is known as method overloading - while compile time itself we know which method going to get executed.
- Overloading gives better performance compared to overriding. The reason is that the
binding of overridden methods is being done at runtime. - Argument list should be different while doing method overloading.
- Return type of overloaded methods should be same.
static binding is used for overloaded methods
- package com.instanceofjava;
- class A{
- public void show(int a){
- System.out.println("saidesh");
- }
- public void show(int a,int b){
- System.out.println("ajay");
- }
- public void show(float a){
- System.out.println("vinod");
- }
- public static void main(String args[]){
- A a=new A();
- a.show(10);
- a.show(1,2);
- a.show(1.2);
- }
- }
- saidesh
- ajay
- vinod
Method Overriding in java:
- Method overriding is nothing but defining multiple methods with same name with
same definition in super class and sub class is known as Method overriding.
While Run time only now which method is Executed. - Overriding gives slower performance compared to overloading.The reason is that the
binding of overridden methods is being done at run time. - Argument list should be same while doing method overriding.
Polymorphism applies to overriding. - In method overriding the return type of overriding method can be different from overridden
method. - Dynamic binding used for method overriding methods
No comments