Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with polymorphism inside a class [closed]

In languages with dynamic typing, the use of polymorphism may trigger errors on a super-class.

I will try to explain my question with a simple example: Supposing a language with dynamic typing (like ECMAScript) and the following class structure:

diagram

class A{
    private idA;
    public A(){
        idA=0;
    }
    public foo(){
        update();
        if (this.idA!=3) Throws new Exception(" What is happening? ");
    }
    private update(){
        this.idA = 3;
    }
}
class B extends A{
    private idB;
    public B(){
        super();
        idB=0;
    }
    public foo(){
        super.foo();
        // Any operation between A::update and B::update()
        if (this.idB!=0) Throws new Exception("hmmm, that could not happend!");
        update();
    }
    private update(){
        this.idB = 5;
    }
}

In this very simple example, when i create an object of the class B, B::foo() call the parent A::foo(), which call "update". The object is an instance of B, so the "update" functions called is B::update, after that, in B::foo, the update function is again called (B::update). The final result is that A::update is never called, and idA still 0.

The class A work correctly when used alone, but after to extend it with B, the function foo() fail.

What is the correct solution this problem:

1) Force the class A to call A::update , that mean an ugly code every call to his own function (protect the super-class):

A::foo(){
    A::update();
    if (this.idA!=3) Throws new Exception(" What is happening? "); 
}

2) B::update is an extension of A::update, so B::update must call itself the parent function (prepare the sub-class, and deal with problems):

B::foo(){
    super.foo();
    ... // Any operation that must be performed between A::update and B::update
}
B::update(){
    super.update();
    this.idB = 5;
}

But in this case is the A::foo which call update, not the B::foo. That mean others problems.

3) Any other solution.

As a summary:

How to protect the super-class code against polymorphism?

  • Add protections into the super-class.
  • Deal with these problem creating the child-class
  • The language must do that! (do not know if it is possible with dynamically typed languages)

I am looking for a very theoretical /canonical solution to this question.

EDITED: to take the problem out of the constructor and clarify some points.

like image 739
Adrian Maire Avatar asked Aug 05 '13 17:08

Adrian Maire


2 Answers

It's generally considered a very bad practice to call instance methods, and especially virtual instance methods from within a constructor exactly for this reason (but also for the reason that the object isn't done being "initialized" yet).

3) Any other solution.

Doc, it hurts when I do this.

Then don't do that!

Seriously, if you need to set IdA in the constructor of A, don't do it by calling update, do it by explicitly setting the value of IdA in the constructor for A.

like image 63
jason Avatar answered Nov 02 '22 00:11

jason


The base class should protect itself from harmful overrides. In keeping with the open/close principle, it should be open to extension but closed to modification. Overriding update is a harmful modification of the base class's intended behaviour. In your example, there is no benefit in overriding update because both A::update and B::update are private methods that deal with private variables. There isn't even an expectation that they should be executed together judging by your exception in B::foo. If B::update was named differently, there wouldn't be anything wrong with your implementation. It would probably be OK anyway: since no language I know of will let you override a private method, B::update could hide A::update rather than overriding it.

Depending on the language, you can limit which methods can be overridden in different ways. Some languages require an indicator (a keyword or attribute usually) that a method can be overridden, others to show that it can't. Private methods are generally not overridable, but not all languages have access modifiers at all, and everything is effectively public. In this case you would have to use some kind of convention as suggested by @PoByBolek.

tl;dr: Children have no business with their parents' privates.

like image 3
Patrick Stephansen Avatar answered Nov 02 '22 00:11

Patrick Stephansen