<?xml version="1.0" encoding="UTF-8"?> <chapter version="5.0" xml:id="sw1ChapterInheritance" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:svg="http://www.w3.org/2000/svg" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:db="http://docbook.org/ns/docbook"> <title>Inheritance</title> <section xml:id="sw1SectGeometryInherit"> <title>Geometry classes reconsidered</title> <para>In <xref linkend="sd1GeometryClasses"/> you implemented rectangle and circle related classes. You may have observed a lack of translation related parameters: Our rectangle and circle instances are being described by with and height or radius respectively.</para> <para>Implementing a drawing application requires our objects to be moved from one position to another e.g.:</para> <figure xml:id="sd1FigFigureMove"> <title>Moving figures</title> <mediaobject> <imageobject> <imagedata fileref="Ref/Fig/figureMove.fig"/> </imageobject> </mediaobject> </figure> <para>We might implement translation (move) related parameters and methods in both classes Rectangle and Circle independently. But since the underlying operation is indeed shape independent we choose a different approach using inheritance in the subsequent exercises.</para> <section xml:id="sd1SectFigureBaseClass"> <title>Defining a <classname>Figure</classname> class hierarchy.</title> <qandaset defaultlabel="qanda" xml:id="sd1QandaFigureBaseClass"> <qandadiv> <qandaentry> <question> <para>Our two geometric primitives circle and rectangle will need a reference point (x,y) in order to allow for defining <methodname>move(...)</methodname> operations. We choose the center of gravity both for rectangles and circles:</para> <mediaobject> <imageobject> <imagedata fileref="Ref/Fig/figureCenter.fig"/> </imageobject> </mediaobject> <para>We thus need two additional parameters <property>x</property> and <property>y</property> representing an object's position. Consider the following <xref linkend="glo_UML"/> inheritance diagram and implement the three <xref linkend="glo_Java"/> classes <classname>Figure</classname>, <classname>Circle</classname> and <classname>Rectangle</classname>:</para> <mediaobject> <imageobject> <imagedata fileref="Ref/Fig/figureInherit.fig"/> </imageobject> </mediaobject> <para>Create unit tests which check for correct implementation of the translation example from <xref linkend="sd1FigFigureMove"/> and the two methods <methodname>getArea()</methodname> and <methodname>getPerimeter()</methodname>.</para> <para>Why is the <methodname>move(...)</methodname> method being defined returning an instance of class <classname>Figure</classname>?</para> </question> <answer> <annotation role="make"> <para role="eclipse">Sd1/Figure/BaseClass</para> </annotation> <para>Defining <methodname>move(...)</methodname> returning an instance of class <classname>Figure allows for operation chaining:</classname></para> <programlisting language="none">final Circle c = new Circle(1., 2., 3.0); c.move(1, 5).move(-3, 7);</programlisting> </answer> </qandaentry> </qandadiv> </qandaset> </section> <section xml:id="sd1SectFigureScale"> <title>Scaling figures</title> <qandaset defaultlabel="qanda" xml:id="sd1QandaFigureScaling"> <qandadiv> <qandaentry> <question> <para>We want allow for scaling of figures:</para> <mediaobject> <imageobject> <imagedata fileref="Ref/Fig/figureScale.fig"/> </imageobject> </mediaobject> <para>In the above example the rectangle is being <quote>inflated</quote> whereas the circle is being shrunk to half its original size. Add a corresponding scale(...) method to the Figure inheritance hierarchy which allows for operation chaining as well.</para> </question> <answer> <annotation role="make"> <para role="eclipse">Sd1/Figure/XXX</para> </annotation> </answer> </qandaentry> </qandadiv> </qandaset> </section> </section> </chapter>