Method overloading in java |
---|
If a class have multiple methods of same name but different argument list and type known as method overloading.
Advantage : Method overloading increases the readability of the program.
Point to be noted of method overloading.
1.Multiple method with same name but Different argument list and argument type.
2.Return type and access modifier may or may not be different.
4.overload happen in same class
5.invoking done based on signature of method.
6.Binding happens at compile time
Example of a method overloading.
class Demo { void sum(int a, int b) { System.out.println(a+b); } void sum(int a, int b, int c) { System.out.println(a+b+c); } public static void main(String args[]) { Demo d=new Demo(); d.sum(2,4); d.sum(2,3,4); } }
In java, method overloading is not possible by changing the only the return type of the method because there may occur ambiguity.
If we try this we will get compile time error saying method already defined.
class Calculation { int sum(int x,int y) { System.out.println(x+y); } double sum(int a,int b) { System.out.println(a+b); } public static void main(String args[]) { Calculation obj=new Calculation(); int result=obj.sum(20,25); // compile time error. } }
Q. Can we overload main() method?
Yes, You can have any number of main methods in a class by method overloading. Let's see the simple example:
class Overloading{ public static void main(int a) { System.out.println(a); } public static void main(String args[]) { System.out.println("main() method invoked"); main(20); } }
Method Overriding in java |
---|
If child class has the same method as declared in the parent class, It is known as method overriding.
- Method overriding is used to provide specific implementation of a method that is already provided by its super class.
- Method overriding is used for runtime polymorphism.
Point to be noted of java overriding:
Method name same,and argument list and argument type should same as in the parent class.
Happens between super(parents) or base( child ) class. must be IS-A relationship (inheritance).
Invoking done based on the data type of the object referred by the reference.
Happens at run time.
Example of method overriding
class Vehicle { void run() { System.out.println("vehicle is running"); } } class Bike extends Vehicle { void run() { System.out.println("bike is running"); } public static void main(String args[]) { Bike b=new Bike(); b.run(); } }
Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Bike
bike is running
Example of polymorphism
class C { void test() { System.out.println("C-test"); } } class D extends C { void test() { System.out.println("D-test"); } } class Demo { static void method(C c1) { c1.test(); } public static void main(String args[]) { C obj1=new C(); D obj2=new D(); method(obj1); method(obj2); } }
Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo
C-test
D-test
Difference between method overloading and method overriding in java.
Overloading:
1.Multiple method with same name but Different argument list and argument type.
2.Return type and access modifier may or may not be different
4.Overload happen in same class
5.Invoking done based on signature of method.
6.Binding happens at compile time( Example of compile time polymorphism).
Overriding:
1.Method name same,and argument list and argument type should same as in the parent class.
2.Return type must be same or covariant in method overriding.
3.Happens between super(parents) or base( child ) class. must be IS-A relationship (inheritance).
5.Invoking done based on the data type of the object reffered by the reference.
6.Happens at run time (Example of run time polymorphism).
Programs on Methos overloading and overriding |
---|
Program # 1
class A { void test1(int i) { System.out.println("Test1(int)"); } int test1() { System.out.println("Test()"); return 10; } public void test1(double d) { System.out.println("Test1(double)"); } void test1(int i,int j) { System.out.println("test1(int,int)"); } } class Manager { public static void main(String[] args) { A a1=new A(); a1.test1(10); a1.test1(); a1.test1(20.9); a1.test1(10,11);} }
OUTPUT:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Manager
Test1(int)
Test()
Test1(double)
test1(int,int)
Program # 2
class D { static void test() { System.out.println("Hello World!"); } public int test() { return 20; } }
OUTPUT:
mukesh@ubuntu:~/Desktop/javaPrograms$ javac Test.java
Test.java:7: error: method test() is already defined in class D
public int test()
^
1 error
Note: In above program
- compiler error : duplicate methods
- Parameter must be diffrent
Program # 3
abstract class E { abstract void test(); void test() { int i=10; } }
OUTPUT:
Note: compiler error : duplicate methods
Program # 4
abstract class F { abstract void test(); abstract void test(int i); void test(int i,int j) { System.out.println("Test1(int,int)"); } } class G extends F { void test() { System.out.println("Test()"); } void test(int i) { System.out.println("Test(int)"); } public static void main(String[] args) { G g1=new G(); g1.test(); g1.test(10); g1.test(1,20); } }
OUTPUT:
Test()
Test(int)
Test1(int,int)
Program # 5
class H { void test1() { System.out.println("Test1()"); } void test2() { System.out.println("Test2()"); } } class I extends H { void test2() { System.out.println("Modified Test2()"); } public static void main(String[] args) { I obj=new I(); obj.test1(); obj.test2(); } }OUTPUT:
Test1()
Modified Test2()
Program # 6
abstract class J { abstract void test(int i); } class K extends J { void test() { System.out.println("Test()"); } /*void test(int i) { System.out.println("test(int)"); }*/ }
OUTPUT:
K.java:6: K is not abstract and does not override abstract method test(int) in J
class K extends J
^
1 error
Program # 7 : While overriding return types of method should be same.
class L { void test1() { System.out.println("test1()"); } } class M extends L { int test1() { System.out.println("test1()"); return 10; } }
OUTPUT:
mukesh@ubuntu:~/Desktop/javaPrograms$ javac Test.java
Test.java:10: error: test1() in M cannot override test1() in L
int test1()
^
return type int is not compatible with void
1 error
Program # 8
abstract class N { abstract int test1(); } class O extends N { void test1() { } }
OUTPUT:
Compiler error return type are not same,could not override
Program # 9
abstract class U { public abstract void test1(); } class V extends U { void test1() //error //public void test1() //compile success { } }
OUTPUT:
V.java:8: test1() in V cannot override test1() in U; attempting to assign weaker access privileges; was public
void test1()
^
1 error
********************************* End *********************************
No comments:
Post a Comment