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

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

CS 251 Intermediate Programming Inheritance

Inheritance. Transitivity

CS-202 Introduction to Object Oriented Programming

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

CMSC 132: Object-Oriented Programming II

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

Inheritance (an intuitive description)

Inheritance, Polymorphism, and Interfaces

Programming Exercise 14: Inheritance and Polymorphism

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

Abstract Classes and Interfaces

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

PROGRAMMING LANGUAGE 2

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Chapter 14 Abstract Classes and Interfaces

CS111: PROGRAMMING LANGUAGE II

What is Inheritance?

ITI Introduction to Computing II

Java Classes, Inheritance, and Interfaces

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

C++ Important Questions with Answers

Super-Classes and sub-classes

Object Oriented Programming. Java-Lecture 11 Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

ITI Introduction to Computing II

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Java Object Oriented Design. CSC207 Fall 2014

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

First IS-A Relationship: Inheritance

CLASS DESIGN. Objectives MODULE 4

Inheritance (Part 5) Odds and ends

JAVA MOCK TEST JAVA MOCK TEST II

INHERITANCE. Spring 2019

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance (Outsource: )

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

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

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

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

Java Fundamentals (II)

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following.

COP 3330 Final Exam Review

Inheritance -- Introduction

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

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Chapter 4 Defining Classes I

More about inheritance

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 18 CSE11 Fall 2013 Inheritance

Inheritance and Polymorphism

Rules and syntax for inheritance. The boring stuff

Chapter 15: Object Oriented Programming

Java Programming Lecture 7

Inheritance and Polymorphism

CS1004: Intro to CS in Java, Spring 2005

Arrays Classes & Methods, Inheritance

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

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

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Chapter 5 Object-Oriented Programming

ENCAPSULATION AND POLYMORPHISM

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

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

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

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

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

Binghamton University. CS-140 Fall Chapter 9. Inheritance

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

Polymorphism and Inheritance

Chapter 6 Introduction to Defining Classes

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

25. Generic Programming

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

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

Practice for Chapter 11

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

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

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

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

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Inheritance (continued) Inheritance

Making New instances of Classes

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

1 Shyam sir JAVA Notes

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

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

CSCE3193: Programming Paradigms

CH. 2 OBJECT-ORIENTED PROGRAMMING

CO Java SE 8: Fundamentals

Transcription:

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7 1 Problem Ralph owns the Trinidad Fruit Stand that sells its fruit on the street, and he wants to use a computer to track its produce. Their sale items include apples, bananas, and lemons. Starting from his old records, Ralph began to design his data structures, and made this sketch for some classes, their fields and methods: Apple Banana Lemon variety origin country cost weight weight weight isorganic cost cost getcost() getcost() getcost() print() print() print() core() peel() squeeze() Ralph then developed common methods for a FruitI interface. Below is Ralph s resulting interface and his initial Apple0 class. public interface FruitI { public int getcost(); // classes must implement! public void print(); class Apple0 implements FruitI { private int weight; private String variety; private final int CENTS_PER_OZ; public Apple0( int weight, int costperoz, String variety ) { this.weight = weight; this.cents_per_oz = costperoz; this.variety = variety; public void print() { System.out.println( this.variety + " Apple" ); public int getcost() { // Apple0 class implements a FruitI method return this.weight * CENTS_PER_OZ ; // core() and other code not shown 1

Ralph buys and sells his produce using weight and cost. When he buys in bulk, his cost is in cents per ounce, and he sells a piece of fruit based on its weight. His cost is the cost per ounce, and that cost does not change after he bought it. When he wants to know the cost, he wants to know his total cost for the fruit; that means getcost() must produce the cost for the particular weight of the fruit. Defining the interface solves the problem of treating different fruits as just some fruit. Since each class implements the same methods and can be referenced by values of the FruitI interface type, the approach supports polymorphism. While an interface allows Ralph to treat different fruits as just some fruit and collect or process multiple kinds of fruit as a collection, this approach has some issues. After writing Apple (Apple0) and Banana (Banana0), Ralph realized there would be a lot of duplication with all fruits as separate classes. The FruitI interface helped specify the common methods but not the common data fields. Ralph wants our help with his TrinidadFruitStand.java program. 2 Solution Analysis and Design 2.1 IS_A Relationships and Inheritance Inheritance creates IS_A relationships different from interfaces. What does the word inheritance mean? In a family, an inheritance is something that is passed down, such as a gene for blue eyes. Object-oriented inheritance relationships are similar; we can create classes that have an inheritance relationship. In OO terminology, we call the more general class a base class or a superclass. When we create another class that inherits from that superclass, we say we are creating a subclass, which extends the superclass. In Java, the keyword extends tells the Java compiler we are creating an inheritance relationship. Some books refer to the IS_A relationship as a parent-child relationship, in which the newborn child inherits characteristics from a parent. The parent class passes down attributes and methods (variables and code) to a child class. Any subclass instance we create inherits the fields and methods declared and defined in the superclass. This inheritance mechanism saves us from having to duplicate code in multiple, similar classes. The Java class libraries embody an extensive inheritance hierarchy. The class Object is the superclass from which all classes extend. Every class javadoc shows that the class extends from Object. 2.2 Design Evolution If we revise the fields a bit, we can find attributes common to apples, bananas and lemons, and we can do something similar with methods. We will write a FruitC 1 class that is a generalization that can hold common attribute values. For the fields and methods that apply only to one kind of fruit, we will create Apple, Banana, and Lemon classes that specialize the FruitC class. When 1. We have named this FruitC for reasons that will be come clear later. For now, think of the C suffix as one that means common. 2

we connect the Apple subclass to FruitC using inheritance, we will be able to construct an Apple object that also IS_A FruitC. We will proceed this way: Create version 1, v1, of an inheritance hierarchy by factoring common properties from specific classes and constructing the classes with testing along the way. Create version 2, v2, to make it more abstract by tuning the hierarchy to ensure it enforces the right behaviors. The accompanying Code.zip file contains directories for each version of the source code. 2.2.1 Factoring out a superclass We start with the Apple0 class and factor out the common characteristics into the FruitC superclass that our new Apple1 class extends. FruitC handles the fields and methods that should be common to all fruits. The remaining specifics of Apple0 move into the Apple1 subclass. Before implementing the hierarchy, we need to modify the initial data attributes based on an analysis of the similarities and differences between Apple, Banana and Lemon attributes. Since the cost per ounce and weight attributes are common; we can put these into the new, FruitC superclass. Similarly, we can rename country to origin and add the field to FruitC. Although isorganic is not in Apple or Lemon, each of these classes could have a value for that attribute. If we choose a false default value, then we can put isorganic into the FruitC superclass. That leaves the attribute variety as a field specific to Apple, and the other kinds of fruits have no additional specific attribute values. Our hierarchy is now taking shape. The FruitC class has fields to store the kinds of values common to the different kinds of fruit; information can be stored more consistently, in a location that is reusable by more than one subclass. What remains is to decide on the methods that FruitC should have. Since Java is our implementation language, we need a way to construct all the parts of our objects, and we can overload the tostring() method from the Object class. To design for Java, we replace print() with a tostring() that will allow clients to get a printable representation of the instance. FruitC Apple Banana Lemon costperoz weight origin isorganic variety FruitC(...) Apple() Banana() Lemon() tostring() getcost() core() peel() squeeze() 2.2.2 Constructing the classes Now we create the superclass and stub out its fields and methods. The FruitC constructor needs 4 parameters: weight, costperoz, origin, isorganic. However, since isorganic has a default value, 3

we can add a 3 parameter constructor with weight, costperoz, and origin. The 3 parameter constructor can call the 4 parameter constructor and pass the default value to it. Java syntax for one constructor to call another is this(). public class FruitC { FruitC( int weight, int costperoz, String origin, boolean isorganic ) { this.weight = weight; FruitC( int weight, int costperoz, String origin ) { this( weight, costperoz, origin, false ); // how java calls another constructor The getweight is common and easy to implement, getcost will return weight * costperoz, and tostring will return "Fruit". The getorigin and isorganic methods are trivial accessors, and there are no mutators because the field values should not change after construction. Now we create stubs for the subclasses and connect them to the superclass. The Java syntax for calling a superclass constructor is super( ), which must be the first statement inside the constructor. Another use for super is to invoke a method in a superclass. For example, suppose that the supplier adds a surcharge for a shipment of Apple, and that surcharge is one cent per Apple. The store might handle this by overriding FruitC.getCost and including the surcharge in the result of calling Apple1.getCost. The Apple1.getCost would call super.getcost to get the cost, add the surcharge, and return the sum. public class Apple extends FruitC { private String variety; public Apple( int weight, int costperoz, String origin, String variety ) { super( weight, costperoz, origin ); // must call super() first this.variety = variety; public int getcost() { // overrides getcost() to add surcharge return 1 + super.getcost() ; // make an up call to superclass 4

2.2.3 Testing We compile the individual classes to validate our code syntax. That should happen frequently, as we write every few methods or classes, to ensure that our code continues to obey the type specifications, interfaces, method names, and Java syntax. We can adapt the main program to test construction of the Apple, Banana, and Lemon instances and operation of their methods. We should make sure that we test these things: Constructors: the superclass has two constructors, and each subclass has at least one of its own constructors. getweight() getcost() getorigin() isorganic() tostring() While this main program performs some simple tests, such an approach would not be advisable for a full implementation of this application. We would want to develop this program into a more comprehensive test harness for the hierarchy. As the design and implementation proceeds, we would develop test cases in this test program. Finally we show a class diagram of our hierarchy using the Unified Modeling Language (UML). The solid line with the hollow arrowhead points from the specialized classes to the general class. The and + signs before the fields and methods symbolize private and public respectively. A UML class diagram shows class inheritance hierarchies and other class interrelationships. FruitC -costperoz: double -weight: double -origin: String -isorganic: boolean +FruitC( weight, cost, origin, isorganic ) +FruitC( weight, cost, origin ) +getweight() : int +getcost() : int + getorigin() : String + isorganic : boolean + tostring(): String Apple1 -variety: String +Apple1( ) +getcost(): int +getvariety(): String +core(): void +tostring(): String Banana1 -beenpeeled: boolean +Banana1( ) +getcost(): int +tostring(): String +peel(): void Lemon1 -beensqueezed: boolean +Lemon1( ) +getcost(): int +tostring(): String +squeeze(): void 5

2.3 Version 2: Abstracting the hierarchy 2.3.1 Abstract Classes One problem with our work makes no sense: it is possible to construct an instance of FruitC. We do not want FruitC to be a concrete class; we want it to be true that every instance of a FruitC is some actual, real kind of fruit. In terms of code, the statement below is legal but undesirable: FruitC fruit = new FruitC( 1, 1, "somewhere" ); To make it impossible for clients to construct an instance, we make a class abstract, and the compiler will enforce that the class cannot be instantiated. Our concrete, FruitC class becomes an abstract class, FruitA. public abstract class FruitA { // Making a class abstract is sufficient to prevent its instantiation even when it implements all the methods in its interface 2. 2.3.2 Abstract Methods If we add a method, getprice() for example, we learn that the implementation of this method depends on the specific subclass because the customer wants to use a different profit factor for different fruits. In that case, we want to make the method abstract in the superclass. Whenever that happens, we must also make the class itself abstract. Here is how an abstract class, FruitA, declares an abstract method: public abstract class FruitA { // other content including fields, constructors, constants, methods /** @return sale price of this fruit */ public abstract int getprice(); // there is a ; instead of a method body Other than the abstract keyword, the declaration of an abstract method has the same structure as the declaration of an interface method. 2.3.3 Instance Equality: defining equals() Now that we can subsume one type under another, we have to think about what equals means. When is one instance of Fruit equal to another? We should write the equals method to mean some sort of equivalence, but which should it be? type equality? value equality? weight equality? some other equality...? 2. Here we mean interface in the general sense, not in the Java syntax sense. 6

The application requirements may specify different needs for comparing instances for equality and equivalence. There are too many possibilities to be able to enumerate these easilly. In the case of the Trinidad Fruit Stand, we might suppose that two fruits are equivalent if they have the same kind of fruit (i.e. the same subclass), organic feature, and overall cost because this is what the store would tell clients and how it prices fruits. At a minimum for equals in an inheritance hierarchy, the code needs to recognize the class of an instance, cast the Object argument to that specific class, and compare the states of the instances, as shown in the following code fragment for a concrete subclass. public class Apple2 extends FruitA { // public boolean equals( Object obj ) { boolean equal = false; if ( obj instanceof Apple2 ) { // good start; same kind Apple2 other = (Apple2) obj; // casting to use specific methods if ( ) { // make comparisons using other reference equal = true; return equal ; // 7