Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class A
{
public void hello()
{
System.out.println("hello in A");
}
}
class B extends A
{
public void hello()
{
System.out.println("hello in B");
}
}
class C
{
public static void main(String... args)
{
A a = new B();
a.hello();
}
}



output of the above code is ---
hello in B



but from internet i read-------------------------
public class StaticBindingTest {
 
    public static void main(String args[])  {
       Collection c = new HashSet();
       StaticBindingTest et = new StaticBindingTest();
       et.sort(c);
     
    }
   
    //overloaded method takes Collection argument
    public Collection sort(Collection c){
        System.out.println("Inside Collection sort method");
        return c;
    }
 
   
   //another overloaded method which takes HashSet argument which is sub class
    public Collection sort(HashSet hs){
        System.out.println("Inside HashSet sort method");
        return hs;
    }
     
}

Output:
Inside Collection sort method

hows its possible i think in second case output will be 
"Inside HashSet sort method"
please correct me if i'm wrong
Posted

1 solution

The output would be "Inside Collection sort method".

This is because the static type of Collection is c (and not HashSet).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900