topic 26 managing complexity
play

Topic 26 Managing Complexity Introduction to Inheritance and - PowerPoint PPT Presentation

Topic 26 Managing Complexity Introduction to Inheritance and software development : The practice of conceptualizing, Polymorphism designing, constructing, documenting, and testing large- "One purpose of CRC cards [a design tool] is


  1. Topic 26 Managing Complexity Introduction to Inheritance and � software development : The practice of conceptualizing, Polymorphism designing, constructing, documenting, and testing large- "One purpose of CRC cards [a design tool] is scale computer programs. (a.k.a. software engineering) to fail early, to fail often, and to fail � Challenges: – managing lots of programmers inexpensively. It is a lot cheaper to tear up a – dividing work bunch of cards that it would be to reorganize – avoiding redundant code (wasted effort) a large amount of source code. " – finding and fixing bugs – testing - Cay Horstmann – maintenance (between 50% and 90% of cost) � Code reuse : writing code once and reusing it in different applications and programs. Based on slides for Building Java Programs by Reges/Stepp, found at http://faculty.washington.edu/stepp/book/ CS305j Inheritance and Polymorphism CS305j Inheritance and Polymorphism 1 2 Introduction to Computing Introduction to Computing Categories Subcategories � Often we categorize objects in the real world � Within the broad range of people, let's talk about a Person specific group: starting with a general category and then becoming members of the University name more specific. – What new attributes and behaviors are there? age favorite movie – Let's think about people, human beings. What things – Let's assume that all members are people, and draw does every human being have? What does every human them as a subcategory of persons, as shown to the talk play right being know how to do? – Members of the University add some new abilities – We could draw a diagram of these things. Something and attributes. Member of the University • UTEID like this: Person • work UTEID what I owe name – Employees perform some of the age books checked data original person's abilities differently. favorite movie out • talk about the University talk (about UT) talk behaviors – Notice we don't repeat things from Person work play check out book CS305j Inheritance and Polymorphism 3 CS305j Inheritance and Polymorphism 4 Introduction to Computing Introduction to Computing

  2. More categorization Categories as code � We can add more � Let's implement a class for a person categories. Person public class Person { – Each category has the private String favoriteMovie; ability of the ones above it. Member of the public void talk(){ University – Categories may add System.out.println("Hi!"); new abilities, or change } Faculty Student Staff the way in which they public void play(){ perform existing abilities. System.out.println("Watching " + favoriteMovie); Tenure Track Lecturer Grad Undergrad } } CS305j Inheritance and Polymorphism CS305j Inheritance and Polymorphism 5 6 Introduction to Computing Introduction to Computing Subcategory, first try Inheritance � inherit : To gain all the data fields and methods from � The class for Member has much in another class, and become a child of that class. common with Person: – superclass : The class from which you inherit. – subclass : The class that inherits. public class UniversityMember { � Inheritance, general syntax: private String favoriteMovie; public class <name> extends <class name> { private String UTEID; – Example: public class UniversityMember extends Person { public void talk(){ – Each member object now automatically: System.out.println("Hi!"); • has a talk and play method } • can be treated as a Person by any other code (e.g. an Employee could be stored as an element of a Person[] ) public void play(){ CS305j Inheritance and Polymorphism 7 CS305j Inheritance and Polymorphism 8 Introduction to Computing Introduction to Computing i l (" hi "

  3. Subclass with inheritance Another example: Shapes � A better version of the UniversityMember class: � Imagine that we want to add a class Square. public class UniversityMember extends Person{ private String UTEID; public void who(){ rectangle System.out.println("My UTEID is " + UTEID); square } } – A square is just a rectangle with both sides the same – We only write the portions that are unique to length. each (data) type. – If we wrote Square from scratch, its behavior would largely – If we have a UniversityMember object we can duplicate that from Rectangle. – We'd like to be able to create a Square class that absorbs call all methods from Person and behavior from the Rectangle class, to remove the UniversityMember redundancy. CS305j Inheritance and Polymorphism CS305j Inheritance and Polymorphism 9 10 Introduction to Computing Introduction to Computing Square and Rectangle Inheritance � Differences between square code and rectangle code: � Reminder: inheritance, general syntax: – Square only needs one size parameter to its constructor. public class <name> extends <class name> { Square sq = new Square(3, 7, 4); rather than Rectangle rect = new Rectangle(2, 9, 13, 6); � Let's declare that all squares are just special – Squares print themselves differently with toString. rectangles: rather than "Square[x=3,y=7,size=4]" public class Square extends Rectangle { "Rectangle[x=2,y=9,width=13,height=6]" � Each Square object now automatically: – has an x, y, width, and height data field – has a getArea, getPerimeter, draw, toString, intersection method – can be treated as a Rectangle by any other code (e.g. a Square could be stored as an element of a Rectangle[] ) CS305j Inheritance and Polymorphism 11 CS305j Inheritance and Polymorphism 12 Introduction to Computing Introduction to Computing

  4. Inheritance and constructors Overriding methods � We want Squares to be constructed with only an (x, y) � We don't want to inherit the toString behavior, position and a size (because their width and height are because Squares print themselves differently than equal). rectangles. – But internally, the Square has x/y/width/height data fields, which all need to be initialized. � override : To write a new version of a method in a – We can make a Square constructor that uses the Rectangle subclass, replacing the superclass's version. constructor to initialize all the data fields. – If Square declares its own toString method, it will replace the � Syntax for calling superclass's constructor: super( <parameter(s)> ); Rectangle toString code when Squares are printed . public String toString() { – Example: return "Square[x=" + this.getX() + ",y=" + this.getY() + public class Square extends Rectangle { ",size=" + this.getWidth() + "]"; public Square(int x, int y, int size) { } super(x, y, size, size); • We have to say, for example, this.getX() instead of this.x because the } data fields are private (can only actually be modified inside the } Rectangle class). • Each Square object is initialized with its width and height equal. The • We'll use this.getWidth() as our size, but the height would also work size parameter to the Square constructor provides the value for the width and height parameters to the superclass Rectangle constructor. equally well. CS305j Inheritance and Polymorphism CS305j Inheritance and Polymorphism 13 14 Introduction to Computing Introduction to Computing Relatedness of types Shape area, perimeter � We've previously written several 2D geometric � Rectangle types such as Circle and Rectangle. – area = w h – We could add other geometric types such as Triangle. – perimeter = 2 w + 2 h � There are certain attributes or operations that are � Circle common to all shapes. = � r 2 – area – perimeter - distance around the outside of the shape – perimeter = 2 � r – area - amount of 2D space occupied by the shape � Triangle � Every shape has these attributes, but each – area = (1/2) b h computes them differently. – perimeter = side1 + side2 + side3 CS305j Inheritance and Polymorphism 15 CS305j Inheritance and Polymorphism 16 Introduction to Computing Introduction to Computing

Recommend


More recommend