EliiTryToLearn
EliiTryToLearn

Reputation: 125

Confusion on Polymorphism java

I was reading up on Polymorphism and after some searching I was confused about about the difference of compile-time and run-time polymorphism.

Lets say i have a class ABC and one other class that extends ABC, class DEF like so:

class ABC{
    public void myMethod(){
    System.out.println("Overridden Method");
   }
}

public class DEF extends ABC{
    public void myMethod(){
    System.out.println("Overriding Method");
    }
}

public static void main(String args[]){
   ABC obj = new DEF();
   obj.myMethod();
   }
}

If we want to be able to replace a class with another class, which performs the same task but in a different way without having too recompile the class that calls them, Is method overriding the way or method overloading?

Upvotes: 3

Views: 162

Answers (2)

Victor
Victor

Reputation: 3978

I guess you are asking two things

1) I was reading up on Polymorphism and after some searching I was confused about about the difference of compile-time and run-time polymorphism.

Let's separate the waters here a little bit...

At compile time the compilator validates and transform the code into bytecodes. The static methods calls are resolved during this phase so you know every time the static method is invoked the same and sole implementation will take place.

run-time polymorphism is what happen at execution to decide what actual implementation of a given method to chosse. Considering that each class in the hierchary provides one implementation of this "polymorphic" method. Behind the curtains the compiler do a lot of work to archieve this, using something known as late binding mechanism.

2) If we want to be able to replace a class with another class, which performs the same task but in a different way without having to recompile the class that calls them, Is method overriding the way or method overloading?

You don't actually say that replaces, inteads talks about extends, that is the thing that your are doing when a class extends another. And the implementations of the a method in parent class can be overwritten in a subclass. Overload, has nothing to do with hierarchies, is to write the same method with diferents set of parameters.

Upvotes: 3

Max
Max

Reputation: 1705

Overloading is when you create multiple classes with the same method name, but different params. If a subclass has the exact same method name and params, it really ends up being overwriting, whether your put @overwrite on top or not. It´s literally the same exact thing and either way you change the outcome of the method, even if you use the base/super - class as a type.

This could be helpful When do you use Java's @Override annotation and why?

You´ll make your compiler complain that you are not actually overwriting a function if you mistype one of the two and nothing ends up being overwritten and you´ll make your code easier to read. It won´t affect the outcome, but you still should use it for those reasons.

Upvotes: 1

Related Questions