Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Advanced Object-Oriented Programming Features
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CSSE501 Object-Oriented Development
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
18 - Abstract Classes, Polymorphism, and Inheritance 1 Abstract Classes, Polymorphism, and Inheritance Barb Ericson Georgia Institute of Technology Dec.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
What is inheritance? It is the ability to create a new class from an existing class.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance in the Java programming language J. W. Rider.
Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part1 Barb Ericson Georgia Institute of.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Modern Programming Tools And Techniques-I
Object-Oriented Design
Inheritance and Polymorphism
Object-Oriented Programming
An Introduction to Inheritance
C++ Classes C++ Interlude 1.
Object Oriented Analysis and Design
Barb Ericson Georgia Institute of Technology May 2006
Inheritance, Polymorphism, and Interfaces
Workshop for CS-AP Teachers
Barb Ericson Georgia Institute of Technology Oct 2005
Inheritance.
Inheritance and Polymorphism
More on Creating Classes
Creating and Modifying Text part 3
More on Creating Classes part 3
Presentation transcript:

Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of Technology Nov 2005

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 run- time –This is called dynamic or run-time binding TextBalloon upperLeft tailEnd Message drawTail() ThoughtBalloon drawTail() SpeechBalloon drawTail()

Georgia Institute of Technology Adding a TextBalloon to a ComicPanel Notice that ComicPanel has add(TextBalloon textBalloon) –Adds it to a list of text balloons The method will be called with both SpeechBalloon and ThoughtBalloon –They are both types of TextBalloon so there is no problem with this Called Upcasting TextBalloon upperLeft tailEnd Message drawTail() ThoughtBalloon drawTail() SpeechBalloon drawTail()

Georgia Institute of Technology Run-time Binding of Methods In getFinalPicture() –Each element of the textBalloonList is told to draw And in draw each is told to drawTail() –What method is called for each of the two types of TextBalloons? The SpeechBalloon The ThoughtBalloon

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 TextBalloon

Georgia Institute of Technology Finding the drawTail method The draw method calls drawTail –Again the Java virtual machine will look for the method starting with the class that created the object the method was invoked on So for a SpeechBalloon object it will find it in SpeechBalloon And for a ThoughtBalloon object it will find it in ThoughtBalloon

Georgia Institute of Technology How it works ThoughtBalloon drawTail() SpeechBalloon drawTail() TextBalloon upperLeft tailEnd Message width draw(graphics g) drawTail() sBalloon:SpeechBalloon upperLeft = objRef tailEnd = objRef Message= objRef width = 100 class = objRef tBalloon:ThoughtBalloon upperLeft = objRef tailEnd = objRef Message= objRef width = 100 class = objRef

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 WhisperBalloon 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 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 SpeechBalloon extends TextBalloon –public class WhisperBalloon extends SpeechBalloon A whisper is still a type of speech

Georgia Institute of Technology Creating a Subclass Click on New and type the following public class WhisperBalloon extends SpeechBalloon { } Save it in WhisperBalloon.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(paramList) –As the first line of code in a constructor To invoke the parent’s constructor with the same parameterList 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 WhisperBalloon SpeechBalloon 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 WhisperBalloon extends SpeechBalloon { public WhisperBalloon (Point uLeft, int theWidth, Point tEnd, String theMessage) { super(uLeft,theWidth,tEnd,theMessage); }

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 drawBalloon –In drawBalloon Just need to set the font to a smaller and plain font after we create a WhisperBalloon 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 –drawBalloon –drawTail –drawText

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. –Some speech balloons have a black background and white text Pick at least one of these and implement it. –Present what you did and why

Georgia Institute of Technology ComicStrip Class Will display a comic strip with one or more ComicPanel’s in it. Modify the main method to create your own comic strip –You can use picture methods to make a black and white comic or one that looks like someone drew it in pencil

Georgia Institute of Technology Summary In analysis you try to understand the problem you are trying to solve –What are the 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