-2
class P
{
    void speak()
    {
        print ("Hello P");
    }
}

class C extends P
{
    void speak(String s)
    {
        print ("Hello C");
    }
}

// And then somewhere…

public static void main()
{
    P ref = new C();
    ref.speak();
}

Please do correct where I'm wrong. When the code compiles, the compiler first checks the class that ref belongs to see if it contains a speak (). Once confirmed, it sees that the speak() is virtual so it won't statically bind it. During runtime, as per logic the Jvm would see that ref points to a C class object so it should call speak () of C class but instead it follows the rule of the statically typed and calls the method of P class. How does the complier know that these 2 methods are overloaded when they are in different classes and therefore have to be statically binded?

Lucifer G
  • 1
  • 4

2 Answers2

0

What you are trying to do is not not overloading. The overloading of a function is when you have multiple functions of the same name but different types or number of parameters within the same class. Take a look at this link for examples What you want to do is overriding which requires class C to inherit from class P, In otherwords Class C is a subclass of class P. You then need to implement a version of the speak method which you have done. Example of inhertiance

Simple replace the declaration of Class C with Class C extends P

Jonathan
  • 35
  • 5
0

Overloading is when a class has multiple methods with the same name but different parameters. With the code below, calling A.speak() will trigger the first method, while calling A.speak(someString) will do the second method.

class A {

    void speak(){
        speak("Hello!")
    }
    
    void speak(String s){
        System.out.println(s);
    }
}

Overriding is when a child class redefines the implementation of an existing method in the child's superclass.

//Superclass A
class A {
    

    void speak(String s){
        System.out.println("A says : " + s);
    }
}

//Child class B
class B extends A{

    //Class B redefines what speak() does
    void speak(String s){
        System.out.println("B says : " + s);
    }
}

Based on your code, I believe what you want is to use Overriding, having class C redefine the implementation of speak(). If you want the speak() method to be overriden the method's parameters must match.

I've fixed your code below:

class P
{
    void speak()
    {
        print ("Hello P");
    }
}

class C extends P {
    void speak()
    {
        print ("Hello C");
    }
}


public static void main()
{
    P ref = new C();
    ref.speak();
}
Ewan Brown
  • 640
  • 3
  • 12
  • I understood your answer but going by the orignal question, can you tell the process of how it is decided that what type of binding will take place? I know that for overloading it's static but what does the compiler do when it comes across the line ref.speak(). Does it check what class speak () belongs to or does it do something else? – Lucifer G Jan 28 '22 at 19:13