1

Here I have this question. Let's say code 1 is Java with runtime polymorphism and code 2 is Java without runtime polymorphism.

Code 1:

class A {
    void run() { System.out.println("A is running"); }
}

class B extends A{
    void run(){ System.out.println("B is running with A!");}

    public static void main(String args[]){

        A a = new B(); //referenced to class A
        a.run();

    }
}

Code 2:

class A {
    void run(){System.out.println("A is running");}
}

class B extends A{
    void run(){ System.out.println("B is running with A!");}

    public static void main(String args[]){
        B a = new B(); //referenced to the same constructor class
        a.run();
    }
}

Even though these two codes give the exact same results, it is well known that runtime polymorphism is really important in OOP. I need an explanation/reason of using code 1 instead of using code 2.

3
  • run-time polymorphism is really helpful where multiple different objet inherits from the same class and you got to store them in a single array Commented Oct 27, 2016 at 9:29
  • 1
    Your example doesn't motivate the usage of runtime polymorphism. Commented Oct 27, 2016 at 9:30
  • Possible duplicate of stackoverflow.com/questions/20783266/… Commented Oct 27, 2016 at 9:34

2 Answers 2

1

Take advantage of polymorphism is not needed in OOP in every cases.
Polymorphism is a mechanism which brings flexibility when flexibility is needed.

For example, in your case, writing :

B a = new B();

or

A a = new B()

depends if you need and have interest to reference the base class rather than the subclass as declared type for your variable.

Referencing the base class (here A) allows many things among these (it is not exhaustive but frequent use cases) :

  • masking the implementation (via factory or injection dependency). In this way, you can change the implementation (returning a C or D which extends A) without breaking calls of client code. In your code, you don't perform that but in real applications you can have this need.
  • If you have processings common to A and B classes, you may propose a method which takes as parameter a instance of type A. Example : public void doProcess(A instance)
    In this case, if you declare B a = new B(), the code will not compile if you pass this instance to that method as semantically, a B instance is a A but a A instance is not necessarily a B

Often, we can read that declaring the interface rather that the implementation is a good practice. In fact, it is in most of cases but in some other cases, it is not. Just one example, if you need to use a TreeMap, you will not declare the interface Map because the behavior of TreeMap is very specific and have special requirements, so you may want to stress on. When you instantiate a classic HashMap, generally, you don't care precising HashMap as declared type since its interface is enough. You have no interest to create a stronger coupling with the implementation.

Sign up to request clarification or add additional context in comments.

2 Comments

This helps a lot! Thanks
You are welcome :) You are right to wonder about the subject since polymorphism is one of key of OOP languages :) If I addressed your question, feel free to mark my answer as accepted.
0

Let's take this case :
You have multiple classes inheriting from the same base class, you want to call their shared function run() on all of them, even though they are different classes.
You'll have to do this:

interface A{
    void run();
}

class B implements A{
void run(){ System.out.println("B is running with A!");}

class C implements A{
void run(){ System.out.println("C is running with A!");}

public static void main(String args[]){
    Array<A> objects = new Array<A>();
    objects.put(new B()); //Object are stored as instances of "A"
    objects.put(new C());
    objects.put(new B());
    for (A obj : objects)
        obj.run();//Calls everything
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.