CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Higher-Order Functions and Loops c. Kathi Fisler,
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Abstract Class and Interface
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Inheritance Writing and using Classes effectively.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Language Issues for Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.3 © Mitchell Wand, This work is licensed under a Creative.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Abstract classes and Interfaces. Abstract classes.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Object Orientated Programming in Perl Simulated Models of Termites.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
Under the Hood: A Map This slidedeck shows the various kinds of information that Java tracks under the hood as you create classes and objects and evaluate.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Introduction to Exceptions in Java CS201, SW Development Methods.
BY:- TOPS Technologies
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Mapping Methods With Arguments
Abstract Classes and Interfaces
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
CS2102: Lecture on Abstract Classes and Inheritance
Lesson 2: Building Blocks of Programming
Object Oriented Programming (OOP) LAB # 8
MSIS 670 Object-Oriented Software Engineering
Polymorphism and access control
Java – Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Abstract Classes and Interfaces
public class Dillo {     public int length;     public boolean isDead;         
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler

How to Use These Slides These slides walk you through how to share common code (i.e., create helper methods) across classes I recommend you download the starter file (posted to the website) and make the edits in the slides, step by step, to see what happens for yourself In the slides, green highlights what changed in the code from the previous slide; yellow highlights show Java compile errors Note any questions, and ask on the board or in the lecture-time chat

class Dillo implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return 2 <= this.length && this.length <= 3 ; } class Boa implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return 5 <= this.length && this.length <= 10 ; } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Notice the almost identical code Back to the Animals (code we had last week)

class Dillo implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return 2 <= this.length && this.length <= 3 ; } class Boa implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return 5 <= this.length && this.length <= 10 ; } We should create a helper method, but where can we put it? (remember, all methods must be in a class) interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Notice the almost identical code

class Dillo implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return 2 <= this.length && this.length <= 3 ; } class Boa implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return 5 <= this.length && this.length <= 10 ; } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } class AbsAnimal { } We will create a new class that abstracts over the common features of Dillo and Boa. We’ll call the new class AbsAnimal (“abs” for abstract)

class Dillo implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return 2 <= this.length && this.length <= 3 ; } class Boa implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return 5 <= this.length && this.length <= 10 ; } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } class AbsAnimal { // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } We will put a helper method for isNormalSize in AbsAnimal. We call the helper isLenWithin ; it takes the varying low and high values as inputs (but otherwise copies the common code, as usual when making a helper)

class AbsAnimal { // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } class Boa implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Next, we rewrite the original isNormalSize methods to call the helper method

class AbsAnimal { // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } class Boa implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } This is the right idea, but if we compile the Dillo and Boa classes, Java will complain that isLenWithin isn’t defined.

class AbsAnimal { // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } class Boa implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } This is the right idea, but if we compile the Dillo and Boa classes, Java will complain that isLenWithin isn’t defined. The problem is that we never connected Dillo and Boa to AbsAnimal.

class AbsAnimal { // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } class Boa extends AbsAnimal implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } We connect Dillo and Boa to AbsAnimal using a new Java keyword, extends, which says that one class ( Dillo/Boa ) includes the content of another ( AbsAnimal )

class AbsAnimal { // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } class Boa extends AbsAnimal implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Now, AbsAnimal won’t compile; Java will say that it doesn’t have a length variable.

class AbsAnimal { // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } class Boa extends AbsAnimal implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Now, AbsAnimal won’t compile; Java will say that it doesn’t have a length variable. But note that the length variable is also common to Dillo and Boa. It should also have moved to AbsAnimal

class AbsAnimal { int length; // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal implements IAnimal { int length; boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } class Boa extends AbsAnimal implements IAnimal { int length; String eats; Boa(int length, String eats) { this.length = length; this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Now, AbsAnimal won’t compile; Java will say that it doesn’t have a length variable. But note that the length variable is also common to Dillo and Boa. It should also have moved to AbsAnimal

class AbsAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal implements IAnimal { boolean isDead; Dillo(int length, boolean isDead) { this.length = length; this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } We need to add a constructor to AbsAnimal, and have it set the value of length [For sake of space, we will hide the Boa class (edits to Dillo apply to Boa as well)] Notice that we removed the length variable from Dillo

class AbsAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal implements IAnimal { boolean isDead; Dillo(int length, boolean isDead) { super(length); this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } We need to add a constructor to AbsAnimal, and have it set the value of length Notice that we removed the length variable from Dillo The Dillo constructor needs to send the length value to the AbsAnimal constructor

class AbsAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal implements IAnimal { boolean isDead; Dillo(int length, boolean isDead) { super(length); this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } In Java, super refers to the constructor for the class that this class extends; inside Dillo, super calls the AbsAnimal constructor. Notice that we removed the length variable from Dillo The Dillo constructor needs to send the length value to the AbsAnimal constructor

class AbsAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal implements IAnimal { boolean isDead; Dillo(int length, boolean isDead) { super(length); this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } In Java, super refers to the constructor for the class that this class extends; inside Dillo, super calls the AbsAnimal constructor. Whenever a class extends another class, its constructor should call super before doing anything else (i.e., the call to super should be the first statement in the method)

class AbsAnimal implements IAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal implements IAnimal { boolean isDead; Dillo(int length, boolean isDead) { super(length); this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Almost done. Since Dillo and Boa both implement IAnimal, we can move that to AbsAnimal as well

class AbsAnimal implements IAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal { boolean isDead; Dillo(int length, boolean isDead) { super(length); this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Here’s the final code class Boa extends AbsAnimal { String eats; Boa(int length, String eats) { super(length); this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); }

Recap so far When multiple classes need to share code (such as a helper method), put that code in a (parent) class that the sharing classes each extends Common variables and implements statements also move to the parent class If a class extends another class, its constructor should call super (to properly set up the contents of the superclass) Classes can use all variables and methods in their superclass

Facts about Extends Terminology: If class A extends class B, then (1) B is the superclass of A; (2) A is a subclass of B; (3) A is also said to inherit from B Restrictions: A class may have at most one superclass (ie, only extends one class), but arbitrarily many subclasses. [In contrast, a class can implement arbitrarily many interfaces.] Behavior: A class has access to all variables and methods of its superclass (there are exceptions, but we will discuss those later) Behavior: A class cannot access the variables or methods of its subclasses

BUT THERE ARE STILL SOME ISSUES TO ADDRESS …

class AbsAnimal implements IAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal { boolean isDead; Dillo(int length, boolean isDead) { super(length); this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } What if someone writes new AbsAnimal(8) ? What kind of animal does this yield? class Boa extends AbsAnimal { String eats; Boa(int length, String eats) { super(length); this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); }

class AbsAnimal implements IAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal { boolean isDead; Dillo(int length, boolean isDead) { super(length); this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } What if someone writes new AbsAnimal(8) ? What kind of animal does this yield? It doesn’t yield any known (or meaningful) kind of animal. AbsAnimal is only meant to hold code, it shouldn’t be used to create objects. We’d like to tell Java not to let anyone create objects from AbsAnimal class Boa extends AbsAnimal { String eats; Boa(int length, String eats) { super(length); this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); }

abstract class AbsAnimal implements IAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } class Dillo extends AbsAnimal { boolean isDead; Dillo(int length, boolean isDead) { super(length); this.isDead = isDead; } // determine whether this dillo's // length is between 2 and 3 public boolean isNormalSize () { return isLenWithin(2,3); } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } To tell Java not to let anyone create objects from a class, we annotate the class with the keyword abstract Now, the expression new AbsAnimal(8) would raise a Java error Rule of thumb: if a class only to hold common code, make it abstract class Boa extends AbsAnimal { String eats; Boa(int length, String eats) { super(length); this.eats = eats; } // determine whether this boa's // length is between 5 and 10 public boolean isNormalSize () { return isLenWithin(5,10); }

WHY DO WE NEED BOTH AN INTERFACE AND AN ABSTRACT CLASS?

abstract class AbsAnimal implements IAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Interfaces and abstract classes serve two very different purposes Interfaces are a form of types: they capture what a class must do, but they do not constrain how the class does something. As such, interfaces cannot contain code (beyond method input/output types) or variables Abstract classes are for sharing (abstracting over) data and code across multiple classes; they constrain how extending classes organize and use data Both roles are important, so OO programs often use both

abstract class AbsAnimal implements IAnimal { int length; // constructor AbsAnimal(int length) { this.length = length; } // determine whether animal’s // length is between low and high boolean isLenWithin (int low, int high) { return low <= this.length && this.length <= high ; } interface IAnimal { // determine whether animal's length // is within normal boundaries boolean isNormalSize(); } Interfaces and abstract classes serve two very different purposes If you already know some Java, you may have been taught to overuse class extension instead of interfaces. Interfaces are proper OO design practice (more on this through 2102) Imagine that we wanted to add fruit flies to our data. They are too small to have a length. Having IAnimal lets us write isNormalSize (to always return true) without having to specify a meaningless length value for a fruit fly.

What you should be able to do now … Use extends to share code among classes Use super in constructors Make a class abstract to prevent someone from creating objects from it Choose between using interfaces and (abstract) classes when designing programs

Some Study Questions Why didn’t we put isLenWithin in IAnimal ? Can AbsAnimal refer to the eats variable of Boa ? Could we have defined isNormalSize directly inside of AbsAnimal, instead of writing isLenWithin ? If so, how? If we wanted to write a doesEatTofu method on Boa, which class should it go into? Should it be mentioned in IAnimal ?

Experiments to Try on the Code Edit the posted starter file with the code from these notes, then experiment with the following: What error does Java give if you try to extend an interface or implement an abstract class? What error does Java give if you try to access a subclass variable in a superclass? If you forgot to delete the int length line from the Dillo class (after adding it to AbsAnimal ), what would Java do?