Overloading Cases
package com.sk.demo;
class Overloading{
public static void main(String[] args){
A a = new A();
a.m1("String"); // string type method will call
a.m1(new Object());// Object type method will call
a.m1(new A());// Object type method will call
a.m1(null); // string type method will call since it is applicable in child level also then no need to go for parent level
System.out.println("------------ For B class ------------");
B b = new B();
b.m1(10);
b.m1(new Object());
b.m1(null); // since null not applicable for int type then
System.out.println("-------------- For C class -------------");
C c = new C();
c.m1("Hello");
c.m1(new StringBuffer("Hi"));
c.m1(new Object());
/*
error: reference to m1 is ambiguous
c.m1(null);
^
both method m1(String) in C and method m1(StringBuffer) in C match
*/
c.m1(null);
System.out.println("-------------- For D class -------------");
D d = new D();
d.m1(10,10.5f);
d.m1(10.5f,10);
//Special
/*
error: reference to m1 is ambiguous
d.m1(10,10);
^
both method m1(int,float) in D and method m1(float,int) in D match
*/
d.m1(10,10);
/*
error: no suitable method found for m1(float,float)
d.m1(10.5f,10.5f);
^
method D.m1(int,float) is not applicable
(argument mismatch; possible lossy conversion from float to int)
method D.m1(float,int) is not applicable
(argument mismatch; possible lossy conversion from float to int)
*/
d.m1(10.5f,10.5f);
System.out.println("-------------- For E class -------------");
E e = new E();
e.m1(); // var arg method
e.m1(10,20); // var arg method e.m1(10);// general method since this concept is old op: int
System.out.println("-------------- For F class -------------");
F f = new F();
Parent p = new Parent();
f.m1(p); // parent
Child ch = new Child();
f.m1(ch); // child
// in overloading method resolution is always based on referenced type
Parent p1 = new Child();
f.m1(p1); //Parent
}
}
class A{
public void m1(String p1){
System.out.println("string obj");
}
public void m1(Object p1){
System.out.println("Object type obj");
}
}
class B{
public void m1(int p1){
System.out.println("int obj");
}
public void m1(Object p1){
System.out.println("Object type obj");
}
}
class C{
public void m1(String p1){
System.out.println("string obj");
}
public void m1(Object p1){
System.out.println("Object type obj");
}
public void m1(StringBuffer p1){
System.out.println("StringBuffer type obj");
}
}
class D{
public void m1(int p1,float p2){
System.out.println("int p1,float p2");
}
public void m1(float p1,int p2){
System.out.println("float p1,int p2");
}
}
class E{
public void m1(int p1){
System.out.println("int");
}
public void m1(int... p1){
System.out.println("var arg method");
}
}
/* ----------------------------------------- */
class Parent{
}
class Child extends Parent{
}
class F{
public void m1(Parent p1){
System.out.println("Parent");
}
public void m1(Child p1){
System.out.println("Child");
}
}
class Overloading{
public static void main(String[] args){
A a = new A();
a.m1("String"); // string type method will call
a.m1(new Object());// Object type method will call
a.m1(new A());// Object type method will call
a.m1(null); // string type method will call since it is applicable in child level also then no need to go for parent level
System.out.println("------------ For B class ------------");
B b = new B();
b.m1(10);
b.m1(new Object());
b.m1(null); // since null not applicable for int type then
System.out.println("-------------- For C class -------------");
C c = new C();
c.m1("Hello");
c.m1(new StringBuffer("Hi"));
c.m1(new Object());
/*
error: reference to m1 is ambiguous
c.m1(null);
^
both method m1(String) in C and method m1(StringBuffer) in C match
*/
c.m1(null);
System.out.println("-------------- For D class -------------");
D d = new D();
d.m1(10,10.5f);
d.m1(10.5f,10);
//Special
/*
error: reference to m1 is ambiguous
d.m1(10,10);
^
both method m1(int,float) in D and method m1(float,int) in D match
*/
d.m1(10,10);
/*
error: no suitable method found for m1(float,float)
d.m1(10.5f,10.5f);
^
method D.m1(int,float) is not applicable
(argument mismatch; possible lossy conversion from float to int)
method D.m1(float,int) is not applicable
(argument mismatch; possible lossy conversion from float to int)
*/
d.m1(10.5f,10.5f);
System.out.println("-------------- For E class -------------");
E e = new E();
e.m1(); // var arg method
e.m1(10,20); // var arg method e.m1(10);// general method since this concept is old op: int
System.out.println("-------------- For F class -------------");
F f = new F();
Parent p = new Parent();
f.m1(p); // parent
Child ch = new Child();
f.m1(ch); // child
// in overloading method resolution is always based on referenced type
Parent p1 = new Child();
f.m1(p1); //Parent
}
}
class A{
public void m1(String p1){
System.out.println("string obj");
}
public void m1(Object p1){
System.out.println("Object type obj");
}
}
class B{
public void m1(int p1){
System.out.println("int obj");
}
public void m1(Object p1){
System.out.println("Object type obj");
}
}
class C{
public void m1(String p1){
System.out.println("string obj");
}
public void m1(Object p1){
System.out.println("Object type obj");
}
public void m1(StringBuffer p1){
System.out.println("StringBuffer type obj");
}
}
class D{
public void m1(int p1,float p2){
System.out.println("int p1,float p2");
}
public void m1(float p1,int p2){
System.out.println("float p1,int p2");
}
}
class E{
public void m1(int p1){
System.out.println("int");
}
public void m1(int... p1){
System.out.println("var arg method");
}
}
/* ----------------------------------------- */
class Parent{
}
class Child extends Parent{
}
class F{
public void m1(Parent p1){
System.out.println("Parent");
}
public void m1(Child p1){
System.out.println("Child");
}
}
Comments
Post a Comment