INHERITANCE AND OBJECTS. Fundamentals of Computer Science I

Similar documents
Inheritance and objects

Inheritance and objects

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

ITI Introduction to Computing II

ITI Introduction to Computing II

Interfaces and itera-on. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Java Object Oriented Design. CSC207 Fall 2014

Reusing Classes. Hendrik Speleers

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

The Nervous Shapes Example

CSCI 135 Final Fall Problem Points Score Total 100

Object-Oriented Programming Concepts

Example: Fibonacci Numbers

COURSE 2 DESIGN PATTERNS

OVERRIDING. 7/11/2015 Budditha Hettige 82

CS1150 Principles of Computer Science Objects and Classes

Logistics. Final Exam on Friday at 3pm in CHEM 102

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

GRAPHICS AND SOUND. Fundamentals of Computer Science I

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

G Programming Languages - Fall 2012

Inheritance and Polymorphism

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Inheritance and Interfaces

Instance Members and Static Members

Input and output thus far. Graphics and sound Input. New input/output capabilities. Output. StdDraw overview. StdDraw. StdAudio

Questions Answer Key Questions Answer Key Questions Answer Key

Using Inheritance to Share Implementations

Chapter 5: Classes and Objects in Depth. Information Hiding

Distributed Systems Recitation 1. Tamim Jabban

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS-202 Introduction to Object Oriented Programming

Questions Answer Key Questions Answer Key Questions Answer Key

INSTRUCTIONS TO CANDIDATES

Inheritance and Polymorphism

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Programming overview

Advanced Placement Computer Science. Inheritance and Polymorphism

ITI Introduction to Computing II

ITI Introduction to Computing II

Watch the following for more announcements

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

Principles of Object Oriented Programming. Lecture 4

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

CMSC 132: Object-Oriented Programming II

Domain-Driven Design Activity

The return Statement

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

core Advanced Object-Oriented Programming in Java

2. The object-oriented paradigm!

Advanced Object-Oriented. Oriented Programming in Java. Agenda. Overloading (Continued)

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Inheritance. Transitivity

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Exercise: Singleton 1

Data Structures and Other Objects Using C++

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Comments are almost like C++

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Computer Science II (20073) Week 1: Review and Inheritance

Abstract. 1. What is an ABSTRACT METHOD? 2. Why you would want to declare a method as abstract? 3. A non-abstract CLASS is called a concrete class

index.pdf January 21,

OOP: Key Concepts 09/28/2001 1

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CS 251 Intermediate Programming Inheritance

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Practice for Chapter 11

Inheritance: Definition

Inheritance Motivation

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

CSC207 Week 3. Larry Zhang

Chapter 5 Object-Oriented Programming

Chapter 2: Java OOP I

Distributed Systems Recitation 1. Tamim Jabban

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Java Session. Day 2. Reference: Head First Java

Introduction to Inheritance

Recursion 1. Recursion is the process of defining something in terms of itself.

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

COP 3330 Final Exam Review

Transcription:

INHERITANCE AND OBJECTS Fundamentals of Computer Science I

Outline Inheritance Sharing code between related classes Putting similar objects in the same bucket Extremely common in modern OOP languages Managing many objects Create class holding a collection of other objects Let's you simplify your main program Hides details of how you store things

Inheritance One class can extends another Parent class: shared vars/methods Child class: more specific vars/methods Class declared to extends the parent class Why? Lets you share code Repeated code is evil Why? Store similar objects in same bucket Can lead to simpler implementations 3

Inheritance Example Goal: Animate circles that bounce off the walls What does an object know? x-position, y-position x-velocity, y-velocity radius What can an object do? Draw itself Update its position, check for bouncing off walls 4

Bouncing Circle Class public class Circle private double x, y, vx, vy, r; public Circle(double x, double y, double vx, double vy, double r) this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.r = r; public void draw() StdDraw.setPenColor(StdDraw.RED); StdDraw.circle(x, y, r); public void updatepos() x += vx; y += vy; if ((x < 0.0) (x > 1.0)) vx *= -1; if ((y < 0.0) (y > 1.0)) vy *= -1; public double getx() return x; public double gety() return y; public double getradius() return r; 5

Bouncing Circle Client public class CircleClient public static void main(string[] args) Circle [] circles = new Circle[30]; for (int i = 0; i < circles.length; i++) circles[i] = new Circle(Math.random(), Math.random(), 0.002 - Math.random() * 0.004, 0.002 - Math.random() * 0.004, Math.random() * 0.1); while (true) StdDraw.clear(); for (int i = 0; i < circles.length; i++) circles[i].updatepos(); circles[i].draw(); StdDraw.show(10); 6

Inheritance Example Goal: Add images that bounce around What does an object know? x-position, y-position x-velocity, y-velocity radius image filename What can an object do? Draw itself Update its position, check for bouncing off walls 7

public class CircleImage private double x, y, vx, vy, r; private String image; public CircleImage(double x, double y, double vx, double vy, double r, String image) this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.r = r; this.image = image; public void draw() StdDraw.picture(x, y, image, r * 2, r * 2); public void updatepos() x += vx; y += vy; if ((x < 0.0) (x > 1.0)) vx *= -1; if ((y < 0.0) (y > 1.0)) vy *= -1; All this code appeared in the Circle class! Repeated code is evil! 8

Inheritance: Bouncing Circular Images! public class CircleImage extends Circle private String image; // image representing object public CircleImage(double x, double y, double vx, double vy, double r, String image) super(x, y, vx, vy, r); this.image = image; This class is a child of the Circle class public void draw() StdDraw.picture(getX(), gety(), image, getradius() * 2, getradius() * 2); Overridden version of draw() method, this one draws a picture scaled according to the radius. Calls the Circle constructor which sets all the other instance variables. NOTE: Need getter methods to get at private instance variables declared in parent. We only need our additional instance variable, others inherited from Circle Override = method with same method signature as parent's method Overload = multiple methods in same class with different signatures 9

Inheritance Example Goal: Add images that bounce and rotate What does an object know? x-position, y-position x-velocity, y-velocity radius image filename rotation angle What can an object do? Draw itself Update its position, check for bouncing off walls rotate image by one degree 10

Rotating Bouncing Circular Image Class public class CircleImageRotate extends CircleImage private int angle; // current rotation angle of image public CircleImageRotate(double x, double y, double vx, double vy, double r, String image) Calls the constructor of our super(x, y, vx, vy, r, image); parent class CircleImage. public void draw() Override the draw() method in our parent CircleImage. StdDraw.picture(getX(), gety(), getimage(), getradius() * 2, getradius() * 2, angle); public void updatepos() angle = (angle + 1) % 360; super.updatepos(); Calls the updatepos() in our parent's parent class Circle. 11

Unified Modeling Language (UML) Class Diagram 12

Client, 3 Object Types, Without Inheritance Goal: Bouncing circles, images, and rotating images Create three different arrays (tedious!) Circle [] circles1 = new Circle[10]; CircleImage [] circles2 = new CircleImage[10]; CircleImageRotate [] circles3 = new CircleImageRotate[10]; Fill in all three arrays (tedious!) for (int i = 0; i < circles1.length; i++) circles1[i] = new Circle(x, y, vx, vy, r); for (int i = 0; i < circles2.length; i++) circles2[i] = new CircleImage(x, y, vx, vy, r, "dont_panic_40.png"); for (int i = 0; i < circles3.length; i++) circles3[i] = new CircleImageRotate(x, y, vx, vy, r, "asteroid_big.png"); Loop through them separately (tedious!) for (int i = 0; i < circles1.length; i++) circles1[i].updatepos(); for (int i = 0; i < circles2.length; i++) circles2[i].updatepos(); for (int i = 0; i < circles3.length; i++) circles3[i].updatepos(); 13

Client, 3 Object Types, With Inheritance Circle [] circles = new Circle[30]; for (int i = 0; i < circles.length; i++) int rand = (int) (Math.random() * 3.0); double x = Math.random(); double y = Math.random(); double vx = 0.002 - Math.random() * 0.004; double vy = 0.002 - Math.random() * 0.004; double r = Math.random() * 0.1; With inheritance: Put them all together in one array! if (rand == 0) circles[i] = new Circle(x, y, vx, vy, r); else if (rand == 1) circles[i] = new CircleImage(x, y, vx, vy, r, "dont_panic_40.png"); else circles[i] = new CircleImageRotate(x, y, vx, vy, r, "asteroid_big.png"); while (true) StdDraw.clear(); for (int i = 0; i < circles.length; i++) circles[i].updatepos(); circles[i].draw(); StdDraw.show(10); 14

What Method gets Run? while (true) StdDraw.clear(); for (int i = 0; i < circles.length; i++) circles[i].updatepos(); circles[i].draw(); StdDraw.show(10); circles[i] could be: Circle, CircleImage or CircleImageRotate object Circle x, y, vx, vy, r draw() updatepos() image draw() CircleImage CircleImageRotate angle draw() updatepos() Rule: Most specific method executes. If the subclass has the desired method, use that. Otherwise try your parent. If not, then your parent's parent, etc. 15

Access Modifiers Access modifiers Controls if subclasses see instance vars/methods private = only the class itself public = everybody can see no modifier (default) = everybody in package protected = everybody in package, any class that extends it (even outside of package) Circle CircleImage CircleImageRotate private x, y, vx, vy, r image angle public draw() updatepos() draw() draw() updatepos() 16

Object Collections Goal: Simplify main, offload work to object that manages a collection of objects Helps hide implementation details You can change how you store things later Let's fix up the bouncing main() Introduce new class Bouncers Holds all the Circle type objects Update and draw them all at once 17

Simplified main Program Bouncers bouncers = new Bouncers(); for (int i = 0; i < 30; i++) bouncers.add(); while (true) StdDraw.clear(); bouncers.updateall(); bouncers.drawall(); StdDraw.show(10); public class Bouncers ----------------------------------------------------------------------- Bouncers() // Create an empty collection of bouncing objects void add() // Add a random type of bouncing object with a // random location, velocity, and radius void updateall() // Update the position of all bouncing objects void drawall() // Draw all the objects to the screen Application Programming Interface (API) for the Bouncers class. 18

Bouncer Implementation, 1/2 public class Bouncers private ArrayList<Circle> objs = new ArrayList<Circle>(); public void add() int rand = (int) (Math.random() * 3.0); double x = Math.random(); double y = Math.random(); double vx = 0.002 - Math.random() * 0.004; double vy = 0.002 - Math.random() * 0.004; double r = Math.random() * 0.1; I decided to use an ArrayList as my underlying data structure. Note: clients of Bouncers don't know this and don't really have to care.... if (rand == 0) objs.add(new Circle(x, y, vx, vy, r)); else if (rand == 1) objs.add(new CircleImage(x, y, vx, vy, r, "dont_panic_40.png")); else objs.add(new CircleImageRotate(x, y, vx, vy, r, "asteroid_big.png")); 19

Bouncer Implementation, 2/2 public void updateall() for (Circle obj : objs) obj.updatepos(); public void drawall() for (Circle obj : objs) obj.draw(); Perfect time to bust out the enhanced for loop. Much more succinct than looping over all the integer indexes. 20

Summary Object inheritance Share code between similar objects Can put objects related by inheritance into a single collection (array, ArrayList, etc.) Class holding collection of objects Helps simplify and contain logic