Programming Languages and Techniques (CIS120)

Size: px
Start display at page:

Download "Programming Languages and Techniques (CIS120)"

Transcription

1 Programming Languages and Techniques (CIS120) Lecture 26 March 26, 2015 Inheritance and Dynamic Dispatch Chapter 24

2 public interface Displaceable { public int getx(); public int gety(); public void move (int x, int y); public class Point implements Displaceable { private int x, y; public Point(int x0, int y0) { x = x0; y = y0; public int getx() { return x; public int gety() { return y; public void move(int dx, int dy) { x = x + dx; y = y + dy; What does this program do? public class Main { public static double dist(displaceable d) { int x = d.getx(); int y = d.gety(); return Math.sqrt(x * x + y * y); public static void main (String[] args) { Point p = new Point(3,4); System.out.println(dist(p)); 1. Nothing. It doesn't type check 2. It prints 5.0 on the console 3. NPE 4. Something else Answer: 2

3 Homework 7 due Tuesday Announcements Get the project set up on your laptop now, in case there are issues with Eclipse Exam: Great Job! view your exam with Ms. Fox starzng next Tuesday a[ernoon soluzons posted next week submit regrade requests by last day of classes Le]er grade ranges on Piazza post StaZsZcs: median: 87 mean: stdev: max: 100 (11)

4 Extension Sharing code between related types

5 Interfaces and Subtyping interfaces classes Types can have many different supertypes / subtypes Last Zme we saw interface extension: sharing code between interfaces Can we make it easier to share code in classes?

6 Class Extension Demo

7 Class Extension: Inheritance Classes, like interfaces, can also extend one another. Unlike interfaces, a class can extend only one other class. The extending class inherits all of the fields and methods of its superclass, and may include addizonal fields and methods. public class DisplaceableImpl implements Displaceable { private int x, y; public DisplaceableImpl(int x0, int y0) { this.x = x0; this.y = y0; public int getx() { return x; public int gety() { return y; public void move(int dx, int dy) { x = x + dx; y = y + dy;

8 Class Extension: Inheritance public class DisplaceableImpl implements Displaceable { private int x, y; public DisplaceableImpl(int x0, int y0) { this.x = x0; this.y = y0; public int getx() { return x; public class Circle extends DisplaceableImpl implements Displaceable, Area { private int radius; public Circle(int x0, int y0, int initradius) { super(x0,y0); radius = initradius; public double getarea () { return * radius * radius; Same extends keyword, different form of extension Constructors are not inherited

9 Simple Inheritance This is an example of simple inheritance, the subclass only adds new fields or methods Use simple inheritance to share common code among related classes Example: Point, Circle, and Rectangle have iden1cal behavior for getx(), gety(), and move() methods Inheritance captures the is a relazonship between objects (e.g. a Car is a Vehicle, a Circle is a DisplaceableThing) Class extension should never be used for the "has a" relazonship

10 Subtyping with Inheritance Interfaces Classes Displaceable Area DisplaceableImpl Shape Point Circle Rectangle Extends Implements - Type C is a subtype of D if D is reachable from C by following zero or more edges upwards in the hierarchy. - e.g. Circle is a subtype of Area, but Point is not

11 public class { boolean equals( o) { // test for equality String tostring() { // return a string representation // other methods omitted is the root of the class tree. Classes that leave off the extends clause implicitly extend Arrays also implement the methods of This class provides methods useful for all objects to support is the highest type in the subtyping hierarchy.

12 Subtyping classes (form a tree) interfaces Displaceable Area DisplaceableImpl Shape Point Circle Rectangle Extends Implements Subtype by fiat - Interfaces extend (possibly many) interfaces - Classes implement (possibly many) interfaces - Classes (except ) extend exactly one other class ( if implicit) - Interface types (and arrays) are subtypes by fiat of

13 Other forms of inheritance Java has other features related to inheritance (some of which we will discuss later in the course): A subclass might override (re- implement) a method already found in the superclass. A class might be abstract i.e. it does not provide implementazons for all of its methods (its subclasses must provide them instead) These features are hard to use properly and the need for them arises in special cases Special methods: equals and tostring Making reusable libraries We recommend avoiding all forms of inheritance (even simple inheritance ) when possible prefer interfaces and composizon. Especially avoid overriding.

14 The Java Abstract Stack Machine Dynamic Dispatch What code runs in a method call?

15 How do method calls work? What code gets run in a method invocazon? o.move(3,4); When that code is running, how does it access the fields of the object that invoked it? x = x + dx; What if the method was inherited from a superclass?

16 An Example public class { private int x; public () { x = 0; public void incby(int d) { x = x + d; public int get() { return x; public class extends { private int y; public (int inity) { y = inity; public void dec() { incby(-y); // somewhere in main: d = new (2); d.dec(); c = d; Answer: - 2 What result does this program calculate for x? NullPointerException

17 ASM refinement: The Class Table Class Table public class { private int x; public () { x = 0; public void incby(int d) { x = x + d; public int get() { return x; public class extends { private int y; public (int inity) { y = inity; public void dec() { incby(-y); The class table contains: The code for each method, Back pointers to each class s parent, and The class s stazc members. String tostring(){ boolean equals extends () { x = 0; void incby(int d){ extends (int inity) {

18 with Explicit this and super public class extends { private int x; public () { super(); this.x = 0; public void incby(int d) { this.x = this.x + d; public int get() { return this.x; public class extends { private int y; public (int inity) { super(); this.y = inity; public void dec() { this.incby(-this.y); // somewhere in main: d = new (2); d.dec(); c = d;

19 ConstrucZng an Workspace Stack Heap d = new (2); d.dec(); c = d; Class Table String tostring(){ boolean equals Invoking a constructor: allocates space for a new object in the heap includes slots for all fields of all ancestors in the class tree (here: x and y) creates a pointer to the class this is the object s dynamic type runs the constructor body a[er pushing parameters and this onto the stack extends () { x = 0; void incby(int d){ extends (int inity) {

20 AllocaZng Space on the Heap Workspace Stack Heap Class Table super(); this.y = inity; d = _; d.dec(); c = d; this x 0 y 0 String tostring(){ boolean equals inity 2 Invoking a constructor: allocates space for a new object in the heap includes slots for all fields of all ancestors in the class tree (here: x and y) creates a pointer to the class this is the object s dynamic type runs the constructor body a[er pushing parameters and this onto the stack Reminder: fields start with a sensible default - 0 for numeric values - null for references extends () { x = 0; void incby(int d){ extends (int inity) {

21 Calling super Workspace Stack Heap Class Table super(); this.y = inity; d = _; d.dec(); c = d; this x 0 y 0 String tostring(){ boolean equals inity 2 extends Call to super: The constructor (implicitly) calls the super constructor Invoking a method/constructor pushes the saved workspace, the method params (none here) and a new this pointer. () { x = 0; void incby(int d){ extends (int inity) {

22 Abstract Stack Machine Workspace Stack Heap Class Table super(); this.x = 0; d = _; d.dec(); c = d; this x 0 y 0 String tostring(){ boolean equals inity 2 _; this.y = inity; this extends (Running s default constructor omi]ed.) () { x = 0; void incby(int d){ extends (int inity) {

23 Assigning to a Field Workspace Stack Heap Class Table this.x = 0; d = _; d.dec(); c = d; this x 0 y 0 String tostring(){ boolean equals inity 2 _; this.y = inity; this extends Assignment into the this.x field goes in two steps: - look up the value of this in the stack - write to the x slot of that object. () { x = 0; void incby(int d){ extends (int inity) {

24 Assigning to a Field Workspace Stack Heap Class Table.x = 0; d = _; d.dec(); c = d; this x 0 y 0 String tostring(){ boolean equals inity 2 _; this.y = inity; this extends Assignment into the this.x field goes in two steps: - look up the value of this in the stack - write to the x slot of that object. () { x = 0; void incby(int d){ extends (int inity) {

25 Done with the call Workspace Stack Heap Class Table ; d = _; d.dec(); c = d; this x 0 y 0 String tostring(){ boolean equals inity 2 _; this.y = inity; this extends Done with the call to super, so pop the stack to the previous workspace. () { x = 0; void incby(int d){ extends (int inity) {

26 ConZnuing Workspace Stack Heap Class Table this.y = inity; d = _; d.dec(); c = d; ; this x 0 y 0 String tostring(){ boolean equals inity 2 extends ConZnue in the class s constructor. () { x = 0; void incby(int d){ extends (int inity) {

27 Abstract Stack Machine Workspace Stack Heap Class Table this.y = 2; d = _; d.dec(); c = d; this x 0 y 0 String tostring(){ boolean equals inity 2 extends () { x = 0; void incby(int d){ extends (int inity) {

28 Assigning to a field Workspace Stack Heap Class Table this.y = 2; d = _; d.dec(); c = d; this x 0 y 2 String tostring(){ boolean equals inity 2 extends Assignment into the this.y field. (This really takes two steps as we saw earlier, but we re skipping some for the sake of brevity) () { x = 0; void incby(int d){ extends (int inity) {

29 Done with the call Workspace Stack Heap Class Table ; d = _; d.dec(); c = d; this x 0 y 2 String tostring(){ boolean equals inity 2 extends Done with the call to the constructor, so pop the stack and return to the saved workspace, returning the newly allocated object (now in the this pointer). () { x = 0; void incby(int d){ extends (int inity) {

30 Returning the Newly Constructed Workspace Stack Heap Class Table d = ; d.dec(); c = d; x 0 y 2 String tostring(){ boolean equals ConZnue execuzng the program. extends () { x = 0; void incby(int d){ extends (int inity) {

31 AllocaZng a local variable Workspace Stack Heap Class Table d.dec(); c = d; d x 0 y 2 String tostring(){ boolean equals extends Allocate a stack slot for the local variable d. Note that it s mutable (bold box in the diagram). Aside: since, by default, fields and local variables are mutable, we o[en omit the bold boxes and just assume the contents can be modified. () { x = 0; void incby(int d){ extends (int inity) {

32 Dynamic Dispatch: Finding the Code Workspace Stack Heap Class Table.dec(); c = d; d x 0 y 2 String tostring(){ boolean equals extends Invoke the dec method on the object. The code is found by pointer chasing through the class hierarchy. This process is called dynamic dispatch: Which code is run depends on the dynamic class of the object. (In this case,.) Search through the methods of the, class trying to find one called dec. () { x = 0; void incby(int d){ extends (int inity) {

33 Dynamic Dispatch: Finding the Code Workspace Stack Heap Class Table this.incby(-this.y); d _; c = d; this x 0 y 2 String tostring(){ boolean equals extends Call the method, remembering the current workspace and pushing the this pointer and any arguments (none in this case). () { x = 0; void incby(int d){ extends (int inity) {

34 Reading A Field s Contents Workspace Stack Heap Class Table this.incby(-.y); d _; c = d; this x 0 y 2 String tostring(){ boolean equals extends Read from the y slot of the object. () { x = 0; void incby(int d){ extends (int inity) {

35 Dynamic Dispatch, Again Workspace Stack Heap Class Table.incBy(-2); d _; c = d; this x 0 y 2 String tostring(){ boolean equals Invoke the incby method on the object via dynamic dispatch. In this case, the incby method is inherited from the parent, so dynamic dispatch must search up the class tree, looking for the implementazon code. The search is guaranteed to succeed Java s stazc type system ensures this. Search through the methods of the, class trying to find one called incby. If the search fails, recursively search the parent classes. extends () { x = 0; void incby(int d){ extends (int inity) {

36 Running the body of incby Workspace Stack Heap Class Table this.x = this.x + d; d this.x = -2; _; int x = d.get(); this _; x -20 y 2 String tostring(){ boolean equals d -2 extends this () { x = 0; It takes a few steps Body of incby: - reads this.x - looks up d - computes result this.x + d - stores the answer (- 2) in this.x void incby(int d){ extends (int inity) {

37 A[er a few more steps Workspace Stack Heap Class Table c = d; d x -2 String tostring(){ y 2 boolean equals extends () { x = 0; Now use dynamic dispatch to invoke the get method for d. This involves searching up the class hierarchy again void incby(int d){ extends (int inity) {

38 A[er a few more steps Workspace Stack Heap Class Table d c x -2 y 2 String tostring(){ boolean equals extends () { x = 0; Now use dynamic dispatch to invoke the get method for c. This involves searching up the class hierarchy again void incby(int d){ extends (int inity) {

39 A[er yet a few more steps Workspace Stack Heap Class Table ; d c x -2 String tostring(){ x -2 y 2 boolean equals extends () { x = 0; void incby(int d){ Done! (Phew!) extends (int inity) {

40 Summary: this and dynamic dispatch When object s method is invoked, as in o.m(), the code that runs is determined by o s dynamic class. The dynamic class, represented as a pointer into the class table, is included in the object structure in the heap If the method is inherited from a superclass, determining the code for m might require searching up the class hierarchy via pointers in the class table This process of dynamic dispatch is the heart of OOP! Once the code for m has been determined, a binding for this is pushed onto the stack. The this pointer is used to resolve field accesses and method invocazons inside the code.

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 26 March 23, 2016 Inheritance and Dynamic Dispatch Chapter 24 Inheritance Example public class { private int x; public () { x = 0; } public void incby(int

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 25 November 1, 2017 Inheritance and Dynamic Dispatch (Chapter 24) Announcements HW7: Chat Client Available Soon Due: Tuesday, November 14 th at 11:59pm

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 25 March 18, 2013 Subtyping and Dynamic Dispatch Announcements HW07 due tonight at midnight Weirich OH cancelled today Help your TAs make the most

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 26 November 3, 2017 The Java ASM, Java Generics Chapter 24 Announcements HW7: Chat Server Available on Codio / InstrucNons on the web site Due Tuesday,

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 24 October 29, 2018 Arrays, Java ASM Chapter 21 and 22 Announcements HW6: Java Programming (Pennstagram) Due TOMORROW at 11:59pm Reminder: please complete

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 21 March 12, 2018 Java: Objects, Interfaces, Static Members Chapters 19 & 20 Announcements Java Bootcamp Tonight!! Towne 100, 6-8 pm HW06: Pennstagram

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 28 March 30, 2016 Collections and Equality Chapter 26 Announcements Dr. Steve Zdancewic is guest lecturing today He teaches CIS 120 in the Fall Midterm

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 24 March 24, 2014 Java ASM Interfaces and Subtyping Announcements HW 07 due tomorrow at midnight Exam 2, in class, a week from Friday (April 4th) The

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 22 March 14 th, 2016 Object Oriented Programming in Java Java Bootcamp tonight Announcements Monday, March 14 from 6-8pm in Levine 101 (Wu & Chen)

More information

CIS 120 Midterm II March 31, 2017 SOLUTIONS

CIS 120 Midterm II March 31, 2017 SOLUTIONS CIS 120 Midterm II March 31, 2017 SOLUTIONS 1 1. OCaml and Java (14 points) Check one box for each part. a. The following OCaml function will terminate by exhausting stack space if called as loop 10: let

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 22 March 14, 2018 Static Methods, Java Arrays Chapters 20 & 21 Announcements HW6: Java Programming (Pennstagram) Due: Tuesday the 20 th at 11:59pm

More information

CIS 120 Midterm II November 16, 2012 SOLUTIONS

CIS 120 Midterm II November 16, 2012 SOLUTIONS CIS 120 Midterm II November 16, 2012 SOLUTIONS 1 1. Java vs. OCaml (22 points) a. In OCaml, the proper way to check whether two string values s and t are structurally equal is: s == t s = t s.equals(t)

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id): CIS 120 Midterm II November 16, 2012 Name (printed): Pennkey (login id): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 24 March 18, 2016 The Java ASM What is the value of ans at the end of this program? Counter[] a = { new Counter(), new Counter() ; Counter[] b = {

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 31 April 3, 2013 Overriding, Equality, and Casts Announcements HW 09 due Tuesday at midnight More informajon about exam 2 available on Friday Unfinished

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

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

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism. Outline Inheritance Class Extension Overriding Methods Inheritance and Constructors Polymorphism Abstract Classes Interfaces 1 OOP Principles Encapsulation Methods and data are combined in classes Not

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Announcements) Programming)Languages)) and)techniques) (CIS120)) FirstWclass)funcTons)via)inner)classes) Anonymous)Inner)Classes) Lecture)26)

Announcements) Programming)Languages)) and)techniques) (CIS120)) FirstWclass)funcTons)via)inner)classes) Anonymous)Inner)Classes) Lecture)26) Programming)Languages)) and)techniques) (CIS120)) Lecture)26) March)28,)2014) )Extension) Announcements) HW08)(GUI)Programming)II))is)due)next)Tuesday)at) 11:59:59pm) Midterm(2(is(Friday,((April(4 th (in(class(

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 36 April 23, 2014 Overriding and Equality HW 10 has a HARD deadline Announcements You must submit by midnight, April 30 th Demo your project to your

More information

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1 Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,

More information

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

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit Object Oriented Programming Part 3 Writing Java with Eclipse and JUnit Today's Lecture Test Driven Development Review (TDD) Building up a class using TDD Adding a Class using Test Driven Development in

More information

CIS 120 Midterm II March 29, 2013 SOLUTIONS

CIS 120 Midterm II March 29, 2013 SOLUTIONS CIS 120 Midterm II March 29, 2013 SOLUTIONS 1 1. Java True/False (20 points) Circle T or F. a. T F In the Java ASM, object values are stored in the heap. b. T F In the Java ASM, method definitions are

More information

Reusing Classes. Hendrik Speleers

Reusing Classes. Hendrik Speleers Hendrik Speleers Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces Software development

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 20 Feb 29, 2012 Transi@on to Java II DON T PANIC Smoothing the transi@on Eclipse set- up instruc@ons in lab today/tomorrow First Java homework assignment

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 28 March 30, 2018 Overriding Methods, Equality, Enums, Iterators Chapters 25 and 26 Method Overriding When a subclass replaces an inherited method

More information

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis Principles of Software Construction: Objects, Design, and Concurrency Objects (continued) toad Spring 2012 Jonathan Aldrich Charlie Garrod School of Computer Science 2012 J Aldrich and W Scherlis Announcements

More information

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120e) Programming Languages and Techniques (CIS120e) Lecture 25 Nov. 8, 2010 ExcepEons and the Java Abstract Stack Machine Announcements Homework 8 (SpellChecker) is due Nov 15th. Midterm 2 is this Friday, November

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance Handout written by Julie Zelenski, updated by Jerry. Inheritance is a language property most gracefully supported by the object-oriented

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 12 Thomas Wies New York University Review Last lecture Modules Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic

More information

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

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

CSE 113 A. Announcements - Lab

CSE 113 A. Announcements - Lab CSE 113 A February 21-25, 2011 Announcements - Lab Lab 1, 2, 3, 4; Practice Assignment 1, 2, 3, 4 grades are available in Web-CAT look under Results -> Past Results and if looking for Lab 1, make sure

More information

Example: Count of Points

Example: Count of Points Example: Count of Points 1 class Point { 2... 3 private static int numofpoints = 0; 4 5 Point() { 6 numofpoints++; 7 } 8 9 Point(int x, int y) { 10 this(); // calling the constructor with no input argument;

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

Inheritance Motivation

Inheritance Motivation Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life

More information

Making New instances of Classes

Making New instances of Classes Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using

More information

Instance Members and Static Members

Instance Members and Static Members Instance Members and Static Members You may notice that all the members are declared w/o static. These members belong to some specific object. They are called instance members. This implies that these

More information

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 22. Inheritance Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Inheritance Object-oriented programming

More information

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120e) Programming Languages and Techniques (CIS120e) Lecture 33 Dec. 1, 2010 Equality Consider this example public class Point {! private final int x; // see note about final*! private final int y;! public Point(int

More information

Featherweight Java (FJ)

Featherweight Java (FJ) x = 1 let x = 1 in... x(1).!x(1) x.set(1) Programming Language Theory Featherweight Java (FJ) Ralf Lämmel This lecture is based on David Walker s lecture: Computer Science 441, Programming Languages, Princeton

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 37 April 22, 2016 Encapsulation & Hashing How is the Game Project going so far? 1. not started 2. got an idea 3. submitted design proposal 4. started

More information

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

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

CSE351 Winter 2016, Final Examination March 16, 2016

CSE351 Winter 2016, Final Examination March 16, 2016 CSE351 Winter 2016, Final Examination March 16, 2016 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. Please stop promptly at 4:20. There are 125 (not 100) points,

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini 24. Inheritance Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Superclasses and Subclasses Inheritance

More information

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java

More information

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

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G INHERITANCE & POLYMORPHISM P2 LESSON 12 P2 LESSON 12.1 INTRODUCTION inheritance: OOP allows a programmer to define new classes

More information

Example: Fibonacci Numbers

Example: Fibonacci Numbers Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers

More information

CS/ENGRD 2110 FALL Lecture 4: The class hierarchy; static components

CS/ENGRD 2110 FALL Lecture 4: The class hierarchy; static components 1 CS/ENGRD 2110 FALL 2016 Lecture 4: The class hierarchy; static components http://courses.cs.cornell.edu/cs2110 Announcements 2 e're pleased with how many people are already working on A1, as evidenced

More information

Computer Science 210: Data Structures

Computer Science 210: Data Structures Computer Science 210: Data Structures Summary Today writing a Java program guidelines on clear code object-oriented design inheritance polymorphism this exceptions interfaces READING: GT chapter 2 Object-Oriented

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 32 April 5, 2013 Equality and Hashing When to override: Equality Consider this example public class Point { private final int x; private final int

More information

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism IST311 Advanced Issues in OOP: Inheritance and Polymorphism IST311/602 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

More information

CIS 110: Introduction to computer programming

CIS 110: Introduction to computer programming CIS 110: Introduction to computer programming Lecture 25 Inheritance and polymorphism ( 9) 12/3/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline Inheritance Polymorphism Interfaces 12/3/2011

More information

OOP: Key Concepts 09/28/2001 1

OOP: Key Concepts 09/28/2001 1 OOP: Key Concepts Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu/~spring 09/28/2001 1 Overview of Part

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 28 November 8, 2017 Overriding Methods, Equality, Enums Chapter 26 Announcements HW7: Chat Server Available on Codio / InstrucNons on the web site

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 23 March 16, 2018 Arrays, Java ASM What is the value of ans at the end of this program? int[] a = {1, 2, 3, 4; int[] b = a; b[0] = 0; int ans = a[0];

More information

Generic programming POLYMORPHISM 10/25/13

Generic programming POLYMORPHISM 10/25/13 POLYMORPHISM Generic programming! Code reuse: an algorithm can be applicable to many objects! Goal is to avoid rewri:ng as much as possible! Example: int sqr(int i, int j) { return i*j; double sqr(double

More information

Compilers CS S-10 Object Oriented Extensions

Compilers CS S-10 Object Oriented Extensions Compilers CS414-2003S-10 Object Oriented Extensions David Galles Department of Computer Science University of San Francisco 10-0: Classes simplejava classes are equivalent to C structs class myclass {

More information

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass? 1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Recitation 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University Project 5 Due Wed, Oct. 22 at 10 pm. All questions on the class newsgroup. Make use of lab

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 8 Lecture 8-3: Encapsulation; Homework 8 (Critters) reading: 8.3-8.4 Encapsulation reading: 8.4 2 Encapsulation encapsulation: Hiding implementation details from clients.

More information

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes CS/ENGRD 2110 FALL 2017 Lecture 7: Interfaces and Abstract Classes http://courses.cs.cornell.edu/cs2110 1 Announcements 2 A2 is due tomorrow night (17 February) Get started on A3 a method every other day.

More information

Inheritance (Deitel chapter 9)

Inheritance (Deitel chapter 9) Inheritance (Deitel chapter 9) 1 2 Plan Introduction Superclasses and Subclasses protected Members Constructors and Finalizers in Subclasses Software Engineering with Inheritance 3 Introduction Inheritance

More information

Compiler construction 2009

Compiler construction 2009 Compiler construction 2009 Lecture 6 Some project extensions. Pointers and heap allocation. Object-oriented languages. Module systems. Memory structure Javalette restrictions Only local variables and parameters

More information

Object-oriented Programming. Object-oriented Programming

Object-oriented Programming. Object-oriented Programming 2014-06-13 Object-oriented Programming Object-oriented Programming 2014-06-13 Object-oriented Programming 1 Object-oriented Languages object-based: language that supports objects class-based: language

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 27 March 28, 2018 Java Generics Collections and Equality Chapters 25 & 26 Announcements HW7: Chat Server Available on Codio / Instructions on the web

More information

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

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance

More information

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

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 28 November 7, 2018 Overriding Methods, Equality, Enums, Iterators Chapters 25 and 26 Announcements HW7: Chat Server Available on Codio / Instructions

More information

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class

More information

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

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 9 Robert Grimm, New York University 1 Review Last week Modules 2 Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic

More information

Advanced Placement Computer Science. Inheritance and Polymorphism

Advanced Placement Computer Science. Inheritance and Polymorphism Advanced Placement Computer Science Inheritance and Polymorphism What s past is prologue. Don t write it twice write it once and reuse it. Mike Scott The University of Texas at Austin Inheritance, Polymorphism,

More information

Inheritance (Part 5) Odds and ends

Inheritance (Part 5) Odds and ends Inheritance (Part 5) Odds and ends 1 Static Methods and Inheritance there is a significant difference between calling a static method and calling a non-static method when dealing with inheritance there

More information

Type Hierarchy. Lecture 6: OOP, autumn 2003

Type Hierarchy. Lecture 6: OOP, autumn 2003 Type Hierarchy Lecture 6: OOP, autumn 2003 The idea Many types have common behavior => type families share common behavior organized into a hierarchy Most common on the top - supertypes Most specific at

More information

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Inheritance Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation

More information

Code Reuse: Inheritance

Code Reuse: Inheritance Object-Oriented Design Lecture 14 CSU 370 Fall 2008 (Pucella) Tuesday, Nov 4, 2008 Code Reuse: Inheritance Recall the Point ADT we talked about in Lecture 8: The Point ADT: public static Point make (int,

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (IS120) Lecture 30 April 4, 2016 Exceptions hapter 27 HW7: PennPals hat Due: Tuesday Announcements Simplified Example class { public void foo() {.bar(); "here in foo");

More information

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

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction Lecture 13: Object orientation Object oriented programming Introduction, types of OO languages Key concepts: Encapsulation, Inheritance, Dynamic binding & polymorphism Other design issues Smalltalk OO

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java 1 CS/ENGRD 2110 SPRING 2014 Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110 Java OO (Object Orientation) 2 Python and Matlab have objects and classes. Strong-typing nature of

More information

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed 1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every

More information

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

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism Ibrahim Albluwi Composition A GuitarString has a RingBuffer. A MarkovModel has a Symbol Table. A Symbol Table has a Binary

More information

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism Block 1: Introduction to Java Unit 4: Inheritance, Composition and Polymorphism Aims of the unit: Study and use the Java mechanisms that support reuse, in particular, inheritance and composition; Analyze

More information

Lecture Notes on Programming Languages

Lecture Notes on Programming Languages Lecture Notes on Programming Languages 85 Lecture 09: Support for Object-Oriented Programming This lecture discusses how programming languages support object-oriented programming. Topics to be covered

More information

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism rights reserved. 2 Motivation Suppose you want to define classes to model circles, rectangles, and

More information

Inheritance. OOP: Inheritance 1

Inheritance. OOP: Inheritance 1 Inheritance Reuse Extension and intension Class specialization and class extension Inheritance The protected keyword revisited Inheritance and methods Method redefinition An widely used inheritance example

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 37 April 28, 2014 TreeSet and HashSet Announcements Game project due Wednesday at midnight (HARD deadline) Check your grades online, should be up-

More information