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

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

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

CS-202 Introduction to Object Oriented Programming

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

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

Inheritance, and Polymorphism.

CS/ENGRD 2110 SPRING 2018

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

CSC207 Week 3. Larry Zhang

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

Chapter 14 Abstract Classes and Interfaces

ITI Introduction to Computing II

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

ITI Introduction to Computing II

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

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

ITI Introduction to Computing II

ITI Introduction to Computing II

CS 251 Intermediate Programming Inheritance

C++ Inheritance and Encapsulation

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

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

index.pdf January 21,

What is Inheritance?

Advanced Placement Computer Science. Inheritance and Polymorphism

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

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

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Inheritance and Polymorphism

CIS 110: Introduction to computer programming

Super-Classes and sub-classes

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

Making New instances of Classes

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Lecture 5: Inheritance

Inheritance Motivation

Rules and syntax for inheritance. The boring stuff

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Structured Programming

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Inheritance and Polymorphism in Java

Inheritance and Polymorphism

CS 11 java track: lecture 3

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Data Structures (INE2011)

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Inheritance (continued) Inheritance

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

Introduction to Object-Oriented Programming

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

Practice for Chapter 11

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Programming in C# Inheritance and Polymorphism

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

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

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

CISC370: Inheritance

Module 01 Processing Recap

Classes and Inheritance Extending Classes, Chapter 5.2

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

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

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Inheritance and Polymorphism

What are the characteristics of Object Oriented programming language?

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

More Relationships Between Classes

Programming Exercise 14: Inheritance and Polymorphism

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

OBJECT ORİENTATİON ENCAPSULATİON

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

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

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Use the scantron sheet to enter the answer to questions (pages 1-6)

CSC9T4: Object Modelling, principles of OO design and implementation


IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

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

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

C08: Inheritance and Polymorphism

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

Introducing Java. Introducing Java. Abstract Classes, Interfaces and Enhancement of Polymorphism. Abstract Classes

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

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

COMP 110/L Lecture 19. Kyle Dewey

Chapter 1: Object-Oriented Programming Using C++

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

ECE 3574: Dynamic Polymorphism using Inheritance

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Polymorphism and Interfaces. CGS 3416 Spring 2018

1: Introduction to Object (1)

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

Implements vs. Extends When Defining a Class

Abstract Classes and Interfaces

Object-Oriented Programming More Inheritance

Transcription:

Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 4: OO Principles - Polymorphism http://courses.cs.cornell.edu/cs2110/2018su

Lecture 3 Recap 2 Good design principles. Modularity Encapsulation Inheritance Access modifiers, extends, constructor chaining, etc.

Lecture 4 3 Abstraction Polymorphism Multiple Inheritance Problems Interfaces Parametrised Types

Inheritance - Recap Inheritance allows types to be specialised Minimise code re-use Allows multiple specialised types (ex: instructor, student) to be used everywhere the base class can be used. But has some shortcomings...

A geometry detour Assume that want to write a geometry program that can manipulate the area and perimeter of 2D shapes. Want to define circle, rectangle and triangle (x, y) Position of a rectangle in the plane is given by its upper-left corner. Calculate perimeter by 2*(width + height), area by width * height (x, y) Position of a circle in the plane is given by the upper-left corner of its bounding box. Perimeter calculated by 2*Π*radius, area by Π*radius^2

A geometry detour - Inheritance? Create a class shape that defines area() and perim() functions, and have every subclass extend Shape and override those methods. What s wrong here? Shape x y area() perim() Rectangle area() perim() width height Triangle area() perim() base height Circle area() perim() radius

Inheritance - Recap Inheritance allows types to be specialised Minimise code re-use Allows multiple specialised types (ex: instructor, student) to be used everywhere the base class can be used. Inheritance can force a family of derived classes to implement specific functionality But there isn t really a convenient default behaviour for the base class.

Abstraction Program specification mandates any class that is a Shape should implement area() and perimeter() But area() and perimeter() of a Shape doesn t really make sense Instead, want to force all Shapes to implement their own area() and perimeter() Shape is an abstract type with certain desired functionality Square, Circle, etc. are concrete instantiations of that type

Abstract classes to the rescue Most OOP languages support a notion of abstract classes Abstract classes can contain method stubs (methods without a body) Abstract classes cannot be instantiated Why? Java uses keyword abstract class abstract Shape { } int x ; int y; int getxposition() { return x;} abstract int area(); Syntax: If a method has keyword abstract in its declaration, use a semicolon instead of a method body

Multiple Inheritance Examples so far suggest that a class can inherit from a single base class Sometimes, want to inherit from multiple base classes Meet the graduate student Can be both a Student and an Instructor What can we do?

Diamond Inheritance Problem Multiple inheritance introduces the diamond problem Definition: Ambiguity that arises when a class inherits from two classes that define and implement the same method. class Instructor extends Person { int salary; int getsalary(); void dance() { System.out.println( Let s boogie );} } class Student extends Person { int gpa; int getgpa(); void dance() { System.out.println( Let s cha-cha );} } GraduateStudent inherits from both Instructor and Student. Should she boogie or cha-cha?

Interfaces to the rescue Java mandates that every class can inherit from at most one class (possibly abstract) Instead, it introduce special classes that can do multiple inheritance: interfaces Definition Interfaces are special classes that have no state (cannot define any fields) all methods are abstract Interfaces define functionality only, a contract that any concrete types must satisfy

Interfaces to the rescue Java uses interface keyword to define an interface Classes must implement an interface public interface A { public int mymethod(); } public class B implements A { public int mymethod(); }

Revisiting the Graduate Student A graduate student can teach Implements a Teaching interface with method getsalary() A graduate student can study Implements a Study interface with a method getgpa(); A graduate student is still a Person (hopefully) Extends class Person, inherit fields name, DoB class GraduateStudent extends Person implements Teaching, Study {... }

Interfaces vs Abstract Classes Not going to lie, they are similar. Hard to determine which one to use at times We ll see next lecture two examples of Java Interfaces Rule of thumb: when it doubt, start with an interface View interfaces as: what something can do/defines an abstract data type/contract to fullfill force high-level of abstraction in code View abstract classes as: represents something allows sharing common code between subclasses What should Shape be? Interface or abstract class?

Manipulating derived types Recall: inheritance allows us to use derived types everywhere we want to use a base type. public int sumareas(shape[] allshapes) Consider a method: Want to calculate the sum of the areas of all the shapes in the drawing But area() is an abstract method and all shapes implement different area methods. What can we do?

First attempt - Casting Explicitly try casting each individual shape to the appropriate type instanceof keyword Downsides: Cumbersome to write, error-prone Every time add a new Shape, have to modify that function public int sumareas(shape[] allshapes) { int sum = 0; int nbshapes = allshapes.length; for (int i = 0 ; i < nbshapes ; i++ { Shape s = shape[i]; if (s instanceof Circle) { Circle c = (Circle) s; sum+= c.area(); } else if (s instanceof Triangle) { Triangle t = (Triangle) s; } else { } } }

Polymorphism to the rescue 18 Definition: a language's ability to process objects of various types and classes through a single, uniform interface Java polymorphism calls the appropriate method for the type of the object that is referred to in each variable rather than the method that is defined by the variable's type Shape s = new Circle(); s.area() will call the circle area method. Polymorphism separates the interface and implementation allows the programmer to program at the interface only

Second attempt Magic of polymorphism Only need to worry about the spec of Shapes (they all implement an area() method). Not about any specifics of the Shape Better modularity Less buggy public int sumareas(shape[] allshapes) { int sum = 0; int nbshapes = allshapes.length; for (int i = 0 ; i < nbshapes ; i++ { Shape s = shape[i]; sum+= s.area(); } return sum; }

Static vs Dynamic Polymorphism Java uses dynamic polymorphism Run the method in the child Must be down at runtime since that is when you know the child s type. Alternative is static polymorphism Decide at compile-time. Since don t know what true type will be, just run the method in the parent type. Dynamic polymorphism much more practical, but has a performance overhead Java only does dynamic C++ offers developers the choice

Principles of OO Recap May all seem similar Modularity Encapsulation Abstraction Polymorphism All sides of the same coin: enable clean, easy to reason about with minimal bugs, where each object has well-defined functionality and exposes only the minimal information necessary to other components of the system.

References in JavaHyperText abstraction abstract class interface implements extends polymorphism subtyping abstract data type