Pulling out a common superclass Barb Ericson Georgia Tech.

Slides:



Advertisements
Similar presentations
GridWorld Case Study Part 2 Bug Variations A Summary by Jim Mims.
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Breakout in Greenfoot Barb Ericson
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Program: Little Crab Mr Gano.
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.
– Advanced Programming P ROGRAMMING IN Lecture 16 Interfaces.
H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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,
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
Abstract Classes and Interfaces
UML Basics & Access Modifier
Abstract classes and Interfaces. Abstract classes.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
GridWorld Case Study1 Barbara Ericson Georgia Tech Jan 2008.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
GridWorld Case Study Barbara Ericson March 24, 2007.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
The GridWorld Case Study Program Section 1 - Overview of the Class Hierarchies Section 2 - The Actor Class Section 3 - The Rock and Flower Classes Section.
Interfaces and Polymorphism CS 162 (Summer 2009).
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
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.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
Module 9. Dealing with Generalization Course: Refactoring.
JAVA CLASSES, METHODS AND VARIABLES.  Defined Noun: A set or category of things having some property or attribute in common and differentiated from others.
Further Abstraction Techniques Chapter 10. Abstract Classes There are times when you do not want someone to be able to make an object of your class. For.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part1 Barb Ericson Georgia Institute of.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance and Encapsulation
Barb Ericson Georgia Institute of Technology June 2007
Barbara Ericson Georgia Tech Jan 2008
Barb Ericson Georgia Institute of Technology May 2006
Inherited Classes in Java
תירגול 9 אובייקטים.
Problem Solving with Data Structures using Java: A Multimedia Approach
Breakout in Greenfoot Barb Ericson
Chapter 14 Abstract Classes and Interfaces
More on Creating Classes
GridWorld Case Study.
More on Creating Classes part 3
Unit 3 - Introduction to Inheritance - Example
CSG2H3 Object Oriented Programming
Presentation transcript:

Pulling out a common superclass Barb Ericson Georgia Tech

Common Fields in Bug Classes BoxBug – Has fields sideLength and steps SpiralBug – Has fields sideLength and steps ZBug – Has fields sideLength and steps BoxBug int sideLength int steps SpiralBug int sideLength int steps ZBug int sideLength int steps

Problem Should add public accessor (getter) and modifiers (setter) methods for each of these fields – Needed in BoxBug – Needed in SpiralBug – Needed in Zbug This means adding the same code to 3 classes! BoxBug int sideLength int steps SpiralBug int sideLength int steps ZBug int sideLength int steps

A Better Way Pull out a common parent class with the common fields in it – This is called generalization PatternBug extends Bug – Has fields of sideLength and steps – Add the getter and setter methods to PatternBug – Change BoxBug, SpiralBug, and ZBug to inherit from PatternBug BoxBug SpiralBug ZBug PatternBug int sideLength int steps getSideLength getSteps setSteps setSideLength

PatternBug Inherits from Bug Has the private fields – steps and sideLength Has public accessors (getters) for the fields – getSteps and getSideLength Has public modifiers/mutators (setters) for the fields – setSteps and setSideLength

Changes to Bug Subclasses Subclass PatternBug instead of Bug Since fields are private we no longer have direct access to the inherited fields – Use getSteps instead of this.steps – Use getSideLength instead of this.sideLength – Use setSteps instead of this.steps = – Use setSideLength instead of this.sideLength =

Challenge Create the PatternBug class and modify the BoxBug, ZBug, and SpiralBug to inherit from that class instead Test that the modified classes still work Turn in the PatternBug class and the modified classes

Breakout Example If you want the ball to change color to the color of the brick that it hit – You can just add a public getColor method to the Brick class – And then set the ball color to the brick color and call updateImage to redraw the image with the new color Using the setColor method in Ball

Breakout Classes Ball inherits from Actor – Has a color – Has a setColor method Brick inherits from Actor – Has a color – Needs a getColor method Paddle inherits from Actor – Has a color

Problem Should add public accessor (getter) and modifiers (setter) methods for the color field to all the classes that have a color field This means adding the same code to 3 classes! Could copy the setColor method in Ball to the others – But copying code is tedious and can lead to more fixing if you find an error in the code Brick Color color Ball Color color Paddle Color color setColor

A Better Way Pull out a common parent class with the common fields in it – This is called generalization – Each of the actors is drawn as an image and has a color DrawnActor extends Actor – Has a color field – Add the getter and setter methods to DrawnActor – Change Brick, Ball, and Paddle to inherit from DrawnActor Brick Ball Paddle DrawnActor Color color getColor setColor updateImage

Another Problem setColor calls updateImage to redraw the image of the actor with the new color Each of Ball, Brick, and Paddle have an updateImage method – But they do something different public void updateImage() { GreenfootImage image = new GreenfootImage(width,width); image.setColor(color); image.fillOval(0,0,width,width); setImage(image); } public void updateImage() { GreenfootImage image = new GreenfootImage(width,height); image.setColor(this.color); image.fillRect(0,0,width,height); setImage(image); } From Ball From Brick

Use abstract! We can add an abstract method to DrawnActor – Abstract methods can't have any method body public abstract void updateImage(); This means the class DrawnActor must also be declared abstract public abstract class DrawnActor extends Actor

Abstract Classes You can not create an object of an abstract class – Can't say new DrawnActor() – Get an error You can create subclasses of abstract classes – This is what they are used for – The subclasses must provide the body for the methods that are declared abstract or else also be declared abstract Or you will get a compiler error if you try to compile the class

Challenge Create a subclass of Actor called DrawnActor – Make it an abstract class With an abstract method updateImage() – Put in the field color and the setColor and getColor methods In setColor call updateImage – Modify Brick, Ball, and Paddle to inherit from DrawnActor instead of Actor Must use setColor and getColor instead of this.color – Will need to cast ((DrawnActor) actor).getColor(); The compiler only knows that the intersecting actor (brick) is an actor not that it is a DrawnActor so we cast to tell it.