Java polymorphism through injection at runtime -


i hear in java can achieve polymorphism through injection @ runtime. can please show simple example of how done? search online can't find anything: maybe searching wrong. know polymorphism through interface , and extension such as

class myclass extends parent implements naming 

in such case achieving polymorphism twice: myclass @ once of type parent , naming. don't how injection works. idea not using @override keyword during injection. hope question clear. thanks.

so end result here, per understanding, change behavior of method through injection instead of @override during development.

so know polymorphism through interface , and extension such as

class myclass extends parent implements naming

this known inhertiance , not polymorphism. myclassis parent , myclass naming. being said, inheritance allows achive polymorphism.

consider class other thanmyclass implements naming :

class someotherclass implements naming {      @override      public void somemethoddefinedintheinterface() {       }  } 

now consider method takes naming argument somewhere in code base :

public void dosomething(naming naming) {      naming.somemethoddefinedintheinterface(); } 

the dosomething method can passed instance of class implements naming. both following calls valid :

dosomething(new myclass());//1 dosomething(new someotherclass());//2 

observe how can call dosomething different parameters. @ runtime, first call call somemethoddefinedintheinterface myclass , second call call somemethoddefinedintheinterface someotherclass. known runtime-polymorphism can achieved through inheritance.

but don't how injection works. idea not using @override keyword during injection

that's true in broader sense. inject class, class should ideally favor composition on inheritance. see this answer job in explaining reason favoring composition on inheritance.

to extend above example answer, let's modify dosomething method follows :

  public class classhasanaming {       private naming naming;        public classhasanaming(naming naming) {           this.naming = naming;       }        public void dosomething() {            naming.somemethoddefinedintheinterface();       }   } 

observe how classhasanaming has-a naming dependency can injected outside world :

classhasanaming callmyclass = new classhasanaming(new myclass()); callmyclass.dosomething(); 

if use factory pattern, can chose subclass gets instantiated @ runtime.

do think have done did above using inheritance?

 public class classisanaming implements naming {     public void dosomething() {         somemethoddefinedintheinterface();     }      @override     public void somemethoddefinedintheinterface() {         //....     }  } 

the answer no. classisanaming bound single implementation of somemethoddefinedintheinterface method @ compile time itself. `


Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. I hear in java can accomplish polymorphism through infusion @ runtime. can please show straightforward case of how done? search online can't discover anything: perhaps looking through wrong. know polymorphism through interface.

    Regards
    Ali kazmi

    ReplyDelete

Post a Comment

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -