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.

Slides:



Advertisements
Similar presentations
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Advertisements

CS 121 – Intro to Programming:Java - Lecture 9 Announcements Two new Owl assignments up - they’re a bit more challenging. Programming assignment 5 is due.
Inheritance Inheritance allows the derivation of a new class from an existing one, for the purpose of reuse, enhancement, adaptation, etc. superclass (a.k.a.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
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 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Intro to OOP with Java, C. Thomas Wu
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
CS 121 – Intro to Programming:Java - Lecture 5 Announcements Course home page: Owl due Thursday at 11; another one up today. Third programming assignment.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Static class members.
1 Chapter 5 - Object-Oriented Programming 1 What is inheritance? Object-oriented systems allow classes to be defined in terms of other classes. Classes.
COS 312 DAY 13 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress Over due – Next progress report is March 26 Assignment 4 Posted – Chap 6 & 7 – Due.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CS 121 – Intro to Programming:Java - Lecture 8 Announcements Current Owl assignment is due Wednesday. Next Owl assignment - arrays I - will be up by tomorrow.
Inheritance  Inheritance is a fundamental object-oriented technique  it enhances software design and promotes reuse  We will focus on:  deriving new.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Inheritance Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
COT int[] x = {3,7,2,4,1}; int[] y = {5,8,6,9}; x = y; x[2] = 0; for (int k: y) { System.out.print(k + " "); }
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
CS 121 – Intro to Programming:Java - Lecture 12 Announcements New Owl assignment up soon (today?) For this week: read Ch 8, sections 0 -3 Programming assignment.
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
1Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and promotes reuse  We will focus on:
Inheritance Chapter 8 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CS 121 – Intro to Programming:Java - Lecture 11 Announcements Inheritance Owl assignment due 11/30 at 11 am Programming assignment six due Friday 12/3.
1 More About Derived Classes and Inheritance Chapter 9.
Lecture 12 Inheritance.
Ch 10- Advanced Object-Oriented Programming Features
Chapter 8 Inheritance.
03/10/14 Chapter 9 Inheritance.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Announcements & Review
Chapter 11 Inheritance and Polymorphism
Presentation transcript:

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

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)

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

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

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); }

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); }

Terminology: Book is the base class, Dictionary is the derived class Book is the super class, Dictionary is the sub class Dictionary specializes Book

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(); }

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); }

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); }

public class Words2 { public static void main (String[] args) { Dictionary2 webster = new Dictionary2 (1500, 52500); webster.pageMessage(); webster.definitionMessage(); }

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; }

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); } }

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); } }

œ´œ ----jGRASP exec: java Draw3 œœßœ œœßœ my color is java.awt.Color[r=255,g=0,b=0]

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); } } }

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()); }

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()); }

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]