C18a: Abstract Class and Method

Similar documents
C09: Interface and Abstract Class and Method

C09: Interface, and Abstract Class and Method

C10: Garbage Collection and Constructors

C11: Garbage Collection and Constructors

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

C12a: The Object Superclass and Selected Methods

C08: Inheritance and Polymorphism

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

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

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

C20a: Selected Interfaces in Java API

Chapter 14 Abstract Classes and Interfaces

CISC 3115 TY3. C28a: Set. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/29/2018 CUNY Brooklyn College

Object-Oriented Programming More Inheritance

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

C08: Inheritance and Polymorphism

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

Lecture 5: Inheritance

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

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

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

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

Inheritance and Interfaces

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

Object-Oriented ProgrammingInheritance & Polymorphism

index.pdf January 21,

Polymorphism CSCI 201 Principles of Software Development

ITI Introduction to Computing II

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Composite Pattern - Shapes Example - Java Sourcecode

ITI Introduction to Computing II

Inheritance, Polymorphism, and Interfaces

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

CSC207 Week 3. Larry Zhang

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

CS111: PROGRAMMING LANGUAGE II

C17a: Exception and Text File I/O

CMSC 132: Object-Oriented Programming II

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

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

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

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

C22a: Problem Solving using Recursion

Inheritance and Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

Binghamton University. CS-140 Fall Chapter 9. Inheritance

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

COMP 110/L Lecture 20. Kyle Dewey

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

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

CS111: PROGRAMMING LANGUAGE II

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

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

ECE 122. Engineering Problem Solving with Java

CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

Inheritance & Polymorphism. Object-Oriented Programming

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

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

Chapter 5 Object-Oriented Programming

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

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Polymorphism. Arizona State University 1

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

Object Orientated Analysis and Design. Benjamin Kenwright

Chapter 6 Introduction to Defining Classes

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

Polymorphism and Interfaces. CGS 3416 Spring 2018

BBM 102 Introduction to Programming II Spring Abstract Classes and Interfaces

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

C30b: Inner Class, Anonymous Class, and Lambda Expression

Design Patterns: State, Bridge, Visitor

What is Inheritance?

ITI Introduction to Computing II

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

Chapter 9 Inheritance

COMP 250. inheritance (cont.) interfaces abstract classes

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Structured Programming

Software Development (cs2500)

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

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

Object-Oriented Programming

ITI Introduction to Computing II

Chapter 10 Classes Continued. Fundamentals of Java

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

ECE 3574: Dynamic Polymorphism using Inheritance

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

C27a: Stack and Queue

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Inheritance, polymorphism, interfaces

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Inheritance, and Polymorphism.

CS 251 Intermediate Programming Inheritance

Introduction to Object-Oriented Programming

Transcription:

CISC 3115 TY3 C18a: Abstract Class and Method Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/31/2018 CUNY Brooklyn College 1

Outline Recap Inheritance and polymorphism Abstract method and class Exercises C18a-1 C18a-2 C18a-3 10/31/2018 CUNY Brooklyn College 2

Recap: Inheritance and Polymorphism Problem: leveraging on polymorphism, write generic method to compute total areas of a list of geometric shapes. 10/31/2018 CUNY Brooklyn College 3

The Shape Class Hierarchy Shape Circle Rectangle 10/31/2018 CUNY Brooklyn College 4

Recap: Inheritance and Polymorphism Problem: leveraging on polymorphism, write generic method to compute total areas of a list of geometric shapes. Solution public double sumareasofshapes(arraylist<shape> shapelist) { double sum = 0.; for(shape shape: shapelist) { sum += shape.getarea(); return sum; 10/31/2018 CUNY Brooklyn College 5

The Dilemma Problem: leveraging on polymorphism, write generic method to compute total areas of a list of geometric shapes. Dilemma: but can we compute a shape s area without knowing the specification of the shape (e.g., type of shape, parameters of the shape)? 10/31/2018 CUNY Brooklyn College 6

The Shape Class Do you like the getarea() method here? public class Shape { public double getarea() { Remarks throw new UnsupportedOperationException("Cannot invoke the method"); We know semantically that each shape has a behavior to compute its area However, we don t know the algorithm without knowing the actual shape The dummy method in the above is not only semantically undesired, but also can easily cause runtime errors. 10/31/2018 CUNY Brooklyn College 7

Abstract Method An abstract method has no implementation public abstract double getarea() ; 10/31/2018 CUNY Brooklyn College 8

Abstract Method: No Implementation Let s declare an abstract method. How about these code snippets? abstract double getarea() ; double getarea() ; abstract double getarea() { abstract double getarea() { double getarea() { 10/31/2018 CUNY Brooklyn College 9

Abstract Method: No Implementation Let s declare an abstract method. How about these code snippets? abstract double getarea() ; double getarea() ; abstract double getarea() { abstract double getarea() { double getarea() { 10/31/2018 CUNY Brooklyn College 10

Abstract Class In Java, any class that has an abstract method must be declared abstract Example abstract class Shape { public abstract double area() ; Abstract class: a class that is declared abstract Abstract classes cannot be instantiated, but they can be subclassed. 10/31/2018 CUNY Brooklyn College 11

Class with Abstract Method 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 two code snippets? class Animal { abstract void makenoise(); abstract class Animal { abstract void makenoise(); 10/31/2018 CUNY Brooklyn College 12

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(); 10/31/2018 CUNY Brooklyn College 13

Abstract Class: Subclass & Instantiation Abstract classes cannot be instantiated, but they can be subclassed. abstract class Shape { public abstract double area() ; How about these code snippets? Shape s = new Shape(); class Circle extends Shape { Shape s = new Circle(); 10/31/2018 CUNY Brooklyn College 14

Abstract Class: Subclass & Instantiation Abstract classes cannot be instantiated, but they can be subclassed. abstract class Shape { public abstract double area() ; How about these code snippets? Shape s = new Shape(); class Circle extends Shape { Shape s = new Circle(); 10/31/2018 CUNY Brooklyn College 15

The Shape Class Hierarchy: Abstract Shape Shape In UML class diagram, italicize names of abstract classes and methods Circle Rectangle 10/31/2018 CUNY Brooklyn College 16

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. The subclass may also declare abstract method itself 10/31/2018 CUNY Brooklyn College 17

Concrete Subclass A subclass provides implementations of all of the abstract methods declared in its superclass. An abstract method thus can have many implementations. 10/31/2018 CUNY Brooklyn College 18

The Shape Class Hierarchy: Concrete Subclasses Abstract class Shape s getarea method has many implementations in the abstract class s subclasses Shape Circle Rectangle RightTriangle 10/31/2018 CUNY Brooklyn College 19

Concrete Subclass Example: public class RightTriangle extends Shape { private double base; private double height; public double getarea() { return 0.5 * base * height; 10/31/2018 CUNY Brooklyn College 20

Abstract Subclass The subclass must also be declared abstract if it does not provide implementation of all of the abstract methods in its superclass. The subclass may also declare abstract method itself. 10/31/2018 CUNY Brooklyn College 21

Abstract Subclass: Extending Type Hierarchy Motivation: add types to type hierarchy to differentiate objects of different types Shape Triangle Rectangle Circle RightTriangle EquilateralTriangle 10/31/2018 CUNY Brooklyn College 22

Abstract Subclass: Extending Type Hierarchy Example: public abstract class Triangle extends Shape { public Triangle(String name) { super(name); public int getnumberofsides() { return 3; Remark: An abstract class must be declared abstract. An abstract class can have concrete methods. 10/31/2018 CUNY Brooklyn College 23

Abstract Subclass: Add New Abstract Behavior Motivation: add new, but unspecified behavior to an abstract class Shape Triangle Rectangle Circle + isequilateral(): boolean RightTriangle + isequilateral(): boolean EquilateralTriangle + isequilateral(): boolean 10/31/2018 CUNY Brooklyn College 24

Abstract Subclass: Add New Behavior Example: public abstract class Triangle extends Shape { public Triangle(String name) { super(name); public int getnumberofsides() { return 3; public abstract boolean isequilateral(); 10/31/2018 CUNY Brooklyn College 25

Questions? Abstract class Abstract method Extending abstract class Concrete subclass Abstract subclass Example programs 10/31/2018 CUNY Brooklyn College 26

Exercise C18a-1 In this exercise, you are to compare the Shape class hierarchies with and without making Shape class abstract. Create directory C18a-1 in your weekly practice repo, and copy the two directories ConcreteShape and AbstractShape to your C18a-1 directory Add a RightTriangle class with two data fields, base and height to the programs in both of the ConcreteShape and AbstratShape directories, the class is a subclass of the Shape class. (As seen in UML class diagram in this Slide) Revise the 4 client classes (e.g., ShapeClient, ShapeClientError, etc) to use your RightTriangle class. Compile and run the programs, and observe compilation error and runtime error if any. Write a comment in the client programs to explain what you observe. Submit your work using git 10/31/2018 CUNY Brooklyn College 27

Exercise C18a-2 In this exercise, you are to realize the UML class diagram in the slide. Create directory C18a-2 in your weekly practice repo, and copy the programs in the AbstractShape directory in your answers to C18a-1 to the directory Add an abstract class Triangle extending the Shape class as shown in the UML class diagram (in the slide). Conforming to the UML class diagram, revise the RightTriangle class so that it extends the Triangle class Conforming to the UML class diagram, create the EquilateralTriangle class Revise the ShapeClient class to use all the concrete classes in the Shape class hierarchy, and also add statements to demonstrate the use of the isequilateral method. Use git to make a submission 10/31/2018 CUNY Brooklyn College 28

Exercise C18a-3 In this exercise, you are to realize the UML class diagram in next slide. Create directory C18a-3 in your weekly practice repo, create all the classes from scratch in the UML class diagram shown in the next slide. Note that Animal and Feline are abstract classes Note that makenoise and pounce are abstract methods. To implement the methods in concrete subclasses, simply print out an appropriate message, such as, Cat purrs, Panther roars, Dove coos, Whale clicks, Cat pounces, Panther pounces, etc. Write a client class, called the AnimalClient class. In the client, write three methods Two generic methods, one of which takes an ArrayList of Animals and invokes each Animal s makenoise method, and the other of which takes an ArrayList of Felines and invokes each Feline s pounce method A main method that demonstrates the use of the two generic methods. Use git to make a submission 10/31/2018 CUNY Brooklyn College 29

C18a-3: The Animal Class Hierarchy Animal + makenoise(): void Feline Dove Whale + pounce(): void + makenoise(): void + makenoise(): void Cat + makenoise(): void + pounce(): void Panther + makenoise(): void + pounce(): void 10/31/2018 CUNY Brooklyn College 30