COMP 250. inheritance (cont.) interfaces abstract classes

Similar documents
COMP 250. Lecture 29. interfaces. Nov. 18, 2016

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

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

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

CSE 143 Lecture 20. Circle

Lecture Notes Chapter #9_b Inheritance & Polymorphism

INHERITANCE. Spring 2019

ITI Introduction to Computing II

ITI Introduction to Computing II

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

Programming 2. Inheritance & Polymorphism

Java Comparable interface

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

Chapter 14 Abstract Classes and Interfaces

Collections, Maps and Generics

Reusing Classes. Hendrik Speleers

Making New instances of Classes

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

COMP 250 Fall inheritance Nov. 17, 2017

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

type conversion polymorphism (intro only) Class class

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

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

doubly linked lists Java LinkedList

Inheritance (Deitel chapter 9)

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

AShape.java Sun Jan 21 22:32: /** * Abstract strategy to compute the area of a geometrical shape. Dung X. Nguyen * * Copyright

Programming II (CS300)

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Inheritance (Part 2) Notes Chapter 6

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

index.pdf January 21,

Interfaces. Relatedness of types. Interface as a contract. The area and perimeter of shapes 7/15/15. Savitch ch. 8.4

Chapter 13 Abstract Classes and Interfaces

CS1150 Principles of Computer Science Objects and Classes

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

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

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

CS Week 13. Jim Williams, PhD

ITI Introduction to Computing II

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2

ITI Introduction to Computing II

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Java interface Lecture 15

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

C12a: The Object Superclass and Selected Methods

Java Review: Objects

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

Inheritance. Transitivity

CMSC 202. Containers

Chapter 9 - Object-Oriented Programming: Polymorphism

Chapter 6: Inheritance

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

CS 113 PRACTICE FINAL

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

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

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

CMSC 132: Object-Oriented Programming II

Building Java Programs

C18a: Abstract Class and Method

Inheritance and Interfaces

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Abstract Classes and Interfaces

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

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Interfaces. An interface defines a set of methods. An interface declaration contains signatures, but no implementations.

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

public static boolean isoutside(int min, int max, int value)

Inheritance Motivation

Inheritance and Polymorphism

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Distributed Systems Recitation 1. Tamim Jabban

List ADT. Announcements. The List interface. Implementing the List ADT

Advanced Placement Computer Science. Inheritance and Polymorphism

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

Inheritance and Interfaces

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

Topic 7: Algebraic Data Types

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Distributed Systems Recitation 1. Tamim Jabban

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Java Primer. CITS2200 Data Structures and Algorithms. Topic 0

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

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

Generics. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 10

Inheritance and Polymorphism

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Transcription:

COMP 250 Lecture 31 inheritance (cont.) interfaces abstract classes Nov. 20, 2017 1

https//goo.gl/forms/ymqdaeilt7vxpnzs2 2

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) extends (automatic) class Animal extends extends class String class Dog extends class Beagle 3

A few more details about class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) 4

[ASIDE slide also added to last lecture] Java API for Object.hashCode() recommends If o1.equals(o2) is true then o1.hashcode() == o2.hashcode() should be true. Why? The converse need not hold. E.g. Two different Java strings might have the same hashcode(). 5

Hash Map hashcode() compression hash values 0 4 3 9 12 6 8 keys (K) int 22 14 m-1 Java HashMap<K,V> overrides K.equals(Object) to compare keys. If key1.equals(key2) is true, then we want key1.hashcode() == key2.hashcode() to be true. Otherwise get(key1) and get(key2) might return different values. 6

Finishing up last lecture class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) 7

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.clone() makes a new object. class NaturalNumber NaturalNumber ( ) boolean equals( Object ) int hashcode( ) String tostring( ) NaturalNumber clone( ) Recall Assignment 1 NaturalNumber.clone() This is overriding. 8

Object.clone() recommendation Q x.clone() == x should be true or false? Q x.equals( x.clone() ) should be true or false? 9

Object.clone() recommendation Q x.clone() == x should be true or false? A false Q x.equals( x.clone() ) should be true or false? A true equals() needs to be carefully defined to ensure this 10

How to clone a list? SLinkedList<Shape> list list.clone() =? head size 4 tail 11

deep copy SLinkedList<Shape> list list.clone() head head size 4 size 4 tail tail 12

shallow copy SLinkedList<Shape> list list.clone() head head size 4 size 4 tail tail 13

https//docs.oracle.com/javase/7/docs/api/java/util/linkedlist.html Java LinkedList<T>.clone() makes a shallow copy. 14

LinkedList<Shape> list1, list2, list3; list1 list2 list3 head head head size 4 size 4 size 4 tail tail tail Assume Shape inherits the Object.equals(Object) method, i.e. equals() means == list1.equals(list2) returns what? list2.equals(list3) what? 15

LinkedList<Shape> list1, list2, list3; list1 list2 list3 head head head size 4 size 4 size 4 tail tail tail Assume Shape inherits the Object.equals(Object) method, i.e. equals() means == list1.equals(list2) returns false. list2.equals(list3) returns true. 16

COMP 250 Lecture 31 inheritance (cont.) interfaces abstract classes Nov. 20, 2017 17

reserved word Java interface like a class, but only the method signatures are defined 18

Example List interface interface List<T> { void add(t) void add(int, T) T remove(int) boolean isempty() T get( int ) int size() https//docs.oracle.com/javase/7/docs/api/java/util/list.html 19

class ArrayList<T> implements List<T> { void add(t) {. void add(int, T) {. T remove(int) {. boolean isempty() {. T get( int ) {. int size() {. void ensurecapacity(int) { void trimtosize( ) { Each of the List methods are implemented. (In addition, other methods are implemented.) 20

class LinkedList<T> implements List<T> { void add(t) {. void add(int, T) {. T remove(int) {. boolean isempty() {. T get( int ) {. int size() {. void addfirst(t) {. void addlast(t) {. Each of the List methods are implemented. (In addition, other methods are implemented.) 21

How are Java interface s used? List<String> list; list = new ArrayList<String>(); list.add("hello"); list = new LinkedList<String>(); list.add( new String( hi ) ); 22

void somemethod( List<String> list ){ list.add("hello"); list.remove( 3 ); somemethod() can be called with a LinkedList or an ArrayList as the parameter. 23

void somemethod( List<String> list ){ list.add("hello"); list.remove( 3 ); list.addfirst( have a nice day ); // compiler error 24

classes interfaces 25

classes interfaces extends extends implements A subclass can extend one superclass. A class can implement multiple interfaces. An interface can extend multiple interfaces. 26

Recall lecture 4 where we introduced ArrayList s and assumed a Shape class. 0 1 2 3 4 5 6 7 8 9 null null null Would it make more sense for Shape to be an interface? 27

interface Shape { double getarea(); double getperimeter(); class Rectangle implements Shape{ class Circle implements Shape{ 28

implements class Rectangle double height, width Rectangle(double height, double width) double getarea() double getperimeter() interface Shape double getarea() double getperimeter() implements double radius class Circle Circle( double radius) double getarea() double getperimeter() implements class Triangle double height, base Triangle(double height, double base){ double getarea() double getperimeter() 29

class Rectangle implements Shape{ double height, width; Rectangle( double h, double w ){ height = h; weight = w; double getarea(){ return height * width; double getperimeter(){ return 2*(height + width); 30

class Circle implements Shape{ double radius; Circle( double r ){ radius = r; double getarea(){ return MATH.PI * radius * radius; double getperimeter(){ return 2*MATH.PI * radius.. similarly for Triangle 31

Shape s = new Rectangle( 30, 40 ); s = new Circle( 2.5 ); s = new Triangle( 4.5, 6.3 ); 32

COMP 250 Lecture 31 inheritance (cont.) interfaces abstract classes Nov. 20, 2017 33

Motivating Example Circular Circle Sphere Cylinder 34

implements interface Circular double getradius() void setradius(double) double getarea() implements implements class Circle class Sphere class Cylinder double double void double radius Circle(){ getradius() setradius(double) getarea() double radius Sphere( ) double getradius() void setradius(double) double getarea() double double void double radius Cylinder( ) getradius() setradius(double) getarea() 35 Can we avoid repeating some of these definitions?

Abstract Class Like a class, it can have fields and methods with bodies Like an interface, it can have methods with only signatures. It cannot be instantiated, but it has constructors which are called by the sub-classes. 36

abstract class Circular double radius Circular( double radius) double getradius() void setradius(double radius) abstract double getarea() Implemented here extends extends extends class Circle Circle( double radius ) double getarea() class Sphere Sphere( double radius ) double getarea() class Cylinder double length Cylinder (double radius, double length) double getarea() 37

abstract class Circular { double radius; Circular(double radius){ this.radius = radius; // field // constructor double getradius(){ return radius; // implemented methods void setradius(double r){ this.radius = r; abstract double getarea(); // abstract method 38

class Circle extends Circular{ Circle(double radius){ super(radius); // constructor // superclass field double getarea(){ double r = this.getradius(); return Math.PI * r*r; 39

class Cylinder extends Circular{ double height; Cylinder(double radius, double h){ super(radius); this.height = h; // constructor double getarea(){ double r = this.getradius(); return 2 * Math.PI * radius * height; 40

classes (abstract or not) interfaces extends extends implements abstract A subclass (abstract or not) extends exactly one superclass. A class (abstract or not) can implement multiple interfaces. 41

A class (abstract or not) cannot extend more than one class (abstract or not). extends extends Not allowed! Why not? 42

A class (abstract or not) cannot extend more than one class (abstract or not). extends extends Not allowed! Why not? The problem could occur if two superclasses have implemented methods with the same signature. Which would be inherited by the subclass? 43