how to achieve dynamic polymorphism without extending a class

Decorator design pattern that exploits encapsulation is what you’re looking for.

Polymorphism through inheritance:

class Cat {
  void meow() {
    // meow...
  }
}
class Lion extends Cat {
}

Polymorphism through encapsulation (Decorator pattern):

interface Cat {
  void meow();      
}
class Lion implements Cat {
  private Cat cat;
  void meow() {
    this.cat.meow();
  }
}

ps. More about decorators: http://www.yegor256.com/2015/02/26/composable-decorators.html

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)