C09: Interface and Abstract Class and Method

Similar documents
C09: Interface, and Abstract Class and Method

C18a: Abstract Class and Method

C11: Garbage Collection and Constructors

C10: Garbage Collection and Constructors

C08: Inheritance and Polymorphism

C12a: The Object Superclass and Selected Methods

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

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

C08: Inheritance and Polymorphism

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

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

CS111: PROGRAMMING LANGUAGE II

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

First IS-A Relationship: Inheritance

Software Development (cs2500)

ECE 122. Engineering Problem Solving with Java

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

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

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

More Relationships Between Classes

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

Object-Oriented Programming More Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

C20a: Selected Interfaces in Java API

type conversion polymorphism (intro only) Class class

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

Inheritance and Polymorphism. CS180 Fall 2007

Inheritance. Transitivity

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof

What is Inheritance?

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

Inheritance (Outsource: )

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

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Inheritance & Polymorphism. Object-Oriented Programming

Object-Oriented Programming

INHERITANCE. Spring 2019

CIS 110: Introduction to computer programming

Java How to Program, 8/e

Inheritance and Polymorphism

Polymorphism and Interfaces. CGS 3416 Spring 2018

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

C02: Overview of Software Development and Java

COMP 110/L Lecture 20. Kyle Dewey

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

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

Java Object Oriented Design. CSC207 Fall 2014

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

Polymorphism: Inheritance Interfaces

JAVA MOCK TEST JAVA MOCK TEST II

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

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

STATIC, ABSTRACT, AND INTERFACE

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

Example: Count of Points

CS-202 Introduction to Object Oriented Programming

OVERRIDING. 7/11/2015 Budditha Hettige 82

Check out Polymorphism from SVN. Object & Polymorphism

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

COMP 110/L Lecture 19. Kyle Dewey

C30b: Inner Class, Anonymous Class, and Lambda Expression

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

Binghamton University. CS-140 Fall Chapter 9. Inheritance

Introduction to Computation and Problem Solving

Chapter 9 Inheritance

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

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

ITI Introduction to Computing II

CS/ENGRD 2110 SPRING 2018

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

Chapter 14 Abstract Classes and Interfaces

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

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

ITI Introduction to Computing II

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

1.00 Lecture 14. Exercise: Plants

Object-Oriented ProgrammingInheritance & Polymorphism

Full file at Chapter 2 - Inheritance and Exception Handling

CLASS DESIGN. Objectives MODULE 4

Programming in the Large II: Objects and Classes (Part 2)

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

CS 251 Intermediate Programming Inheritance

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Inheritance, polymorphism, interfaces

25. Generic Programming

CMSC 132: Object-Oriented Programming II

Exercise: Singleton 1

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

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

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

Overriding Variables: Shadowing

CSA 1019 Imperative and OO Programming

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

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

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

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

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Classes and Inheritance Extending Classes, Chapter 5.2

Transcription:

CISC 3120 C09: Interface and Abstract Class and Method Hui Chen Department of Computer & Information Science CUNY Brooklyn College 9/28/2017 CUNY Brooklyn College 1

Outline Recap Polymorphism In-class group exercise Abstract method Abstract class Interfaces The Object super class The instanceof operator 9/28/2017 CUNY Brooklyn College 2

In-Class Group Exercise Shape, Circle, and Rectangle Discuss your solution with your team members 9/28/2017 CUNY Brooklyn College 3

The Shape Class Do you like the area() method here? public class Shape { public double area() { System.out.println("This method is not supposed to be called."); return 0; Are we ever going to instantiate a Shape object? 9/28/2017 CUNY Brooklyn College 4

Abstract Class A class that is declared abstract Example abstract class Animal { Abstract classes cannot be instantiated, but they can be subclassed. 9/28/2017 CUNY Brooklyn College 5

Subclass & Instantiation Abstract classes cannot be instantiated, but they can be subclassed. abstract class Animal { How about these examples? Animal animal = new Animal(); class Dog extends Animal { 9/28/2017 CUNY Brooklyn College 6

Subclass & Instantiation Abstract classes cannot be instantiated, but they can be subclassed. abstract class Animal { Animal animal = new Animal(); class Dog extends Animal { 9/28/2017 CUNY Brooklyn College 7

Abstract Method A method that is declared without an implementation abstract void makenoise(); A class that has an abstract method must be declared abstract How about these examples? class Animal { abstract void makenoise(); abstract class Animal { abstract void makenoise(); 9/28/2017 CUNY Brooklyn College 8

Class with Abstract Method A class that has an abstract method must be declared abstract class Animal { abstract void makenoise(); abstract class Animal { abstract void makenoise(); 9/28/2017 CUNY Brooklyn College 9

Subclass an Abstract Class Concrete subclass A subclass may provide implementations for all of the abstract methods in its parent class. Abstract subclass The subclass must also be declared abstract if it does not provide implementation of all of the abstract methods in its parent class. 9/28/2017 CUNY Brooklyn College 10

Example: Animal Game Animal Feline Dove Whale Cat Panther 9/28/2017 CUNY Brooklyn College 11

Interfaces Not the interface in Graphical User Interface Java has a reference type, called interface Typically contain abstract methods only. Java 8 introduces the concept of default methods and permits static methods Interfaces cannot be instantiated can only be implemented by classes or extended by other interfaces 9/28/2017 CUNY Brooklyn College 12

Example: Animal Game Interface FelineMotion Animal Interface BirdMotion Interface WhaleMotion Feline Dove Whale Cat Panther 9/28/2017 CUNY Brooklyn College 13

Example: Birds Fly, Whales Swim, and Cats... public interface BirdMotion { public void fly(direction direction, double speed, double distance); public interface WhaleMotion { public void swim(direction direction, double speed, double distance); public interface FelineMotion { public void walk(direction direction, double speed, double distance); public void pounce(animal prey); 9/28/2017 CUNY Brooklyn College 14

Example: Implementing Interfaces abstract class Feline implements FelineMotion { public void walk(direction direction, double speed, double distance) { public void pounce(animal prey) { class Dove extends Animal implements BirdMotion { public void fly(direction direction, double speed, double distance) { 9/28/2017 CUNY Brooklyn College 15

Using an Interface as a Type Interfaces are data types void flyall(arraylist<birdmotion> flyinganimals) { 9/28/2017 CUNY Brooklyn College 16

Evolving Interfaces Interfaces can be extended (like classes) interface CatMotion extends FelineMotion { public void tap(animal animal) ; 9/28/2017 CUNY Brooklyn College 17

Example: Extending FelineMotion Interface FelineMotion Animal Interface BirdMotion Interface WhaleMotion Interface CatMotion Feline Dove Whale Cat Panther interface CatMotion extends FelineMotion { public void tap(animal animal) ; 9/28/2017 CUNY Brooklyn College 18

Implementing Multiple Interfaces A class can implement multiple interfaces But a class cannot extend multiple classes Which one of the following are is allowed in Java? class FlyingCat extends Cat, Dove { class FlyingCat implements BirdMotion, CatMotion { 9/28/2017 CUNY Brooklyn College 19

Implementing Multiple Interfaces A class can implement multiple interfaces But a class cannot extend multiple classes class FlyingCat extends Cat, Dove { class FlyingCat implements BirdMotion, CatMotion { 9/28/2017 CUNY Brooklyn College 20

Example: Flying Cat in the Magic Kindom Interface FelineMotion Animal Interface BirdMotion Interface WhaleMotion Interface CatMotion Feline Dove Whale Cat Panther FlyingCat 9/28/2017 CUNY Brooklyn College 21

What an object can do? Java may know what method an object can invoke only at runtime. As a programmer how do we cope? Use appropriate data types void flyall(arraylist<birdmotion> flyinganimals) { Check object type at runtime (using instanceof) 9/28/2017 CUNY Brooklyn College 22

Operator instanceof Evaluates to true if the object is a given type; false otherwise public static void move(animal animal) { if (animal instanceof Cat) { 9/28/2017 CUNY Brooklyn College 23

The Object Super Class Java has a class called Object, like All classes are subclass of Object in Java Object boolean equals() Class getclass() int hascode() String tostring() 9/28/2017 CUNY Brooklyn College 24

Questions Abstract class Abstract method Interfaces Extending abstract classes Implementing interfaces The instanceof operator The Object superclass 9/28/2017 CUNY Brooklyn College 25

Assignment To be available via Blackboard Project 2 9/28/2017 CUNY Brooklyn College 26