CPSC 111 Introduction to Computation Introduction to Computation November 17 rd , 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger
Department of Computer Science Undergraduate Events Events this week Technical Interview Skills Workshop CSSS Movie Night Workshop Leader: Sabina Nawaz Movies: The Hangover & Star Trek Movies: The Hangover & Star Trek Date: Mon., Nov 16 Date: Thurs., Nov 19 Time: 5 – 6:30 pm Time: 6 ‐ 10 pm Location: FSC 1005, 2424 Main Mall Location: DMP 301 Cool Things: Entrance FREE! One FREE CS Distinguished Lecture Series popcorn and pop for every attendee! Speaker: Glenn Entis Date: Thurs Nov 19 Date: Thurs., Nov 19 Cranium and Board Games Night – ‘Byte ‘ Time: 3:30 ‐ 4:50 pm to Eat’ Fundraising Location: DMP 110 Date: Fri., Nov 20 Time: 5 6 pm Time: 5 – 6 pm Masters of Digital Media Program Location: X ‐ Wing Undergraduate Info Session Student Lounge Date: Thurs., Nov 19 Time: 5 – 6 pm Time: 5 6 pm Location: X ‐ Wing Undergraduate Student Lounge.
Administrative Stuff Assignment 3 is coming up (keep an eye on Vista) Vista) We are trying hard to have the marks for your We are trying hard to have the marks for your midterm 2 ready this week Unlikely that you’ll get the exam papers this week week
Administrative Stuff Big reading in Big Java 3rd edition : Chapter 1 1 through 1 8 (introduction) Chapter 1.1 through 1.8 (introduction) Chapter 2.1 through 2.10 (using objects) Chapter 3.1 through 3.8 (implementing classes) Chapter 4 (data types) Chapter 4 (data types) Chapter 5.1 through 5.4 (decisions) Chapter 6.1 through 6.5 (iteration) Chapter 7.1, 7.5, 7.6, 7.7 (arrays) Chapter 14.1, 14.3 (searching and sorting) Chapter 8.1 through 8.9 (designing classes) Chapter 9.1, 9.2, 9.3 (interfaces and polymorphism) Chapter 10 (inheritance) Chapter 2.11, 2.12 (simple graphics) Chapter 9.5, 9.6, 9.7, 9.8, 10.9, 10.10 (event handling and graphical user interfaces) d hi l i t f )
Administrative Stuff Big reading in Big Java 2nd edition : Chapter 1.1 through 1.8 (introduction) Ch t 1 1 th h 1 8 (i t d ti ) Chapter 2.1 through 2.10 (using objects) Chapter 3.1 through 3.8 (implementing classes) Chapter 4 (data types) Chapter 4 (data types) Chapter 6.1 through 6.4 (decisions) Chapter 7.1 through 7.5 (iteration) Chapter 8.1, 8.5, 8.6, 8.7 (arrays) p , , , ( y ) Chapter 19.1, 19.3 (searching and sorting) Chapter 9.1 through 9.9 (designing classes) Chapter11.1, 11.2, 11.3 (interfaces and polymorphism) Chapter 13 (inheritance) Chapter 5.1, 5.2 (simple graphics) Chapter 11.5, 12.1, 12.2, 12.3 (event handling and graphical user interfaces) d h l f )
So where are we? So where are we? • We learned We learned – programming language basics – about classes – about conditionals (if statements) – about loops p – about arrays – Sorting and searching efficiently • We started looking at how we can reuse our code: one of the main strengths of OOP
OOP is reuse Object ‐ oriented programming is largely about reuse. OOP lets us reuse a class definition to create lots of instances (objects) of that class. Otherwise, we'd be writing the same program over and over and over. (we have seen this) But OOP also lets us reuse class definitions to help with the creation of new, related but different class definitions ( inheritance ). ( ) OOP even lets us use incomplete class definitions to help with the creation of other class definitions. That also makes it so that we don't have to write more code than we need to ( interfaces ). Why is this important? It saves us (programmers) a lot of time when we're managing the development and maintenance of big h ' i th d l t d i t f bi software projects.
Roller Coaster flashback Our very first generation of roller coasters were limited in simulated functionality: O fi t ti f ll t li it d i i l t d f ti lit folks could board roller coasters, but I had no way to let riders get off. That is, the simulated roller coasters had board() methods, but no empty() methods. public class RollerCoaster{ private int numberOfRiders; private int capacity; public RollerCoaster(int aCapacity) { numberOfRiders = 0; capacity = aCapacity; System.out.println("Another ride is ready to go!"); } //we also had another constructor with no parameters // but we’ll ignore it for this discussion public void board() { if (numberOfRiders < capacity) { numberOfRiders = numberOfRiders+1; System.out.println("Welcome on board!"); } else{ else{ System.out.println("Sorry, the coaster’s full"); } }}
Roller Coaster flashback An evolutionary step beyond these primitive roller coasters is the class of roller An evolutionary step beyond these primitive roller coasters is the class of roller coasters that let people get off. These new roller coasters were exactly the same as their predecessors in all other respects. public class RollerCoaster2{ public class RollerCoaster2{ private int numberOfRiders; private int capacity; public RollerCoaster2(aCapacity) { numberOfRiders = 0; capacity = aCapacity; System.out.println("Another ride is ready to go!"); } public void board() { if (numberOfRiders < capacity){ numberOfRiders = numberOfRiders+1; System.out.println("Welcome on board!"); } else{ System.out.println("Sorry, the coaster’s full"); } } public void empty() { numberOfRiders = 0; }}
Now, at this point I have • RollerCoaster, which has a board() method, and • RollerCoaster2, which is identical, with the same board() method, but also has a empty() method has a empty() method. We saw that we can use the two classes together to create roller coasters of these two different types in the same method coasters of these two different types in the same method Not necessarily bad, but not very efficient, nor scalable. For example, both RollerCoaster and RollerCoaster2 have copies of exactly the same board() method. • What if I later find out that there's a bug in board()? • I have to fix it twice. The problem grows if I create other classes from the basic Roller Coaster class
The better way (a first pass) public class ShinyRollerCoaster extends RollerCoaster { } What does that do? We just created a new class called ShinyRollerCoaster that inherited most non ‐ static methods and non ‐ static variables from the RollerCoaster class. To enhance ShinyRollerCoaster beyond this, we just add the additional variables and methods that we want but that aren't included in the RollerCoaster class. Inheritance is the process by which a new class is derived from an existing one. It is a fundamental principle or defining characteristic of object ‐ oriented programming. i
Inheritance and Constructors The subclass (or child class) inherits all methods except constructor methods from the superclass (or parent class). To use the constructor methods from the superclass, we need to use the To use the constructor methods from the superclass, we need to use the reserved word super. public class ShinyRollerCoaster extends RollerCoaster { public ShinyRollerCoaster(int aCapacity) { super(aCapacity);}} The reserved word super, in the context of a constructor method for a subclass, tells Java to use the appropriate constructor method of the subclass, tells Java to use the appropriate constructor method of the superclass.
Let’s see how it works public class ShinyThemePark { public static void main( String[] args ) { System.out.println("Building my theme park"); ShinyRollerCoaster thunderbolt = new ShinyRollerCoaster(2); RollerCoaster flightDeck = new RollerCoaster(2); thunderbolt.board(); thunderbolt.board(); //thunderbolt.empty();// not defined yet thunderbolt.board(); flightDeck.board(); flightDeck.board(); } } Building my theme park A 2 person ride is ready to go A 2 person ride is ready to go Welcome on board! Welcome on board! Sorry, the coaster’s full Welcome on board! Welcome on board!
So where are we? So where are we? • We learned – programming language basics – about classes – about conditionals (if statements) b t diti l (if t t t ) – about loops – about arrays abou a ays – Sorting and searching efficiently • Today we’ll – Talk a bit more about inheritance – Take a look at another construct that allows for code reuse: interfaces reuse: interfaces
The better way (a third pass) Let’s finally refine our ShinyRollerCoaster class to include the empty() method public class ShinyRollerCoaster extends RollerCoaster { public ShinyRollerCoaster(int aCapacity) p y ( p y) { super(aCapacity); } public void empty () { numberOfRiders = 0; } } } } And let’s see how it works
Oops Let’s finally refine our ShinyRollerCoaster class to include the empty() method public class ShinyRollerCoaster extends RollerCoaster { public ShinyRollerCoaster(int aCapacity) p y ( p y) { super(aCapacity); } public void empty () { numberOfRiders = 0; } } } } � Error: ShinyRollerCoaster. java:11: numberOfRiders has private access in RollerCoaster
The better way (a third pass) Let’s finally refine our ShinyRollerCoaster class to include the empty() method public class ShinyRollerCoaster extends RollerCoaster { public ShinyRollerCoaster(int aCapacity) p y ( p y) { super(aCapacity); } public void empty () { numberOfRiders = 0; } } } } The subclass will inherit all the non ‐ static variables of the superclass, but the access control on private variables is so tight that a subclass won't be able to directly access an inherited private variable
Recommend
More recommend