Friday, 11 May 2018

                                  overloading concept

                                                         

case:-1-overloading concept

public class ConsDemo {

public void m1(int a) {
System.out.println("int arg");
}
public void m1(float a)
{
System.out.println("float argument");
}

public static void main(String[] args) {
ConsDemo cd=new ConsDemo();
cd.m1(10);//int arg
cd.m1(12.5f);//float argument
cd.m1('t');//promotion:-int arg
cd.m1(10l);//promotion:-float argument
}
}



==================================================================================
case:-2-overloading concept
//While resolving overloaded methods compiler will always gives the precedence for child type argument when compared with //parent type argument..

public class ConsDemo {

public void m1(String s) {
System.out.println("String version");
}
public void m1(Object a)
{
System.out.println("Object version");
}

public static void main(String[] args) {
ConsDemo cd=new ConsDemo();
cd.m1("Govind");//String version
cd.m1(new Object());//Object version
cd.m1(null);//String version

}
}



==================================================================================
case :-3 overloading concept

public class ConsDemo {

public void m1(String s) {
System.out.println("String version");
}
public void m1(StringBuffer s1)
{
System.out.println("StringBuffer version");
}

public static void main(String[] args) {
ConsDemo cd=new ConsDemo();
cd.m1("Govind");//String version
cd.m1(new StringBuffer("Ballabh"));//StringBuffer version
//cd.m1(null);//compile time error

}
}

==================================================================================

case :-4 overloading concept

public class ConsDemo {

public void m1(int a,float f) {
System.out.println("int float args");
}
public void m1(float f,int a)
{
System.out.println("float int arguments");
}

public static void main(String[] args) {
ConsDemo cd=new ConsDemo();
cd.m1(12, 5.6f);//int float args
cd.m1(2.5f, 20);//float int arguments
//cd.m1(10, 10);//ambiguity error
//cd.m1(2.7f, 8.3f);//cannot find symbol(float,float)
}
}


==================================================================================

Case:-5 overloading concept
public class ConsDemo {
public void m1(int x) {
System.out.println("int args");
}
public void m1(int ...x)
{
System.out.println("varargs method");
}

public static void main(String[] args) {
ConsDemo cd=new ConsDemo();
cd.m1(12,12,12);//varargs method
cd.m1(20);//int args
cd.m1(67,12);//varargs method

}
}
==================================================================================
case:-6 overloading concept
public class Animal
{
}
class Monkey extends Animal
{
}

public class Test {
public void m1(Animal a)
 {
System.out.println("Animal Version");
}
public void m1(Monkey m)
{
System.out.println("Monkey Version");
}
public static void main(String[] args) {
Test t=new Test();
Animal a1=new Animal();
t.m1(a1);//Animal Version

Monkey m=new Monkey();
t.m1(m);// Monkey Version

Animal a2=new Monkey();
t.m1(a2);// Animal Version

//Note:- In overloading method resolution always takes care by
//compiler based on reference type in overloading runtime object won't play any role

}}

No comments:

Post a Comment