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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
wr93_
  • 130
  • 1
  • 1
  • 13
  • 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 – Treycos Oct 27 '16 at 09:29
  • 1
    Your example doesn't motivate the usage of runtime polymorphism. – Marko Topolnik Oct 27 '16 at 09:30
  • Possible duplicate of http://stackoverflow.com/questions/20783266/what-is-the-difference-between-dynamic-and-static-polymorphism-in-java – Nurjan Oct 27 '16 at 09:34

2 Answers2

1

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
}
Treycos
  • 7,373
  • 3
  • 24
  • 47
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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • This helps a lot! Thanks – wr93_ Oct 27 '16 at 13:17
  • 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. – davidxxx Oct 27 '16 at 13:31