CIS 110: Introduction to computer programming

Similar documents
Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

ECE 122. Engineering Problem Solving with Java

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

More On inheritance. What you can do in subclass regarding methods:

Inheritance -- Introduction

CS-202 Introduction to Object Oriented Programming

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Lecture 10. Overriding & Casting About

Super-Classes and sub-classes

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CMSC 132: Object-Oriented Programming II. Inheritance

More about inheritance

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Inheritance and Polymorphism

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

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

C12a: The Object Superclass and Selected Methods

An example from Zoology

Last class. -More on polymorphism -super -Introduction to interfaces

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

CMSC 132: Object-Oriented Programming II

Questions Answer Key Questions Answer Key Questions Answer Key

Polymorphism. return a.doublevalue() + b.doublevalue();

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Java Object Oriented Design. CSC207 Fall 2014

COP 3330 Final Exam Review

Person class. A class can be derived from an existing class by using the form

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

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

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

Inheritance and Polymorphism

Implements vs. Extends When Defining a Class

Programming in C# Inheritance and Polymorphism

CS1150 Principles of Computer Science Objects and Classes

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Abstract class & Interface

CS313D: ADVANCED PROGRAMMING LANGUAGE

JAVA MOCK TEST JAVA MOCK TEST II

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Informatik II (D-ITET) Tutorial 6

Person class. A class can be derived from an existing class by using the form

Inheritance (Part 5) Odds and ends

Week 5-1: ADT Design

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

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

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Practice for Chapter 11

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 5: Inheritance

Programming Languages and Techniques (CIS120)

Making New instances of Classes

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

Inheritance, Polymorphism, and Interfaces

Inheritance & Polymorphism

INHERITANCE. Spring 2019

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Example: Count of Points

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Inheritance (IS A Relationship)

Lecture 18 CSE11 Fall 2013 Inheritance

Informatik II Tutorial 6. Subho Shankar Basu

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Programming Languages and Techniques (CIS120)

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Collections Algorithms

Inheritance, part 2: Subclassing. COMP 401, Spring 2015 Lecture 8 2/3/2015

CSC207 Quiz 1 Solutions Monday 6 February 2017, 12:15 PM. 1. (A) (B) (C) (D) (E) 7. Nothing, does not run true false

Programming Exercise 14: Inheritance and Polymorphism

OOP Subtyping. Lecture 20

ITI Introduction to Computing II

C08: Inheritance and Polymorphism

CS 2102 Exam 2 D-Term 2013

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CLASS DESIGN. Objectives MODULE 4

Introduction to Inheritance

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

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

PROGRAMMING LANGUAGE 2

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

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

Polymorphism and Inheritance

CS 251 Intermediate Programming Inheritance

ITI Introduction to Computing II

Chapter 3: Inheritance and Polymorphism

Basic Object-Oriented Concepts. 5-Oct-17

Chapter 6 Introduction to Defining Classes

Inheritance (continued) Inheritance

Transcription:

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 CIS 110 (11fa) - University of Pennsylvania 2

Inheritance 12/3/2011 CIS 110 (11fa) - University of Pennsylvania 3

Example: student and faculty records Consider parsing a file of student/faculty records. alur,faculty,35,levine 609,professor susan,student,18,3.9,junior zives,faculty,32,levine 566,associate lee,student,20,3.5,senior nenkova,student,21,4.0,freshman 12/3/2011 CIS 110 (11fa) - University of Pennsylvania 4

The Student class public class Student { private String username; private int age; private double gpa; private String status; public Student(String username, int age, double gpa, String status) { this.username = username; this.age = age; this.gpa = gpa; this.status = status; public String getusername() { return username; public int getage() { return age; public double getgpa() { return gpa; public String getstatus() { return status; 12/3/2011 CIS 110 (11fa) - University of Pennsylvania 5

The Faculty class public class Faculty { private String username; private int age; private String office; private String status; public Faculty(String username, int age, String office, String status) { this.username = username; this.age = age; this.office = office; this.status = status; public String getusername() { return username; public int getage() { return age; public String getoffice() { return office; public String getstatus() { return status; 12/3/2011 CIS 110 (11fa) - University of Pennsylvania 6

Class redundancy Student and faculty share fields and methods! username/age, getusername, getage How do we factor out class redundancy? Insight: a student and a faculty are related somehow 12/3/2011 CIS 110 (11fa) - University of Pennsylvania 7

The Person class public class Employee { // For simplicity's sake, let's just consider methods... public int getage() { return 20; public String getusername() { return "username"; 12/3/2011 CIS 110 (11fa) - University of Pennsylvania 8

Extending classes - inheritance We can extend the Person class to inherit its two methods. public class Student extends Employee { /** no impl yet */ Now we can call methods of the Employee class on Student objects. Student s = /*... */; s.getusername(); s.getage(); A class can only extend at most one other class! 12/3/2011 CIS 110 (11fa) - University of Pennsylvania 9

Inheritance of terminology public class Employee { /*... */ public class Student extends Employee { /*... */ We say that Student extends Employee. Student inherits Employee. Student derives from Employee. Student is a subclass of Employee. Employee is the superclass of Student. Employee is the parent class of Student. Student is-a Employee. Employee Student Inheritance hierarchy 12/4/2011 CIS 110 (11fa) - University of Pennsylvania 10

Adding onto the Student class public class Student extends Employee { public double getgpa() { return 4.0; public String getstatus() { return "Junior"; We can call four methods on a Student object. Two from Employee (getusername(), getage()). Two from Student (getgpa(), getstatus()). Inheritance allows for code sharing between classes. Only one of the benefits! 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 11

Inheriting state as well as behavior Let's add state back into the classes: public class Employee { private String username; private int age; public Employee(String username, int age) { this.username = username; this.age = age; public int getage() { return age; public String getusername() { return username; BAD!! DOES NOT COMPILE! public class Student extends Employee { private double gpa; private String status; public Student(double gpa, String status) { this.gpa = gpa; this.status = status; public double getgpa() { return gpa; public String getstatus() { return status; 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 12

Setting up your superclass public class Student extends Employee { private double gpa; private String status; public Student(String username, int age, double gpa, String status) { super(username, age); this.gpa = gpa; this.status = status; public double getgpa() { return gpa; public String getstatus() { return status; We "set up our parent" by calling its constructor with super( ). 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 13

Accessing inherited members Say I want to allow students to change their age (but not faculty). public class Student extends Employee { //... public void setage(int age) { this.age = age; BAD!! DOES NOT COMPILE! Employee's age field is marked private and thus is not visible from Student. 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 14

The protected modifier public = visible to everyone private = visible to only me (the class) protected = visible to me + all my subclasses public class Employee { //... protected int age; Allows flexibility at the cost of encapsulation 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 15

Overriding methods Say we want to prepend the username with the status of the student. Solution: let's override the behavior of getusername in Student. public class Employee { //... protected String username; public class Student extends Employee { //... public String getusername() { return status + ":" + username; 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 16

Invoking superclass methods However, say we don't want to expose write access to username to Student. Instead, let's invoke Employee's getusername method directly instead of making username protected! public class Employee { //... private String username; public class Student extends Employee { //... public String getusername() { return super.getusername() + ":" + username; "super" = invoke my parent's version of this method. 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 17

The Object class Object is the ultimate superclass for all other Java classes. Object String Employee Scanner Student 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 18

Important methods of the object class // Returns the String representation of this object public String tostring(); // Returns true if this object is equal to other public boolean equals(object other); public class Employee { public boolean equals(object other) { if (other instanceof Employee) { Employee e = (Employee) other; return username.equals(e.username) && age == e.age; else { return false; instanceof = binary operator that is true if other is the same class or a subclass of Employee 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 19

Polymorphism 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 20

Student is-a Employee When Student extends from Employee, we say Student is-a employee: Student is a specialization of Employee. Student has all the behavior and potentially more! Student has all of the functionality of Employee. 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 21

Polymorphism Because a Student is-a Employee, this works! Polymorphism: "many forms", the same code can be used with many types. Employee e = new Student(...); The static type of e, i.e., the type e is declared with The dynamic type of e, i.e., the actual type of the object. Intuition: Student does everything Employee can do (by virtue of extends) so we can use a Student where ever an Employee is expected. 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 22

Method calls and polymorphism What gets returned for these method calls? public class Employee { public String tostring() { return "Employee: " + username; Employee e1 = new Employee(...); Student s1 = new Student(...); Employee e2 = new Employee(...); e1.tostring(); s1.tostring(); e2.tostring(); public class Student extends Employee { public String tostring() { return "Student: " + getusername(); "Employee:..." "Student:..." "Student:..." 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 23

Dynamic dispatch When resolving a method call, we start with the dynamic (actual) type of the object. If that class defines the method, we invoke it. Otherwise, we repeat the process with its immediate superclass. Employee e = new Faculty(...); e.tostring(); "Employee:..." Employee Faculty 2. Does Employee define tostring? Yes! 1. Does Faculty define tostring? No! 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 24

To inherit or not to inherit Inheritance should be used when one class can be substituted for another. Really, class A is-a B class A extends B. E.g., a Circle IS-NOT a Point even though a circle is a point + a radius. Alternative: has-a relationship. A Circle has-a Point as a field. 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 25

Interfaces 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 26

extends isn't enough Sometimes we want to be able to have two or more is-a relationships for a class. e.g., employees that are comparable in addition to being people. Sometimes we don't need to share code. Only need to specify a set of "required" methods. 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 27

Introducing interfaces Interfaces allow us to specify the requirements for an is-a relationship without providing any implementation details. public interface Comparable { public int compareto(object other); public class Student extends Employee implements Comparable { //... public int compareto(object other) { /*... */ 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 28

The interface construct public interface Comparable { public int compareto(object other); Interfaces specify a series of abstract methods that a class must implement to, e.g., be Comparable. Classes can implement multiple interfaces (but only extend from a single class). Allows us to get polymorphism without code sharing. 12/5/2011 CIS 110 (11fa) - University of Pennsylvania 29