Polymorphism: Inheritance Interfaces

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

COMP 110/L Lecture 19. Kyle Dewey

Inheritance and Polymorphism

COP 3330 Final Exam Review

CMSC 132: Object-Oriented Programming II

Binghamton University. CS-140 Fall Chapter 9. Inheritance

CS-202 Introduction to Object Oriented Programming

JAVA MOCK TEST JAVA MOCK TEST II

Inheritance and Polymorphism

Inheritance -- Introduction

Polymorphism and Inheritance

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

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

Introduction to Inheritance

Inheritance and Interfaces

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

CSC207 Week 3. Larry Zhang

Java Object Oriented Design. CSC207 Fall 2014

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

CS 251 Intermediate Programming Inheritance

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Programming Exercise 14: Inheritance and Polymorphism

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

CO Java SE 8: Fundamentals

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

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Starting Out with Java: From Control Structures Through Objects Sixth Edition

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

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

Inheritance, Polymorphism, and Interfaces

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

CIS 110: Introduction to computer programming

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Inheritance. Transitivity

CMSC 132: Object-Oriented Programming II

Unified Modeling Language (UML) Class Diagram

OBJECT ORİENTATİON ENCAPSULATİON

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

Chapter 11: Inheritance

Inheritance (Part 2) Notes Chapter 6

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Example: Count of Points

Methods Common to all Classes

CS 200 More Classes Jim Williams, PhD

Super-Classes and sub-classes

Lecture 18 CSE11 Fall 2013 Inheritance

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

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

Inheritance, polymorphism, interfaces

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

C09: Interface and Abstract Class and Method

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

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

Inheritance and Interfaces

Intro to Computer Science 2. Inheritance

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

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

CSE 143 Lecture 20. Circle

INHERITANCE. Spring 2019

Creating Java Programs with Greenfoot

Comparing Objects 3/14/16

C++ Important Questions with Answers

public UndergradStudent(String n, String m, String p) { programme = p; super(n, m);

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

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

Chapter 6 Reflection

Topic 7: Algebraic Data Types

Chapter 10 Classes Continued. Fundamentals of Java

ITI Introduction to Computing II

Chapter 11 Classes Continued

CS 520 Theory and Practice of Software Engineering Fall 2018

Practice for Chapter 11

Inheritance CSC 123 Fall 2018 Howard Rosenthal

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

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

Chapter 15: Object Oriented Programming

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

COMP 110/L Lecture 20. Kyle Dewey

ITI Introduction to Computing II

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Chapter 3: Inheritance and Polymorphism

ITI Introduction to Computing II

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

ECE 122. Engineering Problem Solving with Java

ITI Introduction to Computing II

index.pdf January 21,

Chapter 10: Inheritance

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

Exercise: Singleton 1

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

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

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

Inheritance. COMP Week 12

Data Abstraction. Hwansoo Han

Chapter 9 Inheritance

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Java Magistère BFA

Programming II (CS300)

Transcription:

Polymorphism: Inheritance Interfaces 256

Recap Finish Deck class methods Questions about assignments? Review Player class for HW9 (starter zip posted) Lessons Learned: Arrays of objects require multiple object creation: new for the array memory, new for each object that goes into it (see Deck.java example). Warm-up: Piazza poll: @352 Class Relationships 257

Polymorphism literally: many forms key feature of object oriented programming method overloading: same name, different parameters (number and/or type) Inheritance creates a new [sub, derived] class by extending existing [super, base] one method overriding: redefine method in subclass that exists in superclass Interface defines common set of operations implementations differ from class to class 258

Class Relationships USES: ClassA uses ClassB when ClassA has static data members or methods with local variables of type ClassB. For example, many of our main classes use the Scanner class and the String class. HAS: ClassA has one or more instance data members of type ClassB. This could be class composition (single members) or aggregation (array of ClassB) IS: ClassA is a subclass (inheritance based derivation) of ClassB. For example, Poodle is a sub-class of Dog. 259

Relationship Examples Let's revisit the poker tournament example... Tournament has Tables (aggregation) Table has Dealer, Players, Deck Person has String, Table or just table number? Dealer is Person, has Deck Player is Person, has Hand CardSet has Cards Deck is CardSet, uses Random Hand is CardSet Card has String 260

UML Class Diagram (unified modeling language) Person IS-A Each class is represented by a rectangle Player Dealer An arrow goes from each sub-class to its base (IS) Hand HAS-A Deck HAS-A A diamond connects each class to those it contains (HAS) IS-A CardSet HAS-A Card 261

Recall: Object class in JAVA all java classes are derived from Object directly or indirectly contains these methods (plus others): String tostring() returns class name + memory address boolean equals(object o) compares memory addresses (same as ==) Object clone() returns copy of object we override these methods (tostring & equals especially) in our classes to get more appropriate behaviors than the provided default 262

Inheritance We can build new classes by extending existing ones The subclasses inherit all the members of the superclass Protected members of the superclass can be accessed directly in the subclass, but private members cannot Subclasses inherit the private members (ie, their objects contain the private data), but they can't operate on them directly. We use super to call superclass constructors to initialize subclass objects, and also to refer to other inherited members in some cases. 263

Inheritance Terminology These are all different words and phrases for the same things in an inheritance relationship: ClassA -> IS a -> ClassB Subclass -> is a subset of -> Superclass Derived class -> is derived from -> Base class Child class -> inherits from -> Parent class 264

Inheritance Pic public class Parent { private char init; protected double money; public Parent(char c, double d) { this.init = c; this.money = d; } } public class Child extends Parent { private int val; public Child(char c, double d, int n) { this.super(c, d); this.money = 2 * this.money; this.val = n; } } Parent p = new Parent( P, 3500.00); Child c = new Child( S, 20.20, 6); init P c val 6 init S p money 3500.0 money 40.4 265

Interfaces They are collections of operations, specified through method headers, and comments All methods are public by default Interfaces may contain some data it is always public and static by default We write classes to implement an interface When implementing an interface, a class must have definitions for every method in the interface An "abstract" method is one without a definition (as in an interface) A class can implement multiple interfaces, and multiple classes usually implement each interface We cannot use new to create an object of an interface type they don't have constructors! 266

Shapes Polymorphism Example Let's implement these together... implements Interface: Shape.java implements Circle.java Parallelogram.java (base class) Has ShapeTest.java main program Is-a Rectangle.java (derived & base class) Is-a Square.java (derived class) 267

Polymorphism related reserved words extends: to say subclass extends base class super: to call base class constructor or base class version of an over-ridden method in subclass instanceof: to see if an object belongs to a particular class protected: to give access to derived classes, access to all other classes in the same package interface: to define an interface implements: to say a class will have method definitions to support an interface 268

Shapes Exercise Download ShapeTest.java from the schedule, along with the starter classes We'll write a Shape interface together, and then you'll update classes Circle, Parallelogram and Rectangle to implement it Use as little code as possible, making the most of inheritance instead 269

Poly References & Objects MyFace face = new MyBase(4); implements (MyFace <---------- MyBase) MyFace MyBase value: 4 MyBase base = new MyBase(5); MyBase MyBase value: 5 MySub sub = new MySub(6, 7); MySub MySub data: 7 MyBase value: 6 MyBase bsub = new MySub(8, 9); (MyBase extends MySub) MyBase MySub data: 9 MyBase value: 8 270

Comparable interface in JAVA API Contains one method: int compareto (Object param) Should return a negative int if this < param, 0 if same, positive int if this > param String class implements Comparable for example We implement this interface in lots of classes, particularly if we might want to sort a collection of those objects Example: let's update Card to implement Comparable 271