2.7 Method Overloading & Method Overriding.

Method Overloading :

Some operation with different parameters.

Example :

class Demo1
{
void test(int arg)
{
-
}
void test(double arg)
{
-
}
void test(char arg)
{
-
}
}

Example Program1 :

class Demo1
{
void test()
{
System.out.println("rnning no arg test() method");
}
void test(int arg)
{
System.out.println("rnning int arg test() method");
System.out.println("arg value is"+arg);
}
void test(double arg)
{
System.out.println("rnning double arg test() method");
System.out.println("arg value is"+arg);
}
void test(int arg1, double arg2)
{
System.out.println("rnning int, double arg test() method");
System.out.println("arg1 value is"+arg1);
System.out.println("arg2 value is"+arg2);
}
}
class MainClass1
{
public static void main(String args[])
{
System.out.println("main method started");
Demo1 d1 = new Demo1();
d1.test();
d1.test(25);
d1.test(4.6);
d1.test(14, 7.2);
System.out.println("program ended");
}
}

Example Program2 :

class Demo2
{
void disp()
{
System.out.println("running disp() method");
}
}
class Sample1 extends Demo2
{
void disp(int avg)
{
System.out.println("running disp(int) method");
System.out.println("avg value:"+avg);
}
}
class MainClass2
{
public static void main(String args[])
{
System.out.println("main method started");
Sample1 s1 = new Sample1();
s1.disp();
s1.disp(14);
System.out.println("program ended");
}
}

Notes:

--> In a class  defining multiple method with same name at different parametre is known as method overloaded.

--> Parametre should differ either interms of parametre type or parametre length.
`
--> In a class we can overload both static and non-static methods.

--> Sub class can overload the methods of super class.

--> Overloaded method are invoked based on the parametre type.

--> Method overloading is used to assume compile time polymorphism.

--> Main method can be overloaded with different parametre. JVM begins the execution by calling main method with string array argument.

Method Overriding :

Need IS-A(inheritance) relationship.

Example :

class Demo1
{
void test() //declaration.
{
- //method body or method implementation.
}
}


class B extends A
{
void test()
{
- //new implementation.
}
}


Example Program1

class Demo1
{
void test()
{
System.out.println("running test() method");
}
}
class Demo2 extends Demo1
{
void test()
{
System.out.println("test() metod overriding in Demo2");
}
}
class MainClass1
{
public static void main(String args[])
{
System.out.println("Program Started");
Demo2 d1 = new Demo2();
d1.test();
System.out.println("Program Ended");
}
}

Notes :

-> Inheriting a method from super class changing its implementation in the subclass according to sub-class specification is known as method overriding.
-> when performing method overriding sub-class the same signature of super class and change the implementation.
-> To perform method according to IS-A relationship is pass.
-> The sub-class cannot override the following method are super class.
1. static
2. private
3. final
-> because static method cannot be inheritance.
-> final method can be inherited but the final keyword doesn't alone will change the implementation in subclass.
-> private-: this method has restriction upto the class it can not be access from out side the class.

-> method overridng used to achieve run-time polymorphism.
-> when developing application if we want to change the implementation of any functionality than we go for method overriding.
-> The overrided context can access the super class implementation by using super keyword.




Comments

Popular Posts