CIS 110: Introduction to computer programming

Slides:



Advertisements
Similar presentations
CS 211 Inheritance AAA.
Advertisements

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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Computer Science and Engineering College of Engineering The Ohio State University Lot More Inheritance and Intro to Design Patterns Lecture 12.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Lecture 12 Inheritance.
Object-oriented Programming in Java
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
One class is an extension of another.
03/10/14 Inheritance-2.
Advanced Programming in Java
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
One class is an extension of another.
CSC 143 Inheritance.
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Overriding Methods & Class Hierarchies
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

CIS 110: Introduction to computer programming Lecture 25 Inheritance and polymorphism (§ 9) 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Inheritance 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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; } 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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; } 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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… 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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"; } } 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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! 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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! 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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; 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; BAD!! DOES NOT COMPILE! 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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(…). 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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. 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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… 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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; } } 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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; } "super" = invoke my parent's version of this method. public class Student extends Employee { // ... public String getUsername() { return super.getUsername() + ":" + username; } 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania The Object class Object is the ultimate superclass for all other Java classes. Object String Scanner Employee … … Student 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Polymorphism 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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. 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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. 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

Method calls and polymorphism What gets returned for these method calls? public class Employee { public String toString() { return "Employee: " + username; } public class Student extends Employee { public String toString() { return "Student: " + getUsername(); } Employee e1 = new Employee(...); Student s1 = new Student(...); Employee e2 = new Employee(...); e1.toString(); s1.toString(); e2.toString(); "Employee: ..." "Student: ..." "Student: ..." 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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 2. Does Employee define toString? Yes! Employee e = new Faculty(...); e.toString(); "Employee: ..." Faculty 1. Does Faculty define toString? No! 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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. 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Interfaces 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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. 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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) { /* ... */ } } 8/22/2019 CIS 110 (11fa) - University of Pennsylvania

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. 8/22/2019 CIS 110 (11fa) - University of Pennsylvania