{"id":626,"date":"2013-10-12T23:58:22","date_gmt":"2013-10-13T06:58:22","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=626"},"modified":"2013-10-12T23:59:40","modified_gmt":"2013-10-13T06:59:40","slug":"lab-week-11-inheritance","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2013\/10\/lab-week-11-inheritance\/","title":{"rendered":"Week 11 Lab &#8211; Inheritance"},"content":{"rendered":"<p>The following exercises are taken from chapter 8 of the textbook.<\/p>\n<p><strong>Exercise 8.4<\/strong> Open the\u00a0<a href=\"http:\/\/www.cse.unsw.edu.au\/~cs1400\/13s2\/examples\/dome-v2.zip\">project dome-v2<\/a>. This project contains a version of the DoME application rewritten to use inheritance, as described above. Note that the class<strong>diagram<\/strong> displays the inheritance relationship. Open the source code of the DVD class and remove the &#8216;<strong>extends Item<\/strong>&#8216; phrase. Close the editor. What changes do you observe in the class diagram? Add the &#8216;<strong>extends Item<\/strong>&#8216; phrase again.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 8.5<\/strong> Create a\u00a0<strong>CD<\/strong> object. Call some of its methods. Can you call the inherited methods (for example,\u00a0<strong>setComment<\/strong>)? What do you observe about the inherited methods?<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 8.6<\/strong> In order to illustrate that a subclass can access non-private elements of the superclass without any special syntax, try the following slightly artificial modification to the\u00a0<strong>CD<\/strong> and\u00a0<strong>Item<\/strong> classes. Create a method called\u00a0<strong>printShortDetails<\/strong>in the\u00a0<strong>CD<\/strong> class. Its task is to print just the artist and the title of a CD on a line by itself. However, because the\u00a0<strong>titIe<\/strong> field is private in the\u00a0<strong>Item<\/strong> class, it will be necessary to add a public\u00a0<strong>getTitIe<\/strong> method to Item. Call this method from\u00a0<strong>printShortDetails<\/strong> to access the title for printing. Remember that no special syntax is required when a subclass calls a superclass method. Try out your solution by creating a\u00a0<strong>CD<\/strong> object. Implement a similar method in the\u00a0<strong>DVD<\/strong> class to print just the director and title.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 8.8 <\/strong>Open the\u00a0<em>dome-v2 <\/em>project. Add a class for games and a subclass for video games to the project. Create some video game objects and test that all methods work as expected.<\/p>\n<p><a href=\"http:\/\/blogs.unsw.edu.au\/infs1609\/files\/2011\/09\/inheritance.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blogs.unsw.edu.au\/infs1609\/files\/2011\/09\/inheritance.png\" alt=\"Database inheritance diagram\" width=\"500\" height=\"300\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 8.12 <\/strong>Assume we have four classes:\u00a0<strong>Person<\/strong>,\u00a0<strong>Teacher<\/strong>,\u00a0<strong>Student<\/strong> and<strong>PhDStudent<\/strong>.\u00a0<strong>Teacher<\/strong> and\u00a0<strong>Student<\/strong> are both subclasses of\u00a0<strong>Person<\/strong>.\u00a0<strong>PhDStudent<\/strong> is a subclass of\u00a0<strong>Student<\/strong>.<\/p>\n<p>a. Which of the following assignments are legal, and why or why not?<br \/>\n<code><br \/>\nPerson p1 = new Student();<br \/>\nPerson p2 = new PhDStudent();<br \/>\nPhDStudent phd1 = new Student();<br \/>\nTeacher t1 = new Person();<br \/>\nStudent s1 = new PhDStudent();<br \/>\n<\/code><\/p>\n<p>b. Suppose that we have the following legal declarations and assignments<\/p>\n<p><code>Person p1 = new Person();<br \/>\nPerson p2= new PerSon ();<br \/>\nPhDStudent phd1 = new PhDStudent();<br \/>\nTeacher t1 = new Teacher () ;<br \/>\nStudent s1 = new Student();<br \/>\n<\/code><\/p>\n<p>Based on those just mentioned, which of the following assignments are legal and or why not?<\/p>\n<p><code>s1 = p1;<br \/>\ns1\u00a0 = p2;<br \/>\np1 = s1;<br \/>\nt1 = st;<br \/>\ns1 = phd1;<br \/>\nphd1 = s1;<br \/>\n<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 8.13 <\/strong>Test your answers to the previous question by creating bare-bones versions of the classes mentioned in that exercise and trying it out in BlueJ<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following exercises are taken from chapter 8 of the textbook. Exercise 8.4 Open the\u00a0project dome-v2. This project contains a version of the DoME application rewritten to use inheritance, as described above. Note that the classdiagram displays the inheritance relationship. Open the source code of the DVD class and remove the &#8216;extends Item&#8216; phrase. Close [&hellip;]<\/p>\n","protected":false},"author":328,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[630],"tags":[],"class_list":["post-626","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/users\/328"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/comments?post=626"}],"version-history":[{"count":5,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/626\/revisions"}],"predecessor-version":[{"id":631,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/626\/revisions\/631"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}