Download presentation
Presentation is loading. Please wait.
Published byMeagan Moore Modified over 9 years ago
1
CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment up soon Next Monday is a Thursday - so discussion sections will meet then. Preregistration advice: for CS majors, premajors: you can get help/RAC code at mtg in CS BLDG 151 today, Wednesday, both at 4 pm
2
Recall: statement level / class level dichotomy Today: Inheritance - we’re at the class level If a class is a variant of another, existing class - call it A - we’d like to reuse A as much as we can for the implementation of the new class. When the new class IS-A (an example of) the original class, we call this process Inheritance. Sometime this extension is an obvious one: Person -> Student (add a gpa, year at school, student id, etc) Rectangle -> colored Rectangle (add a color) Vehicle -> motorized vehicle (yup: add a motor)
3
Let’s focus on a concrete example Suppose you have a Person class, with these attributes: String firstName String lastName String SSN int age And so forth (gender, address, city, zip, state, phone#..) ----- Now I want to create an Employee class, that’s just like a person, only an Employee has: an EmployeeNumber and a boolean tbShot
4
How can I do this? Note that: I don’t want to touch the Person part of the class - it’s been debugged, etc. What about the attributes of Person - can I see them? What about the methods of Person - how can I use them, and indeed can I use them at all What about the Employee constructor? ---- The big, big picture: programming is theft. A large OO program, say in Java, might use hundreds or even thousands of classes, a great many of which have been previously created, or are derived from classes that have been previously created
5
class Book { protected int pages = 1500; //---------------------------------------------------------------- // Prints a message about the pages of this book. //---------------------------------------------------------------- public void pageMessage () { System.out.println ("Number of pages: " + pages); }
6
class Dictionary extends Book { private int definitions = 52500; //----------------------------------------------------------------- // Prints a message using both local and inherited values. //----------------------------------------------------------------- public void definitionMessage () { System.out.println ("Number of definitions: " + definitions); System.out.println ("Definitions per page: " + definitions/pages); }
7
Terminology: Book is the base class, Dictionary is the derived class Book is the super class, Dictionary is the sub class Dictionary specializes Book
8
public class Words { //----------------------------------------------------------------- // Instantiates a derived class and invokes its inherited and // local methods. //----------------------------------------------------------------- public static void main (String[] args) { Dictionary webster = new Dictionary (); webster.pageMessage(); // an inherited method webster.definitionMessage(); }
9
class Book2 { protected int pages; public Book2 (int numPages) { pages = numPages; } //---------------------------------------------------------------- // Prints a message about the pages of this book. //---------------------------------------------------------------- public void pageMessage () { System.out.println ("Number of pages: " + pages); }
10
class Dictionary2 extends Book2 { private int definitions; public Dictionary2 (int numPages, int numDefinitions) { super (numPages); definitions = numDefinitions; } public void definitionMessage () { System.out.println ("Number of definitions: " + definitions); System.out.println ("Definitions per page: " + definitions/pages); }
11
public class Words2 { public static void main (String[] args) { Dictionary2 webster = new Dictionary2 (1500, 52500); webster.pageMessage(); webster.definitionMessage(); }
12
import element.*; import java.awt.Color; public class ColoredRect extends Rect{ private Color c; public ColoredRect(int x, int y, int width, int height, Color c){ super(x,y,width,height); this.c = c; } public Color getMyColor(){ return c; } public void setMyColor(Color clr){ c = clr; }
13
public void fillOut(DrawingWindow d){ d.setForeground(c); fillOn(d); d.setForeground(Color.white); } public String toString(){ return(super.toString() + '\n' + " my color is " + c); } }
14
import element.*; import java.awt.Color; public class Draw3{ public static void main(String[] args) { DrawingWindow d = new DrawingWindow(); ColoredRect r = new ColoredRect(30,40,50,60,Color.red); System.out.println(r); r.fillOut(d); } }
15
œ´œ ----jGRASP exec: java Draw3 œœßœ œœßœ my color is java.awt.Color[r=255,g=0,b=0]
16
import element.*; import java.awt.Color; public class DrawRects{ public static void main(String[] args) { DrawingWindow d = new DrawingWindow(); ColoredRect r; for(int j=0; j < 20; j++){ if (j%2 ==0) r = new ColoredRect(10*j,40,10,50,Color.red); else r = new ColoredRect(10*j,40,10,50,Color.blue); r.fillOut(d); } } }
18
import java.util.Random; public class Dice{ Random r; // aggregation - p 250 int[] scoreboard = new int[13]; public Dice(){ r = new Random(); initializeScoreboard(); } public void initializeScoreboard(){ for(int j = 0; j < 13; j++) scoreboard[j] = 0; } public int tossDie(){ return (1+r.nextInt(6)); } public int throwDice(){ return(tossDie() + tossDie()); }
19
import java.util.Random; public class Dice2 extends Random{ int[] scoreboard = new int[13]; public Dice2(){ super(); // note call to super initializeScoreboard(); } public void initializeScoreboard(){ for(int j = 0; j < 13; j++) scoreboard[j] = 0;} public int tossDie(){ return (1+nextInt(6)); } // notice nextInt call.. public int throwDice(){ return(tossDie() + tossDie()); }
20
Let’s try these: ComicBook extends Book2 [a book with panels..] MultiCoinFlipper extends Random [ has a number of coins] Employee extends Person [add empnumber, tbshot]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.