C08: Inheritance and Polymorphism

Similar documents
C08: Inheritance and Polymorphism

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

What is Inheritance?

C09: Interface and Abstract Class and Method

C18a: Abstract Class and Method

C10: Garbage Collection and Constructors

CMSC 132: Object-Oriented Programming II. Inheritance

C11: Garbage Collection and Constructors

C09: Interface, and Abstract Class and Method

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

Practice for Chapter 11

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

Java Object Oriented Design. CSC207 Fall 2014

Week 5-1: ADT Design

More on Inheritance. Interfaces & Abstract Classes

Inheritance and Polymorphism

COMP 401 Spring 2013 Midterm 2

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

C12a: The Object Superclass and Selected Methods

Lecture 18 CSE11 Fall 2013 Inheritance

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

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

C02: Overview of Software Development and Java

Create a Java project named week10

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

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Inheritance -- Introduction

Classes and Inheritance Extending Classes, Chapter 5.2

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

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

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

C++ Important Questions with Answers

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

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

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

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

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

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

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

ECE 122. Engineering Problem Solving with Java

JAVA MOCK TEST JAVA MOCK TEST II

CIS 110: Introduction to computer programming

Java Magistère BFA

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

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

CISC370: Inheritance

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

INHERITANCE AND EXTENDING CLASSES

Overriding Variables: Shadowing

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS-202 Introduction to Object Oriented Programming

Binghamton University. CS-140 Fall Dynamic Types

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

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

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

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

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

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

ECE 122. Engineering Problem Solving with Java

CS 251 Intermediate Programming Inheritance

Arrays Classes & Methods, Inheritance

Chapter 14 Abstract Classes and Interfaces

CS 112 Introduction to Programming

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

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

Admin. q Admin and recap q Class inheritance

Programming overview

Inheritance and Polymorphism

Inheritance (continued) Inheritance

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

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

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

CLASS DESIGN. Objectives MODULE 4

ITI Introduction to Computing II

CST141--Inheritance 2

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

Software and Programming 1

Polymorphism. Arizona State University 1

Abstract class & Interface

Informatik II (D-ITET) Tutorial 6

Programming in C# Inheritance and Polymorphism

COMP 401 Spring 2013 Midterm 2

ITI Introduction to Computing II

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

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

Object-Oriented Programming More Inheritance

Abstract Classes and Interfaces

CMSC 132: Object-Oriented Programming II

Object Oriented Programming

Object Oriented Programming

C++ Inheritance and Encapsulation

Inheritance, Polymorphism, and Interfaces

C06: Java API & Libraries

Polymorphism. Agenda

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Transcription:

CISC 3120 C08: Inheritance and Polymorphism Hui Chen Department of Computer & Information Science CUNY Brooklyn College 9/19/2017 CUNY Brooklyn College 1

Outline Recap and issues Project progress? Practice assignments? CodeLab? Review guide? Inheritance Polymorphism via inheritance Type casting Assignments 9/19/2017 CUNY Brooklyn College 2

Class and Type A class defines a type, and often models a set of entities Build a system for managing Brooklyn College, we consider People, a set of individuals (objects), modeled as a class that defines the set of objects People at Brooklyn College 9/19/2017 CUNY Brooklyn College 3

Subtypes Some people at Brooklyn are different from the others in some way Professors and students are subtypes of Brooklyn College People Professors Students People at Brooklyn College 9/19/2017 CUNY Brooklyn College 4

Type Hierarchy Characteristics and behavior What are Students and Professors in common? What are Students and Professors different? People at Brooklyn College Professors Students 9/19/2017 CUNY Brooklyn College 5

What s in common? What characteristics (attributes) and behavior (actions) do People at Brooklyn College have in common? Characteristics (attributes): name, ID, address, email, phone, Behavior (actions): change address, apply parking, 9/19/2017 CUNY Brooklyn College 6

What s Special? What s distinct about students? Characteristics (attributes): classes taken, tuition and fees, Behavior (actions): add class, drop class, pay tuition, What s distinct about professors? Characteristics (attributes): course taught, rank, title, Behavior (actions): register grade, apply promotion, 9/19/2017 CUNY Brooklyn College 7

Inheritance & Type Hierarchy A subtype (child) inherits characteristics (attributes) and behavior (actions) of its base type (parent) - Class taught, - Register grades, People at Brooklyn College - Name, ID, address, phone, - Change address, apply parking Professors - Class taken, - Add class, Students 9/19/2017 CUNY Brooklyn College 8

Questions Concepts of Type, subtype, class, subclass Inheritance 9/19/2017 CUNY Brooklyn College 9

Super Type (Super Class): Person public class Person { protected String name; protected String id; protected String address; public Person(String name, String id, String address) { this.name = name; this.id = id; } public void changeaddress(string address) { } } 9/19/2017 CUNY Brooklyn College 10

Subtype (Subclass): Student public Student extends Person { private ArrayList<String> classestaken; public Student(String name, String id, String address) { } super(name, id, address); classestaken = new ArrayList<String>(); public void havetakenclass(string classname) { } public void showclassestaken() { } } 9/19/2017 CUNY Brooklyn College 11

Subtype (Subclass): Professor public class Professor extends Person { private final static int SABATTICAL_LEAVE_INTERVAL = 7; private int yearstarted; public Professor(String name, String id, String address, int yearstarted) { super(name, id, address); this.yearstarted = yearstarted; } public void applysabbatical(int applicationyear) { } } 9/19/2017 CUNY Brooklyn College 12

Control Access to Members protected String name; Modifier Class Package Subclass World public Yes Yes Yes Yes protected Yes Yes Yes No (no modifier) Yes Yes No No private Yes No No No 9/19/2017 CUNY Brooklyn College 13

Choose Access Control Level Goal: you want to reduce the chances your class is being misused. Access levels is to help achieve it. Use private unless you have a good reason not to. Use the most restrictive access level that makes sense for a particular member. Avoid public fields except for constants. (Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.) 9/19/2017 CUNY Brooklyn College 14

Constructors Initialize attributes of an object when it is being created (or instantiated) Subclass s constructor Java will call the parent class s default constructor if you do not call one of parent s constructors explicitly. You may explicitly call it via super( ). super(name, id, address); 9/19/2017 CUNY Brooklyn College 15

Override Methods in Super Class: Methods public class Person {... public String tostring() { return "Person (name=" + name + ", id=" + id + ", address=" + address + ")"; } } public class Student extends Person { public String tostring() { return "Student (name=" + name + ", id=" + id + ", address=" + address + ", coursestaken=[" + String.join(", ", classestaken) + "])"; } } 9/19/2017 CUNY Brooklyn College 16

Override Methods in Super Class: Example Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave"); Student adam = new Student("Adam Smith", "00248", "2902 Bedford Ave"); System.out.println (ben.tostring()); System.out.println(adam.toString()); Person (name=ben Franklin, id=00124, address=2901 Bedford Ave) Student (name=adam Smith, id=00248, address=2902 Bedford Ave, coursestaken=[]) 9/19/2017 CUNY Brooklyn College 17

Questions Inheritance in Java Access control of class members Constructors Overriding methods A few other related items this, super 9/19/2017 CUNY Brooklyn College 18

Polymorphism One type appears as and is used like another type Example A Student object can be used in place of a Person object. Inheritance is an approach to realize polymorphism 9/19/2017 CUNY Brooklyn College 19

Polymorphism: Example 1 Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave"); Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave"); System.out.println (ben.tostring()); System.out.println(adam.toString()); Person (name=ben Franklin, id=00124, address=2901 Bedford Ave) Student (name=adam Smith, id=00248, address=2902 Bedford Ave, coursestaken=[]) 9/19/2017 CUNY Brooklyn College 20

Polymorphism: Example 2 public static void display(person person) { } System.out.println(person.toString()); Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave"); Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave"); display(ben); display(adam); Person (name=ben Franklin, id=00124, address=2901 Bedford Ave) Student (name=adam Smith, id=00248, address=2902 Bedford Ave, coursestaken=[]) 9/19/2017 CUNY Brooklyn College 21

How about Other Methods? Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave"); Student adam = new Student("Adam Smith", "00248", "2902 Bedford Ave"); adam.havetakenclass("cisc3120"); display(ben); display(adam); Person (name=ben Franklin, id=00124, address=2901 Bedford Ave) Student (name=adam Smith, id=00248, address=2902 Bedford Ave, coursestaken=[cisc3120]) 9/19/2017 CUNY Brooklyn College 22

How about this example? You say, adam appears to be a Student object. Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave"); Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave"); adam.havetakenclass("cisc3120"); display(ben); display(adam); Error: The method havetakenclass(string) is undefined for the type Person 9/19/2017 CUNY Brooklyn College 23

Type Casting You can only invoke the method of declared type, i.e., Person. Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave"); Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave"); ((Student)adam).haveTakenClass("CISC3120"); display(ben); display(adam); Person (name=ben Franklin, id=00124, address=2901 Bedford Ave) Student (name=adam Smith, id=00248, address=2902 Bedford Ave, coursestaken=[cisc3120]) 9/19/2017 CUNY Brooklyn College 24

Actual Type and Declared Type Declared type: type at compilation time Actual type: type at runtime A variable may refer to an object of different type at runtime Example: actual and declared types of ben, and adam? Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave"); Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave"); ((Student)adam).haveTakenClass("CISC3120"); 9/19/2017 CUNY Brooklyn College 25

Type Casting Down-casting Cast to a subtype It is allowed when there is a possibility that it succeeds at run time (e.g., type to be casted to matches actual type) In the example, a Person object references to a Student object, and the down casting is allowed. Up-casting Cast to a super type It is always allowed 9/19/2017 CUNY Brooklyn College 26

Questions Polymorphism via inheritance in Java Type casting in Java 9/19/2017 CUNY Brooklyn College 27

Terms of Choice Super type Super class Base type Base class Parent class Child class 9/19/2017 CUNY Brooklyn College 28

Design Consideration Composition vs. Inheritance 9/19/2017 CUNY Brooklyn College 29

More Example: Boat, RowBoat Both examples (Person-Student-Professor and Boat-RowBoat) are in the sampleprograms repository on Github 9/19/2017 CUNY Brooklyn College 30

Assignments To be available via CUNY Blackboard 9/19/2017 CUNY Brooklyn College 31