Comic Strip Analysis and Design Inheritance Abstract Classes
Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism Barb Ericson Georgia Institute of Technology Nov 2005 Georgia Institute of Technology
Learning Goals • Do an analysis for a comic strip • Do a design for a comic strip • Pull out a common parent class – Generalization • Give an example of an abstract class • Extend a parent class, but do something differently – Specialization • Understand polymorphism – And how run-time or dynamic binding works Georgia Institute of Technology
What do we mean by a comic strip? • What parts does it have? • What things should you be able to find out about one? Georgia Institute of Technology
A Comic Strip • Has a name and an author • Typically has 4 panels, but can have more or less The Comic Strip: Pears Before Swine Georgia Institute of Technology
A Comic Panel • Has a picture • And one or more speech balloons Georgia Institute of Technology
A Speech Balloon • Is a rounded rectangle or ellipse with text (words) in it • Has a tail that indicates who is speaking – The tail is shown as a triangle from the bottom of the rounded rectangle to the speaker Georgia Institute of Technology
A UML Class Diagram • Shows the design for an object-oriented solution Comic. Strip author name – Shows each class as a rectangular box • May include the fields and methods – Shows the relationships between objects of the classes 1* Comic. Panel 1 0* Picture Speech. Balloon upper. Left tail. End message • Has a (association) • Is a (inheritance) Georgia Institute of Technology
Thought Balloons • Are like speech balloons – But they show what a character is thinking, not saying – Use circles to connect balloon to thinker The Comic Strip: Ben Georgia Institute of Technology
How to Handle Thought Balloons? • We could copy the class for speech balloon and save it out with a new name – And change everywhere that has Speech to Thought – But what if we later want to add something to both classes • We would have to add it in both classes – Also, how would we handle the Comic. Panel? • Would it have 0 or more Speech. Balloons and 0 or more Thought. Balloons? Georgia Institute of Technology
How to Handle Thought Balloons? • We could make a new class Thought. Balloon Comic. Strip author name – As a subclass of Speech. Balloon • Since they are very similar • But, is this right? – Is a Thought. Balloon a kind of Speech. Balloon? 1* Comic. Panel 1 0* Picture Speech. Balloon upper. Left tail. End message Thought. Balloon Georgia Institute of Technology
Be Careful with Inheritance! • Only use it when the child is really of the same type of thing as the parent – Not just to reuse similarities • Instead pull out a common parent class – And have both Speech. Balloon and Thought. Balloon inherit from it • Pull out all common fields and methods – And put them in the common parent class • This is also called generalization Georgia Institute of Technology
Generalization • Move common fields and methods into a parent class – And let the children classes inherit the fields and methods – Easier to maintain than copying fields and methods – Better than using inheritance when the child isn’t really a kind of parent – Makes it easier to add new children classes Comic. Strip author name 1* Comic. Panel 1 Picture Speech. Balloon Georgia Institute of Technology 0* Text. Balloon upper. Left tail. End message Thought. Balloon
Text. Balloons • What should a text balloon know how to do? – It should know how to draw itself • It will need a graphics context – Used to do the drawing • Add the draw method to the UML class diagram Text. Balloon upper. Left tail. End Message draw(Graphics g) Georgia Institute of Technology
Abstract Class • Can we draw just a text balloon? – What would we draw? • We could draw the balloon and text • What kind of tail would it have? • We need to know what type of text balloon it is in order to draw it. – Which we will only know at run-time • Can’t predict what will be needed • So Text. Balloon is an abstract class – One that you can not create an object from – You can only inherit from it – And it will have an abstract method draw. Tail • Subclasses will override draw or also be abstract Georgia Institute of Technology
Try it • Try to create a Text. Balloon object using the interactions pane in Dr. Java – Use the constructor that takes a upper left point (java. awt. Point), a width, the point of the tail (java. awt. Point) and a string. import java. awt. Point; Text. Balloon balloon = new Text. Balloon(new Point (23, 33), 100, new Point(50, 40), ”Hi”); • What error do you get? Georgia Institute of Technology
How to Create an Abstract Class • Add the keyword abstract to the class definition – public abstract class Text. Balloon • Abstract classes often have abstract methods – To create an abstract method add the keyword abstract to the method definition • and end the method with a semicolon after the parameter list – public abstract void draw. Tail(param. List); Georgia Institute of Technology
Exercise • Run the main method in Comic. Panel to see an example with both a speech balloon and a thought balloon • Modify the main method to create your own comic panel with a speech balloon and a thought balloon Georgia Institute of Technology
Polymorphism • Means many forms • Means that the method that is executed depends on the type of the object – Often the method is determined by the type of the object at runtime – This is called dynamic or run-time binding Text. Balloon upper. Left tail. End Message draw. Tail() Speech. Balloon Thought. Balloon draw. Tail() Georgia Institute of Technology
Adding a Text. Balloon to a Comic. Panel • Notice that Comic. Panel has add(Text. Balloon text. Balloon) – Adds it to a list of text balloons • The method will be called with both Speech. Balloon and Thought. Balloon – They are both types of Text. Balloon so there is no problem with this Text. Balloon upper. Left tail. End Message draw. Tail() Speech. Balloon Thought. Balloon draw. Tail() • Called Upcasting Georgia Institute of Technology
Run-time Binding of Methods • In get. Final. Picture() – Each element of the text. Balloon. List is told to draw • And in draw each is told to draw. Tail() – What method is called for each of the two types of Text. Balloons? • The Speech. Balloon • The Thought. Balloon Georgia Institute of Technology
Finding the draw method • The java virtual machine will start looking for a draw method – In the object that defines the class that created the current object • Each object keeps a reference to the class that created it – If the method isn’t found it will try the parent of the class that created the object – And so on up the inheritance tree until the method is found • It will be found or the code wouldn’t have compiled – draw will be found in Text. Balloon Georgia Institute of Technology
Finding the draw. Tail method • The draw method calls draw. Tail – Again the Java virtual machine will look for the method starting with the class that created the object • So for a Speech. Balloon object it will find it in Speech. Balloon • And for a Thought. Balloon object it will find it in Thought. Balloon Georgia Institute of Technology
How it works s. Balloon: Speech. Balloon upper. Left = obj. Ref tail. End = obj. Ref Message= obj. Ref width = 100 class = obj. Ref t. Balloon: Thought. Balloon upper. Left = obj. Ref tail. End = obj. Ref Message= obj. Ref width = 100 class = obj. Ref Text. Balloon upper. Left tail. End Message width draw(graphics g) draw. Tail() Speech. Balloon Thought. Balloon draw. Tail() Georgia Institute of Technology
Adding a new Subclass • A whisper balloon has a dashed outline and small dim lettering. It indicates the character is whispering. • Create a Whisper. Balloon class – What class should it inherit from? – What methods need to be overriden? – How can you draw a dashed outline? • Set the type of stroke to dashed • See http: //java. sun. com/docs/books/tutorial/2 d/display/s trokeandfill. html Georgia Institute of Technology
Creating a Subclass • Use the extends keyword to say what class the current class is inheriting from – If none specified inherits from java. lang. Object – public class Speech. Balloon extends Text. Balloon – public class Whisper. Balloon extends Speech. Balloon • A whisper is still a type of speech Georgia Institute of Technology
Creating a Subclass • Click on New and type the following public class Whisper. Balloon extends Speech. Balloon { } • Save it in Whisper. Balloon. java • Try to compile it Georgia Institute of Technology
Subclass Constructors • If the parent’s fields are private – And they should be private – You won’t be able to directly access them • But you will still need a way to initialize them • You can use super(param. List) – As the first line of code in a constructor • To invoke the parent’s constructor with the same parameter. List • If you don’t have this – A call to super() is added for you by the compiler • As the first line of code in the child class constructor Georgia Institute of Technology
Add a Constructor to Whisper. Balloon • Speech. Balloon doesn’t have a no argument constructor – So you will need to add an explicit call to the parent’s constructor import java. awt. Point; public class Whisper. Balloon extends Speech. Balloon { public Whisper. Balloon (Point u. Left, int the. Width, Point t. End, String the. Message) { super(u. Left, the. Width, t. End, the. Message); } } Georgia Institute of Technology
Overriding Methods • If a subclass has a method with the same name and parameter list as a parent – That will be the first method found when looking for the method • It will be executed instead of the parent method • This is called overriding a parent’s method • If you still want the parent’s method to be executed – Use a call to super. method() to start looking • In the parent class of the class that has the method with the call in it Georgia Institute of Technology
What Method to Override? • We could override draw but do we need to? – It has most of the functionality that we need • Just need to save the current stroke, set the stroke to dashed, and reset it after doing the same as the parent draw. Balloon – In draw. Balloon • Just need to set the font to a smaller and plain font after we create a Whisper. Balloon object – In the constructor Georgia Institute of Technology
Protected Visibility • Means subclasses and all classes in the same package have access – A bit more private than public – Used to override an inherited method • The public draw method calls 3 protected methods – draw. Balloon – draw. Tail – draw. Text Georgia Institute of Technology
Exercise • There are other kinds of text balloons – Some speech balloons only have a line to the speaker, not a triangle. – Some speech balloons don’t draw the outline of the balloon in black. – Some thought balloons use a cloud to enclose the text. – A scream balloon has bold text and a spiny outline with a flash like tail. It indicates the character is screaming. • Pick at least one of these and implement it. – Present what you did and why Georgia Institute of Technology
Summary • In analysis you try to understand the problem you are trying to solve – What are things involved? – What is each responsible for? • In design you describe classes and their relationships – In a UML class diagram • Inheritance is used to – Pull out common things into a parent class - Generalization – Allow a child to differ from a parent - Specialization • Abstract classes are used to allow for easy addition of new subclasses – With some abstract method overriden by the child class • Polymorphism allows for the right method to be executed based on the run-time type of the object Georgia Institute of Technology
- Slides: 33