-2

Question (What my code is trying to achieve in terms of interfaces? (Compile - time / run - time) polymorphism ?

Consider below code as an example

interface RemoteControl {
    void turnOn();
    void turnOn(int channel);
}

class TV implements RemoteControl {
    public void turnOn() {
        System.out.println("TV is turned on");
    }

    public void turnOn(int channel) {
        System.out.println("TV is turned on. Channel: " + channel);
    }
}


public class UserCode {
    public static void main(String[] args) {
        RemoteControl tvRemote = new TV(); 

        tvRemote.turnOn(); 
        tvRemote.turnOn(5); 
    }
}

Now in the above code can you tell me what type of polymorphism its trying to achieve.?

So below part is decided at compile-time or run-time ( as there are not inherited classed but in terms of interface ?

RemoteControl tvRemote = new TV();
tvRemote.turnOn(); 
tvRemote.turnOn(5); 
  • 1
    What's your definition of "static polymorphism". It seems popular to define it (for Java) as method overloading, but that is not polymorphism -- each method is a different method, and the fact that they have the same names is irrelevant, although it means that we have to define how the compiler selects the right method at compile time based on the types of the parameters. – tgdavies Aug 02 '23 at 23:31
  • @tgdavies How? what i know and read is that methods with Same name and different parameters is method-overloading. And if method overloading occurs in a same class then it is decided during compilation-time as which to select and hence we can also refer it as a static polymorphism – Ash Kecthum Aug 02 '23 at 23:43
  • 1
    How, because that is how run-time polymorphism is defined and how it works. The compiler cannot know in advance what actual instance is calling the method at compile time when it is an interface method. Period. – Hovercraft Full Of Eels Aug 03 '23 at 00:46
  • @HovercraftFullOfEels no means i am not asking about run - time polymorphism here (How) means he said (the fact that they have the same names is irrelevant) so in terms of that i said "How". He was first saying about method - overloading but said its not polymorphism – Ash Kecthum Aug 03 '23 at 00:58
  • That doesn't really make sense. Can you re-explain what you're asking about, because your code is *showing* run-time polymorphism involving a class that inherits from an interface, including "the part below". – Hovercraft Full Of Eels Aug 03 '23 at 01:00
  • 1
    Regarding your question's edit, the answer is the same: your code is showing run-time polymorphism. It is also showing method overloading due to parameter changes. – Hovercraft Full Of Eels Aug 03 '23 at 01:04
  • @HovercraftFullOfEels Okay let me be clear about one thing. for eg TV obj = new TV() suppose TV class include method overloading then its determined during compile-time right ?. Now as per above code RemoteControl tvRemote = new TV(); tvRemote.turnOn() here you are saying its achieving run-time polymorphsim (so basically in terms of interfaces too we say it as a run-time polymorphism as i created reference of interface with TV object? ). Maybe i should have said like TV obj = new TV() and than call its methods like obj.turnOn() ( this is compiled time right ? ) – Ash Kecthum Aug 03 '23 at 01:11
  • 1
    So is your question: "Is different byte code produced when the compile-time type of a reference is a concrete class rather than an interface?" I'm not clear on what you are trying to understand about the difference. – tgdavies Aug 03 '23 at 01:30

1 Answers1

-2

The Remote Control interface's methods are compiled polymorphism, whereas the TV's overridden methods are runtime polymorphism.

You can read more at this Geeks For Geeks Article, but an easy way to tell which methods are runtime and which are compiled would be if there was inheritance. If the methods are inherited and overridden, they would by runtime polymorphism.

Jordan
  • 58
  • 6
  • Thank you for your response. But when i posted with different doubt but this was the same example. Some other person said it my code is achieving static / compile-time polymorphsim. So i am bit getting confused with all different answers. Yeah it would be easy in terms of inheritance but i want to get clear in terms of interfaces – Ash Kecthum Aug 03 '23 at 00:29
  • 1
    Sorry, but this answer is nonsense. @AshKecthum, do not follow this advice, please. – Hovercraft Full Of Eels Aug 03 '23 at 00:47
  • @HovercraftFullOfEels God thank you for mentioning this. Its been 6 hours i am just learning about interfaces and polymorphism still – Ash Kecthum Aug 03 '23 at 00:54