Kappale 9 Olioohjelmointi periytyminen Sislt 9 1 9

  • Slides: 51
Download presentation
Kappale 9 - Olio-ohjelmointi: periytyminen Sisältö 9. 1 9. 2 9. 3 9. 4

Kappale 9 - Olio-ohjelmointi: periytyminen Sisältö 9. 1 9. 2 9. 3 9. 4 9. 5 9. 6 9. 7 Johdanto Yliluokat ja aliluokat protected –avainsana Yliluokkien ja aliluokkien välinen suhde Case Study: Kolmitasoinen periytymishierarkia Muodostimet ja lopettimet aliluokissa Periytyminen ohjelmistosuunnittelussa Lähde: Deitel & Deitel, Java How to Program –kirjan kalvot ict 02 d 1

9. 1 Johdanto • Periytyminen – Luodaan olemassa olevan luokan pohjalta (periytetään) uusi luokka

9. 1 Johdanto • Periytyminen – Luodaan olemassa olevan luokan pohjalta (periytetään) uusi luokka • Saadaan käyttöön vanhan luokan (yliluokan) attribuutit ja metodit. • Täydennetään uutta luokkaa tarvittavilla uusilla ominaisuuksilla ja metodeilla. – Aliluokka laajentaa yliluokkaa (subclass extends superclass) • Aliluokka – Erikoistunut tietynlaisten olioiden käsittelyyn – Perii yliluokan ominaisuudet, voidaan muokata tarpeisiin sopivaksi – Periytyminen lisää koodin uudelleenkäyttöä. ict 02 d 2

9. 1 Johdanto • Luokkahierarkia – Suora yliluokka • Periytetty eksplisiittisesti (yksi taso ylöspäin

9. 1 Johdanto • Luokkahierarkia – Suora yliluokka • Periytetty eksplisiittisesti (yksi taso ylöspäin hierarkiassa). – Epäsuora yliluokka • Periytetty kaksi tai useampia tasoja ylempää hierarkiassa. – Yksittäisperiytyminen • Luokka periytetään yhdestä yliluokasta. – Moniperiytyminen • Luokka periytetään useammasta kuin yhdestä yliluokasta. – Java ei tue moniperiytymistä! ict 02 d 3

9. 1 Johdanto • Periytyminen vai koostuminen: “olla” vs. “omistaa” – “olla” periytyminen •

9. 1 Johdanto • Periytyminen vai koostuminen: “olla” vs. “omistaa” – “olla” periytyminen • Aliluokan oliota käsitellään kuten yliluokan oliota. • Esimerkki: Auto on kulkuneuvo. – Kulkuneuvon ominaisuudet ovat myös auton ominaisuuksia. – “omistaa” koostuminen • Oliolla on muita olioita attribuutteina. • Esimerkki: Autossa on ohjauspyörä. ict 02 d 4

9. 2 Yliluokat ja aliluokat • Yliluokat ja aliluokat – Luokan olio on myös

9. 2 Yliluokat ja aliluokat • Yliluokat ja aliluokat – Luokan olio on myös jonkin toisen luokan olio. • Esimerkki: Suorakaide on nelikulmio. – Luokka Suorakaide periytyy luokasta Nelikulmio – Nelikulmio: yliluokka – Suorakaide: aliluokka – Yliluokka edustaa yleensä suurempaa olioiden joukkoa kuin aliluokka. • Esimerkki: – yliluokka: Kulkuneuvo • Autot, veneet, polkupyörät, jne. – aliluokka: Auto • Pienempi, spesifisempi, kulkuneuvojen joukon osajoukko ict 02 d 5

9. 2 Aliluokat ja yliluokat • Esimerkkejä periytymisestä ict 02 d 6

9. 2 Aliluokat ja yliluokat • Esimerkkejä periytymisestä ict 02 d 6

9. 2 Aliluokat ja yliluokat • Periytymisen hierarkia – Periytymissuhteet: puumainen hierarkiarakenne – Jokaisesta

9. 2 Aliluokat ja yliluokat • Periytymisen hierarkia – Periytymissuhteet: puumainen hierarkiarakenne – Jokaisesta luokasta tulee • yliluokka – Tarjoaa attribuutteja/metodeja muille luokille. TAI • aliluokka – Perii attribuutteja/metodeja muilta luokilta. ict 02 d 7

Periytyminen UML-luokkakaaviossa • UML-luokkakaaviossa periytymistä kuvaa alla näkyvä nuolityyppi (generalization). • Yliluokasta perittyjä attribuutteja

Periytyminen UML-luokkakaaviossa • UML-luokkakaaviossa periytymistä kuvaa alla näkyvä nuolityyppi (generalization). • Yliluokasta perittyjä attribuutteja ja metodeja ei tarvitse toistaa aliluokan kuvauksessa. ict 02 d 8

Kaavio. 9. 2 Periytymishierarkia UML-kaaviona yliopiston Yhteiso. Jasenille. ict 02 d 9

Kaavio. 9. 2 Periytymishierarkia UML-kaaviona yliopiston Yhteiso. Jasenille. ict 02 d 9

Kaavio 9. 3 Muotojen periytymishierarkia UML-luokkakaaviona. ict 02 d 10

Kaavio 9. 3 Muotojen periytymishierarkia UML-luokkakaaviona. ict 02 d 10

9. 3 protected –näkyvyysalue • protected -määre – public- ja private-määreiden välimuoto – protected

9. 3 protected –näkyvyysalue • protected -määre – public- ja private-määreiden välimuoto – protected –tyyppisiin komponentteihin on pääsy: • ko luokasta, • ko luokan aliluokista, • muista saman pakkauksen luokista. – Yliluokan komponentin käyttäminen aliluokasta: • vakiosana super ja piste (. ). ict 02 d 11

9. 4 Yliluokan ja aliluokan välinen suhde • Yliluokan ja aliluokan välinen suhde –

9. 4 Yliluokan ja aliluokan välinen suhde • Yliluokan ja aliluokan välinen suhde – Esimerkki: Piste/ympyrä periytymishierarkia • Piste – x-y koordinaattipari • Ympyrä – x-y koordinaattipari – säde ict 02 d 12

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 13 // Fig. 9. 4: Point. java // Point class declaration represents x-y coordinate x- jaany-koordinaatit ovatpair. private-attribuutteja. Click to edit Master title style public class Point { private int x; // x part of coordinate pair private int y; // y part of coordinate pair Implisiittinen kutsu // no-argument constructor Click to edit Master text styles Object-luokasta public level Point() Second { peritylle Third level // implicit call to Object constructor occurs here muodostimelle. Fourth level } Fifth level // constructor public Point( int x. Value, int y. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation } // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } Ohjelmointi 2 Point. java Rivit 5 -6 x- ja ykoordinaatit määritelty private attribuutteja. Rivi 11 Implisiittinen kutsu Objectluokasta peritylle muodostimelle. 13

28 29 30 31 32 33 34 35 36 37 38 39 40 41

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 14 // return x from coordinate pair public int get. X() { return x; } Click to edit Master title style // set y in coordinate pair publictovoid inttext y. Value ) Click editset. Y( Master styles { Second level y = y. Value; // no need for validation Third level } Fourth level Fifth level // return y from coordinate pair public int get. Y() { return y; } Point. java Korvataan Object-luokan metodi to. String. // return String representation of Point object public String to. String() { return "[" + x + ", " + y + "]"; } Rivit 47 -50 Korvataan Object -luokan metodi to. String. } // end class Point Ohjelmointi 2 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 15 // Fig. 9. 5: Point. Test. java // Testing class Point. import javax. swing. JOption. Pane; Click to edit Master title style public class Point. Test { Luodaan Point-olio. public static void main( String[] args ) { Click to edit Master text styles Point point = new Point( 72, 115 ); // create Point object Second level Third level // get point coordinates Fourth level String point. get. X() + Fifth leveloutput = "X coordinate is " +Muutetaan pisteen "n. Y coordinate is " + point. get. Y(); x- ja ykoordinaattien arvoja. point. set. X( 10 ); point. set. Y( 20 ); // set x-coordinate // set y-coordinate // get String representation of new point value output += "nn. The new location of point is " + point; JOption. Pane. show. Message. Dialog( null, output ); // display output System. exit( 0 ); Point. Test. java Rivi 9 Luodaan Point-olio Rivit 15 -16 Kutsutaan Muutetaan pisteen implisiittisesti Pointxja y-koordinaattien -luokan to. String– arvoja. metodia. Rivi 19 Kutsutaan implisiittisesti to. String –metodia. } // end main } // end class Point. Test Ohjelmointi 2 15

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 16 // Fig. 9. 6: Circle. java // Circle class contains x-y coordinate pair and radius. x-ja y-koordinaatit ja Click to edit Master title style radius ovat private- public class Circle { private int x; private int y; private double radius; // x-coordinate of Circle's center // y-coordinate of Circle's center attribuutteja. // Circle's radius Click to edit Master text styles // no-argument constructor Second level publiclevel Circle() Third { Fourth level // implicit call to Object constructor occurs here Fifth level } Circle. java // constructor public Circle( int x. Value, int y. Value, double radius. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation set. Radius( radius. Value ); } // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } Ohjelmointi 2 Rivit 5 -7 x- ja y- koordinaatit ja säde private attribuutteja. Rivit 25 -28 Huomaa Pointluokan kanssa yhtenevä koodi. Huomaa Point-luokan kanssa yhtenevä koodi. 16

30 31 32 33 34 35 36 37 38 39 40 41 42 43

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 17 // return x from coordinate pair public int get. X() { return x; } Click to edit Master title style // set y in coordinate pair public void set. Y( int y. Value ) { Click to edit Master text styles y =level y. Value; // no need for validation Second } Third level Huomaa Point-luokan kanssa yhtenevä koodi. Circle. java Rivit 31 -47 Huomaa Pointluokan kanssa yhtenevä koodi. Fourth level // return y from coordinate pair Fifth level public int get. Y() { return y; } // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } // return radius public double get. Radius() { return radius; } Muuttujalle radius täytyy antaa positiivinen arvo. Ohjelmointi 2 Rivi 51 Varmista että radiusmuuttujalle annetaan positiivinen arvo. 17

60 61 62 63 64 65 66 67 68 69 70 71 72 73

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 18 // calculate and return diameter public double get. Diameter() { return 2 * radius; } Click to edit Master title style // calculate and return circumference publictodouble get. Circumference() Click edit Master text styles { Second level return Third level. Math. PI * get. Diameter(); } Fourth level Fifth level // calculate and return area public double get. Area() { return Math. PI * radius; } Circle. java // return String representation of Circle object public String to. String() { return "Center = [" + x + ", " + y + "]; Radius = " + radius; } } // end class Circle Ohjelmointi 2 18

19 1 2 3 4 5 6 7 8 9 10 11 12 13

19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 7: Circle. Test. java // Testing class Circle. import java. text. Decimal. Format; import javax. swing. JOption. Pane; Click to edit Master title style Luodaan Circle-olio. public class Circle. Test { Click to edit Master text String[] styles args ) public static void main( Second level { Third Circle level circle = new Circle( 37, 43, 2. 5 ); // create Circle object Fourth level get Circle's initial x-y coordinates and radius Fifth // level String output = "X coordinate is " + circle. get. X() + "n. Y coordinate is " + circle. get. Y() + "n. Radius is " + circle. get. Radius(); circle. set. X( 35 ); circle. set. Y( 20 ); circle. set. Radius( 4. 25 ); // set new x-coordinate // set new y-coordinate // set new radius // get String representation of output += "nn. The new location circle. to. String(); Ekspilisiittinen kutsu new to. String-metodille circle value Käytetään set-metodeja and radius of circle aren" + arvon private-attribuutin muuttamiseen. Circle. Test. java Rivi 10 Luodaan Circle-olio Rivit 17 -19 Käytetään set-metodeja muuttamaan private attribuutin arvoa. Rivi 23 Kutsutaan eksplisiittisesti circlen to. String -metodia // format floating-point values with 2 digits of precision Decimal. Format two. Digits = new Decimal. Format( "0. 00" ); Ohjelmointi 2 19

20 28 29 30 31 32 33 34 35 36 37 38 39 40

20 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 // get Circle's diameter output += "n. Diameter is " + two. Digits. format( circle. get. Diameter() ); Click to edit Master title style // get Circle's circumference output += "n. Circumference is " + ); Click totwo. Digits. format( edit Master text circle. get. Circumference() styles Käytetään get-metodeja halkaisija, kehä ja pintaalan tiedusteluun. Second level get Circle's area Third//level output Fourth level+= "n. Area is " + two. Digits. format( circle. get. Area() ); Fifth level JOption. Pane. show. Message. Dialog( null, output ); // display output Circle. Test. java Rivit 29 -37 System. exit( 0 ); } // end main Käytetään getmetodeja halkaisijan, kehän ja pinta-alan tiedusteluun. } // end class Circle. Test Ohjelmointi 2 20

21 1 2 3 4 5 6 7 8 9 10 11 12 13

21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 8: Circle 2. java // Circle 2 class inherits from Point. Luokka Circle 2 on luokan Point aliluokka. Click edit Master title. Point style{ public to class Circle 2 extends private double radius; // Circle 2's radius private attribuutti // no-argument constructor radius. Click to edit Master text styles public Circle 2() Second level { implicit call to Point constructor occurs here Third // level } Fourth level Fifth level // constructor Yritys käsitellä Point-yliluokan public Circle 2( int x. Value, int y. Value, double radius. Value ) private-attribuutteja x and y { aiheuttaa // implicit call to Point constructor occurssyntaksivirheen. here x = x. Value; // not allowed: x private in Point y = y. Value; // not allowed: y private in Point set. Radius( radius. Value ); } // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } Ohjelmointi 2 Circle 2. java Rivi 4 Luokka Circle 2 on luokan Point aliluokka. Rivi 5 private -attribuutti radius. Rivit 17 -18 Yritys käsitellä yliluokan Point private-attribuutteja x and y aiheuttaa syntaksivirheen. 21

22 34 35 36 37 38 39 40 41 42 43 44 45 46

22 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 // calculate and return diameter public double get. Diameter() { return 2 * radius; } Click to edit Master title style // calculate and return circumference Click to edit Master text styles public double get. Circumference() Second level { Third return level Math. PI * get. Diameter(); } Fourth level Circle 2. java Fifth level // calculate and return area public double get. Area() { return Math. PI * radius; } Rivi 56 Yritys käsitellä yliluokan Point Yritys käsitellä Point- private object attribuutteja x and y yliluokan privateaiheuttaa attribuutteja x and y syntaksivirheen. privateaiheuttaa in Point syntaksivirheen. // return String representation of Circle public String to. String() { // use of x and y not allowed: x and y return "Center = [" + x + ", " + y + "]; Radius = " + radius; } } // end class Circle 2 Ohjelmointi 2 22

23 Click to edit Master title style Circle 2. java: 17: x has private

23 Click to edit Master title style Circle 2. java: 17: x has private access in Point x = x. Value; // not allowed: x private in Point ^ Circle 2. java: 18: y has private access in Point Click to edit // Master text styles y = y. Value; not allowed: y private in Point Second level ^ Third level x has private access in Point Circle 2. java: 56: Fourth return level "Center = [" + x + ", " + y + "]; Radius = " + radius; Fifth level ^ Circle 2. java: 56: y has private access in Point return "Center = [" + x + ", " + y + "]; Radius = " + radius; ^ 4 errors Yritys käsitellä Pointyliluokan privateattribuutteja x and y aiheuttaa syntaksivirheen. Ohjelmointi 2 Circle 2. java käännösyrityksen output Yritys käsitellä yliluokan Point private attribuutteja x and y aiheuttaa syntaksivirheen. 23

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 24 // Fig. 9. 9: Point 2. java // Point 2 class declaration represents an x-y coordinate pair. x- ja y-koordinaatit määritelty protected attribuutteina, public class Point 2 { Click to edit Master title style joihin on pääsy aliluokasta. protected int x; // x part of coordinate pair protected int y; // y part of coordinate pair // no-argument constructor Click to edit Master text styles public Point 2() Second level { Third level // call to Object constructor occurs here Fourth implicit level } Fifth level // constructor public Point 2( int x. Value, int y. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation } // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } Ohjelmointi 2 Point 2. java Rivit 5 -6 x- ja ykoordinaatit määritelty protected attribuutteina, joihin on pääsy aliluokasta. 24

28 29 30 31 32 33 34 35 36 37 38 39 40 41

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 25 // return x from coordinate pair public int get. X() { return x; } Click to edit Master title style // set y in coordinate pair publictovoid inttext y. Value ) Click editset. Y( Master styles { Second level y = y. Value; // no need for validation Third level } Fourth level Fifth level // return y from coordinate pair public int get. Y() { return y; } Point 2. java // return String representation of Point 2 object public String to. String() { return "[" + x + ", " + y + "]"; } } // end class Point 2 Ohjelmointi 2 25

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Fig. 9. 10: Circle 3. java // Circle 3 class inherits from Point 2 and has Luokka access Circle 3 to Point 2 // protected members x and y. luokasta Point 2. private-attribuutti Click to edit Master title style radius. public class Circle 3 extends Point 2 { private double radius; // Circle 3's radius // no-argument constructor Click to edit Master text styles public Circle 3() Second level { Third level // call to Point 2 constructor occurs here Fourth implicit level } Fifth level Implisiittinen kutsu yliluokan oletusmuodostimelle. // constructor public Circle 3( int x. Value, int Muutetaan y. Value, double radius. Value ) perittyjen { muuttujien x ja y arvoja, // implicit call to Point 2 constructor occurs here muuttujat määritelty x = x. Value; // no need for validation protected –tyyppisiksi y = y. Value; // no need for validation yliluokassa Point 2. set. Radius( radius. Value ); } // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } Ohjelmointi 2 26 periytetään Circle 3. java Rivi 5 Luokka Circle 3 periytetään luokasta Point 2. Rivi 6 private attribuutti radius. Rivit 11 ja 17 Kutsutaan implisiittisesti yliluokan oletusmuodostinta. Rivit 18 -19 Muutetaan perittyjen muuttujien x ja y arvoja, muuttujat määritelty protectedtyyppisiksi yliluokassa Point 2. 26

29 30 31 32 33 34 35 36 37 38 39 40 41 42

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 27 // return radius public double get. Radius() { return radius; } Click to edit Master title style // calculate and return diameter public double get. Diameter() { Click to edit Master text styles return Second level 2 * radius; } Third level Fourth level // calculate and return circumference Fifth level public double get. Circumference() { return Math. PI * get. Diameter(); } // calculate and return area public double get. Area() { return Math. PI * radius; } Circle 3. java Rivi 56 Käytetään perittyjä attribuutteja x ja y, jotka on määritelty protected – Käsitellään perittyjä tyyppisiksi attribuutteja x ja y, jotka on yliluokassa määritelty protected – Point 2. tyyppisiksi yliluokassa // return String representation of Circle 3 object public String to. String() { Point 2. return "Center = [" + x + ", " + y + "]; Radius = " + radius; } } // end class Circle 3 Ohjelmointi 2 27

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 28 // Fig. 9. 11: Circle. Test 3. java // Testing class Circle 3. import java. text. Decimal. Format; import javax. swing. JOption. Pane; Click to edit Master title style Circletest 3. java Rivi 11 Luokaan Circle 3 -olio. public class Circle. Test 3 { Luodaan publictostatic void main( args ) Click edit Master text String[] styles { Second level // instantiate Circle object Third level Circle 3 Fourth levelcircle = new Circle 3( 37, 43, 2. 5 ); Fifth level // get Circle 3's initial x-y coordinates and radius String output = "X coordinate is " + circle. get. X() + "n. Y coordinate is " + circle. get. Y() + "n. Radius is " + circle. get. Radius(); circle. set. X( 35 ); circle. set. Y( 20 ); circle. set. Radius( 4. 25 ); // set new x-coordinate // set new y-coordinate // set new radius Käytetään // get String representation of output += "nn. The new location circle. to. String(); Circle 3 -olio. Käytetään Circle 3: n getmetodeja protected. Rivi 16 metodeja private. Käytetään Circle 3 -luokan tyyppisten attribuuttien x ja y attribuuttien käsittelyyn. get-metodeja private- perittyjä set metodeja muutettaessa new circleprotected value Käytetään Circle 3: n –tyyppisten setand radiusattribuuttien of circle aren" + x ja y arvoja. metodia private-tyyppisen attribuutin radius arvon muuttamiseen. Ohjelmointi 2 Rivit 14 -15 Käytetään perittyjä getmetodeja protected – tyypisten attribuuttien x ja y käsittelyyn. perittyjä get- tyyppisten attribuuttien käsittelyyn. Rivit 18 -19 Käytetään perittyjä setmetodeja perittyjen protected-attribuuttien x ja y arvojen muuttamiseen. Rivi 20 Käytetään Circle 3 – luokan set-metodia private-tyyppisen attribuutin radius arvon muuttamiseen. 28

29 26 27 28 29 30 31 32 33 34 35 36 37 38

29 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // format floating-point values with 2 digits of precision Decimal. Format two. Digits = new Decimal. Format( "0. 00" ); Click to edit Master title style // get Circle's diameter output += "n. Diameter is " + two. Digits. format( circle. get. Diameter() ); Click to edit Master text styles // level get Circle's circumference Second Thirdoutput level += "n. Circumference is " + two. Digits. format( circle. get. Circumference() ); Fourth level Fifth level // get Circle's area output += "n. Area is " + two. Digits. format( circle. get. Area() ); JOption. Pane. show. Message. Dialog( null, output ); // display output Circletest 3. java System. exit( 0 ); } // end method main } // end class Circle. Test 3 Ohjelmointi 2 29

9. 4 Yliluokan ja aliluokan välinen suhde • protected-attribuuttien käyttö – Edut • Aliluokan

9. 4 Yliluokan ja aliluokan välinen suhde • protected-attribuuttien käyttö – Edut • Aliluokan jäsenet voivat muuttaa attribuuttien arvoja suoraan. • Lievä tehokkuushyöty: – vältytään set- ja get-metodien liikakäytöltä. – Haitat • Ei validiteettitarkistusta – aliluokka voi sijoittaa attribuutille laittoman arvon • Implementaatioriippuvaista: – Aliluokan metodit ovat todennäköisesti riippuvaisia yliluokan toteutuksesta. – Yliluokan toteutuksen muutokset voivat aikaansaada muutoksia aliluokassa. • Hauras (fragile) ohjelmisto ict 02 d 30

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 31 // Fig. 9. 12: Point 3. java // Point class declaration represents an x-y coordinate pair. Parempi suunnittelukäytäntö: käytetään private-määrettä public class Point 3 { Click to edit Master title style protected–määreen sijaan private int x; // x part of coordinate pair aina kunpair mahdollista. private int y; // y part of coordinate // no-argument constructor Click to edit Master text styles public Point 3() Second level { Third level // call to Object constructor occurs here Fourth implicit level } Fifth level // constructor public Point 3( int x. Value, int y. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation } // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } Ohjelmointi 2 Point 3. java Parempi suunnittelukäytäntö: käytetään privatemäärettä protected– määreen sijaan aina kun mahdollista. 31

28 29 30 31 32 33 34 35 36 37 38 39 40 41

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 32 // return x from coordinate pair public int get. X() { return x; } Click to edit Master title style // set y in coordinate pair publictovoid inttext y. Value ) Click editset. Y( Master styles { Second level y = y. Value; // no need for validation Third level } Fourth level Fifth level // return y from coordinate pair public int get. Y() { return y; } Point 3. java // return String representation of Point 3 object public String to. String() { return "[" + get. X() + ", " + get. Y() + "]"; } } // end class Point 3 Ohjelmointi 2 32

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 9. 13: Circle 4. java Luokka Circle 4 // Circle 4 class inherits from Point 3 and accesses Point 3's luokasta Point 3. // private x and y via Point 3's public methods. Click to edit Master title style public class Circle 4 extends Point 3 { private double radius; 33 periytyy private-tyyppinen attribuutti radius. // Circle 4's radius Click to edit Master text styles // no-argument constructor Second level publiclevel Circle 4() Third { Fourth level // implicit call to Point 3 constructor occurs here Fifth level } // constructor public Circle 4( int x. Value, int y. Value, double radius. Value ) { super( x. Value, y. Value ); // call Point 3 constructor explicitly set. Radius( radius. Value ); } Circle 4. java Rivi 5 Luokka Circle 4 periytyy luokasta Point 3. Rivi 7 private – tyyppinen attribuutti radius. // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } Ohjelmointi 2 33

28 29 30 31 32 33 34 35 36 37 38 39 40 41

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 34 // return radius public double get. Radius() { return radius; } Click to edit Master title style // calculate and return diameter public double get. Diameter() { Click to edit Master text styles return Second level 2 * get. Radius(); } Third level Fourth level // calculate and return circumference Fifth level public double get. Circumference() { return Math. PI * get. Diameter(); } Circle 4. java Kutsutaan get. Radius –metodia sen sijaan, että viitattaisiin suoraan attribuuttiin radius. // calculate and return area public double get. Area() { return Math. PI * get. Radius(); Korvataan } luokan Point 3 metodi to. String. // return String representation of Circle 4 object public String to. String() { return "Center = " + super. to. String() + "; Radius = " + get. Radius(); } Rivit 37, 49 ja 55 Kutsutaan get. Radius – metodia sen sijaan, että käsiteltäisiin suoraan attribuuttia radius. Rivit 53 -56 Korvataan Point 3: n metodi to. String. } // end class Circle 4 Ohjelmointi 2 34

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 35 // Fig. 9. 14: Circle. Test 4. java // Testing class Circle 4. import java. text. Decimal. Format; import javax. swing. JOption. Pane; Click to edit Master title style Circletest 4. java public class Circle. Test 4 { Rivi 11 Luodaan Circle 4 -olio. publictostatic void main( args ) Rivit 14 ja 15 Click edit Master text String[] styles { Käytetään perittyjä get. Second level metodeja private // level instantiate Circle object Third Käytetään perittyjä getattribuuttien x ja y Circle 4 Fourth levelcircle = new Circle 4( 37, 43, 2. 5 ); metodeja private - lukemiseen. arvojen Fifth level Rivi 16 getattribuuttien x ja y arvojen // get Circle 4's initial x-y coordinates and radius Käytetään Circle 4: n get. String output = "X coordinate is " + circle. get. X() + lukemiseen. metodia private – "n. Y coordinate is " + circle. get. Y() + attribuutin radius "n. Radius is " + circle. get. Radius(); käsittelyyn. Rivit 18 -19 circle. set. X( 35 ); // set new x-coordinate Käytetään perittyjä setcircle. set. Y( 20 ); // set new y-coordinate Käytetään perittyjä setmetodeja privatecircle. set. Radius( 4. 25 ); // set new radius attribuuttien x ja y metodeja privateattribuuttien x ja y arvojen // get String representation of new circle value Käytetään Circle 4: n set- arvojen muuttamiseen. output += "nn. The new location and radius of circle aren" + metodia private– circle. to. String(); attribuutin radius arvon muuttamiseen. Ohjelmointi 2 Rivi 20 Käytetään Circle 4: n set-metodia private– attribuutin radius arvon muuttamiseen. 35

36 26 27 28 29 30 31 32 33 34 35 36 37 38

36 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // format floating-point values with 2 digits of precision Decimal. Format two. Digits = new Decimal. Format( "0. 00" ); Click to edit Master title style // get Circle's diameter output += "n. Diameter is " + two. Digits. format( circle. get. Diameter() ); Click to edit Master text styles // level get Circle's circumference Second Thirdoutput level += "n. Circumference is " + two. Digits. format( circle. get. Circumference() ); Fourth level Fifth level // get Circle's area output += "n. Area is " + two. Digits. format( circle. get. Area() ); JOption. Pane. show. Message. Dialog( null, output ); // display output Circletest 4. java System. exit( 0 ); } // end main } // end class Circle. Test 4 Ohjelmointi 2 36

9. 5 Case Study: Kolmitasoinen peritymishierarkia • Kolmitasoinen piste/ympyrä/lieriö-hierarkia – Piste • x-y koordinaattipari

9. 5 Case Study: Kolmitasoinen peritymishierarkia • Kolmitasoinen piste/ympyrä/lieriö-hierarkia – Piste • x-y koordinaattipari – Ympyrä • x-y koordinaattipari • säde – Lieriö • x-y koordinaattipari • säde • korkeus ict 02 d 37

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig. 9. 15: Cylinder. java // Cylinder class inherits from Circle 4. 38 private-attribuutti height. Click edit Master title style public to class Cylinder extends Circle 4 private double height; { // Cylinder's height Luokka Cylinder luokasta // no-argument constructor periytetään Click to edit Master text styles public Cylinder() Circle 4. Second level { implicit call to Circle 4 constructor occurs here Third // level } Fourth level Fifth level // constructor public Cylinder( int x. Value, int y. Value, double radius. Value, double height. Value ) { super( x. Value, y. Value, radius. Value ); // call Circle 4 constructor set. Height( height. Value ); } // set Cylinder's height public void set. Height( double height. Value ) { height = ( height. Value < 0. 0 ? 0. 0 : height. Value ); } Ohjelmointi 2 Cylinder. jav a Rivi 4 Luokka Cylinder periytetään luokasta Circle 4. Rivi 5 private – attribuutti height. 38

27 28 29 30 31 32 33 34 35 36 37 38 39 40

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 // get Cylinder's height public double get. Height() { return height; } Click to edit Master title style 39 Korvataan yliluokan Circle 4 metodi get. Area Kutsutaan Circle 4: n calculate Cylinder toteutuksella, jokaarea palauttaa get. Area-metodia Cylinder-olion pinta-alan. käyttämällä sanaa super. // override Circle 4 method get. Area to publictodouble get. Area() Click edit Master text styles { Second level return Third level 2 * super. get. Area() + get. Circumference() * get. Height(); } Fourth level Fifth level // calculate Cylinder volume public double get. Volume() { return super. get. Area() * get. Height(); } Korvataan Circle 4: n metodi to. String. Circle 4: n Cylinder Kutsutaan object to. String–metodia käyttämällä sanaa super. // return String representation of public String to. String() { return super. to. String() + "; Height = " + get. Height(); } Cylinder. java Rivit 34 ja 42 Korvataan yliluokan Circle 4 metodi get. Area toteutuksella, joka palauttaa Cylinder-olion pintaalan. Rivi 36 Kutsutaan Circle 4: n get. Area-metodia käyttämällä sanaa super. Rivit 46 -49 Korvataan Circle 4: n metodi to. String. Line 48 Kutsutaan Circle 4: n to. String –metodia käyttämällä sanaa super. } // end class Cylinder Ohjelmointi 2 39

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 40 // Fig. 9. 16: Cylinder. Test. java // Testing class Cylinder. import java. text. Decimal. Format; import javax. swing. JOption. Pane; Click to edit Master title style Cylinder. Test. java Rivit 14 ja 15 Kutsutaan epäsuoraan perittyjä Point 3: n getmetodeja. public class Cylinder. Test { publictostatic void main( args ) Click edit Master text String[] styles { Rivi 16 Second level // create Cylinder object Kutsutaan suoraan perittyä Third level Circle 4: n get-metodia. Cylinder Fourth level cylinder = new Cylinder( 12, 23, 2. 5, 5. 7 ); Kutsutaan epäsuoraan perittyjä Point 3: n get. Fifth level Rivi 16 Kutsutaan // get Cylinder's initial x-y coordinates, radius and height Kutsutaan suoraan perittyä Cylinder–luokan metodeja. Kutsutaan Cylinder– get-metodia. String output = "X coordinate is " + cylinder. get. X() + Circle 4: n get-metodia. luokan get-metodia. "n. Y coordinate is " + cylinder. get. Y() + "n. Radius is " + Rivit 18 -19 cylinder. get. Radius() + "n. Height is " + cylinder. get. Height(); Kutsutaan epäsuoraan perittyjä Point 3: n setcylinder. set. X( 35 ); // set new x-coordinate metodeja. Kutsutaan epäsuoraan cylinder. set. Y( 20 ); // set new y-coordinate cylinder. set. Radius( 4. 25 ); // set new perittyjä radius Point 3: n set. Rivi 20 metodeja. suoraan perittyä Kutsutaan suoraan perittyä cylinder. set. Height( 10. 75 ); // set new Kutsutaan height Circle 4: n set-metodia. Kutsutaan Cylinder–luokan cylinder value Rivi 21 set-metodia. // get String representation of new output += "nn. The new location, radius and height of cylinder aren" + cylinder. to. String(); Kutsutaan korvattua to. String-metodia. Ohjelmointi 2 Kutsutaan Cylinder: n setmetodia. Rivi 26 Kutsutaan korvattua to. String-metodia. 40

41 28 29 30 31 32 33 34 35 36 37 38 39 40

41 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 // format floating-point values with 2 digits of precision Decimal. Format two. Digits = new Decimal. Format( "0. 00" ); Click//toget edit Master diameter title style Cylinder's output += "nn. Diameter is " + two. Digits. format( cylinder. get. Diameter() ); Click // to get edit. Cylinder's Master text styles circumference Secondoutput level += "n. Circumference is " + two. Digits. format( cylinder. get. Circumference() ); Third level Fourth level get Cylinder's area Fifth // level output += "n. Area is " + two. Digits. format( cylinder. get. Area() ); Cylinder. Test. java Kutsutaan korvattua // get Cylinder's volume output += "n. Volume is " + two. Digits. format( cylinder. get. Volume() ); get. Area-metodia. JOption. Pane. show. Message. Dialog( null, output ); // display output Rivi 40 Kutsutaan korvattua get. Area -metodia. System. exit( 0 ); } // end main } // end class Cylinder. Test Ohjelmointi 2 41

9. 6 Muodostimet ja lopettimet aliluokissa • Aliluokan olion luonti – Muodostinkutsujen ketju •

9. 6 Muodostimet ja lopettimet aliluokissa • Aliluokan olion luonti – Muodostinkutsujen ketju • Aliluokan muodostin kutsuu yliluokan muodostinta. – Implisiittisesti tai eksplisiittisesti. • Periytymishierarkian perusta – Viimeisin ketjussa kutsuttu muodostin on Objectluokan muodostin. – Alkuperäisen aliluokan muodostimen runko suoritetaan viimeiseksi. – Esimerkki: Point 3/Circle 4/Cylinder hierarkia • Point 3 –luokan muodostinta kutsutaan toiseksi viimeiseksi (viimeisenä kutsutaan Object – luokan muodostinta). • Point 3 muodostimen koodi suoritetaan toisena (ensiksi suoritetaan Object muodostimen koodi). ict 02 d 42

9. 6 Muodostimet ja lopettimet johdetuissa luokissa • Roskia keräävä aliluokan olio – finalize–metodikutsujen

9. 6 Muodostimet ja lopettimet johdetuissa luokissa • Roskia keräävä aliluokan olio – finalize–metodikutsujen ketju • Käänteinen muodostinketjuun nähden. • Aliluokan lopetinta kutsutaan ensiksi. • Seuraavaksi hierarkiassa seuraavan yliluokan lopetin. – Jatketaan ylös hierarkiassa kunnes viimeisimmän yliluokan lopetin on saavutettu. • Viimeisimmän yliluokan (Object) lopettimen jälkeen olio on poistettu muistista. ict 02 d 43

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 44 // Fig. 9. 17: Point. java // Point class declaration represents an x-y coordinate pair. public class Point { private int x; // x part of coordinate pair private int y; // y part of coordinate pair Click to edit Master title style // no-argument constructor public Click to Point() edit Master text styles { Second level implicit call to Object constructor occurs here Third// level System. out. println( "Point no-argument constructor: " + this ); Fourth level } Fifth level // constructor public Point( int x. Value, int y. Value ) { // implicit call to Object constructor occurs here x = x. Value; // no need for validation y = y. Value; // no need for validation System. out. println( "Point constructor: " + this ); } Point. java Rivit 12, 22 ja 28 Muodostimien ja lopettimen ja output-viestit demoavat lopettimen kutsujärjestyksiä. tulosteet kutsujärjetyksien demoamiseksi. // finalizer protected void finalize() { System. out. println( "Point finalizer: " + this ); } Ohjelmointi 2 44

31 32 33 34 35 36 37 38 39 40 41 42 43 44

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 45 // set x in coordinate pair public void set. X( int x. Value ) { x = x. Value; // no need for validation } Click to edit Master title style // return x from coordinate pair public int get. X() { Click to edit Master text styles return Second levelx; } Third level Fourth level // set y in coordinate pair Fifth level public void set. Y( int y. Value ) { y = y. Value; // no need for validation } Point. java // return y from coordinate pair public int get. Y() { return y; } // return String representation of Point 4 object public String to. String() { return "[" + get. X() + ", " + get. Y() + "]"; } } // end class Point Ohjelmointi 2 45

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 46 // Fig. 9. 18: Circle. java // Circle 5 class declaration. public class Circle extends Point { Click to edit Master title style private double radius; // Circle's radius // no-argument constructor public Circle() Click to edit Master text styles { Second level implicit call to Point constructor occurs here Third// level System. out. println( "Circle no-argument constructor: " + this ); Fourth level } Circle. java Fifth level // constructor public Circle( int x. Value, int y. Value, double radius. Value ) { super( x. Value, y. Value ); // call Point constructor set. Radius( radius. Value ); System. out. println( "Circle constructor: " + this ); } // finalizer protected void finalize() { System. out. println( "Circle finalizer: " + this ); super. finalize(); Muodostimien ja lopettimen 12, 21 ja 29 output-viestit. Rivit demoavat Muodostimien ja kutsujärjestyksiä. lopettimen tulosteet kutsujärjetyksien demoamiseksi. // call superclass finalize method } Ohjelmointi 2 46

47 32 33 34 35 36 37 38 39 40 41 42 43 44

47 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 // set radius public void set. Radius( double radius. Value ) { radius = ( radius. Value < 0. 0 ? 0. 0 : radius. Value ); } Click to edit Master title style // return radius Click to edit Master text styles public double get. Radius() Second level { Third return level radius; } Fourth level Fifth level // calculate and return diameter public double get. Diameter() { return 2 * get. Radius(); } Circle. java // calculate and return circumference public double get. Circumference() { return Math. PI * get. Diameter(); } Ohjelmointi 2 47

48 55 56 57 58 59 60 61 62 63 64 65 66 67

48 55 56 57 58 59 60 61 62 63 64 65 66 67 68 // calculate and return area public double get. Area() { return Math. PI * get. Radius(); } Click to edit Master title style Click to edit. String Master text styles of Circle 5 object // return representation Second level public String to. String() { level Third Fourthreturn level "Center = " + super. to. String() + "; Radius = " + get. Radius(); } level Fifth } // end class Circle. java Ohjelmointi 2 48

49 1 2 3 4 5 6 7 8 9 10 11 12 13

49 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig. 9. 19: Constructor. Finalizer. Test. java // Display order in which superclass and subclass // constructors and finalizers are called. Click to edit Master title style public class Constructor. Finalizer. Test { public void main( String args[] ) Click to static edit Master text styles { Second level Point Third levelpoint; Circle Fourth levelcircle 1, circle 2; Fifth level point = new Point( 11, 22 ); Point–olion luonti. System. out. println(); circle 1 = new Circle( 72, 29, 4. 5 ); System. out. println(); circle 2 = new Circle( 5, 7, 10. 67 ); point = null; circle 1 = null; circle 2 = null; // mark for garbage collection System. out. println(); Ohjelmointi 2 Constructor. Fi nalizer. Test. j ava Rivi 12 Kahden Circle–olion Point–olion luonti ali- ja yliluokkien luonti. muodostin- ja lopetinkutsujen Rivit 15 ja 18 havainnollistamiseksi. Kahden Circle– olion luonti ali- ja yliluokkien muodostin- ja lopetinkutsujen havainnollistamiseksi. 49

50 26 27 28 29 30 System. gc(); // call the garbage collector }

50 26 27 28 29 30 System. gc(); // call the garbage collector } // end main Click to edit Master title style } // end class Constructor. Finalizer. Test Point. Click constructor: 22] to edit [11, Master text styles Second level Point. Third constructor: Center = [72, 29]; Radius = 0. 0 level Circle constructor: Center = [72, 29]; Radius = 4. 5 Fourth level Fifth level Point constructor: Center = [5, 7]; Radius = 0. 0 Circle constructor: Center = [5, 7]; Radius = 10. 67 Point finalizer: [11, 22] Circle finalizer: Center = [72, 29]; Radius = 4. 5 Point finalizer: Center = [72, 29]; Radius = 4. 5 Circle finalizer: Center = [5, 7]; Radius = 10. 67 Point finalizer: Center = [5, 7]; Radius = 10. 67 Ohjelmointi 2 Aliluokan Circle– muodostimen runko suoritetaan yliluokan Point 4 muodostimen suorituksen jälkeen. Circle-olion lopettimia Constructor. F kutsutaan käänteisessä järjestyksessä muodostimiin inalizer. Test. nähden. java 50

9. 9 Ohjelmistosuunnittelu ja periytyminen • Olemassa olevan ohjelman hyödyntäminen – Periytetään valmiita luokkia

9. 9 Ohjelmistosuunnittelu ja periytyminen • Olemassa olevan ohjelman hyödyntäminen – Periytetään valmiita luokkia • Lisätään tarvittavat ominaisuudet ja toiminteet • Uudelleenmääritellään yliluokan jäseniä • Ei suoraa pääsyä yliluokan lähdekoodiin – Viittaukset esikäännettyyn koodiin – Independent software vendors (ISVs) • Kehittävät lisensoitua koodia myytäväksi – Julkaisu esikäännetyssä muodossa • Käyttäjät johtavat uusia luokkia – ISV lähdekoodiin pääsyä ei tarvita ict 02 d 51