1 / 6

Polimorphism

Polimorphism. POLYMORPHISM. What is polymorphism ? The feature that allows an object of a certain class to be seen also as an object of another Why do we need it ? Sometimes it makes programming easier Example We are dealing with different kinds of animals in a farm

kress
Download Presentation

Polimorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Polimorphism

  2. POLYMORPHISM • What is polymorphism ? • The feature that allows an object of a certain class to be seen also as an object of another • Why do we need it ? • Sometimes it makes programming easier • Example • We are dealing with different kinds of animals in a farm • We want to do the same things with them but they will react differently

  3. Dealingwithanimals • Counthowmanylegswehave in total in a list of animals intlegs(Animal[] x) { //x isanarray of Animal objects intnlegs = 0; for(int i = 1; i < x.length; i++) if (x[i].name,equals(“Cat”)) nlegs += 4; elseif (x[i].name.equals(“Cock”)) nlegs+= 2; elseif (. . . • Whathappensifwehave a new type of animal ?

  4. ApplyingPolymorphism • Letshaveeach animal defined as a differntclassbutall of themhave a methodcalledlegs() ClassCockextends Animal { . . . publicintlegs() { return 2; } . . . } intlegs(Animal[] x) { int n = 0; for(int i = 1; i < x.length; i++) n += x[i].legs(); return n; }

  5. Resources in Java • In java we can haveanobject of class “Pig” and anobject of class “Cock” which can beealsoseen as of class “Animal” (thushavingthesamemthodsbutreactingdiffernt to it) by: • Simple inheritance: Animal declares nlegs(), Pig and Cockoverwriteit • Abstractclass: Animal declares nlegs() as abstract, Pig and Cockoverwriteit • No differencewith 1 whenwritingPig and Cock, • Preventusersfromcreating Animal objects • Interface: instead of writingclass Animal wewrite interface Animal which declares methodnlegs(), Pig and Cockimplementit

  6. Letsseethe case of figures withall 3 • Wewillhave a base classGfigure • Fromthisonewewill “derive” Grectangle, Gcircle and Gtriangle • Theyhave a originpoint (x,y) and can be drawnover a Consoleobject • Consoleclasscreatedforthis lectura to easydrawings • Wewant to easilyresize and move figures ontheConsole • Folder figures simple inheritanceimplementsthiswith simple inheritance • Folder figures abstractclassesimplementsthiswithanabstractclass • Folder figures interface implementsthiswithan interface

More Related