Interfaces and Abstract Classes in CS/ENGRD 2110 Spring 2016

undefined
 
CS/ENGRD 2110
SPRING 2016
 
Lecture 7: Interfaces and Abstract Classes
http://courses.cs.cornell.edu/cs2110
 
1
 
Announcements
 
2
 
Attendance for this week’s recitation is mandatory!
A2 is due Wednesday
Get started on A3 – do one method a day
 
Overview
 
3
 
Big Demo!
Interfaces
Abstract Classes
Normal Classes vs. Abstract Classes vs. Interfaces
Interfaces
4
/** A mutable collection of E values */
public
 
interface
 Collection<E> {
   /** Return true if this collection contains elem*/
   
boolean 
contains(Object elem);
   /** Ensure that this contains elem.
      * Return true if the collection is changed by this. */
   
boolean 
add(E elem);
}
New keyword
No fields!
No constructors!
Interfaces cannot
by 
new
ed!
Implementing Interfaces
5
/** A range of integers that always includes 0 */
public
 
class
 IntRange 
implements 
Collection<Integer> {
   
private int
 min = 0; 
// Represents the range min..max
   
private int
 max = 0; 
// min <= max
   /** Return true if elem is an integer in the range. */
   
public
 
boolean 
contains(Object elem) {…}
   /** Minimally extend the range to include elem.
      * Return true if the range had to be extended. */
   public
 
boolean 
add(Integer elem) {…}
}
Classes 
implement
 interfaces
Using Interfaces
6
/** Returns whether the collection contains every
   * integer between and including min and max.
   * Precondition: ints is not null */
public
 
static boolean 
containsRange(
  
Collection<Integer> ints,
  
int
 min, 
int
 max) {
 
for
 (
int
 i = min; i <= max; i++)
  
if
 (!ints.contains(i))
   
return false
;
 
return true
;
}
Because ints has type Collection<Integer>,
you can use any method declared in the
Collection<Integer> interface.
Works on any Collection!!!
This includes your own
DLinkedList from A3!
Interfaces
are types
Extending Interfaces
7
/** A mutable indexed list of E values */
public interface
 List<E> 
extends
 Collection<E> {
   
int 
size(); // return size of the list
   E get(
int
 index); // return elem at index
   E set(
int
 index, E elem); // change elem at index
   
boolean 
add(
int
 index, E elem); // insert elem at index
   E remove(
int
 index); // remove and return elem at index
}
Interfaces 
extend
 other interfaces
Implicitly includes all
methods in Collection<E>
Abstract Classes
8
/** Provides default implementations for list methods */
public 
abstract
 class 
AbstractList<E> 
implements
 List<E> {
   
public 
abstract
 int 
size();
   
public 
abstract
 
E get(
int
 index);
   
public 
abstract
 
E set(
int
 index, E elem);
   
public 
abstract
 boolean 
add(
int
 index, E elem);
   public 
abstract
 
E remove(
int
 index);
   
public boolean 
add(E elem) { 
return
 add(size(), elem); }
   
public boolean 
contains(E elem) {
 
for
 (
int
 i = 0; i < size(); i++)
  
if 
(!Objects.equals(elem, get(i)))
   
return
 
false
;
 
return
 
true
;
   }
}
Abstract classes
cannot be 
new
ed
Indicates that
subclasses are
responsible for
providing the
implementation
Only abstract
classes can have
abstract methods
Outdated use of abstract classes!
- see next slide on defaults
Defaults in Java 8
9
/** Provides default implementations for list methods */
public interface
 List<E> 
extends 
Collection<E> {
   int 
size();
   E get(
int
 index);
   E set(
int
 index, E elem);
   boolean 
add(
int
 index, E elem);
   E remove(
int
 index);
   
default
 boolean 
add(E elem) { 
return
 add(size(), elem); }
   
default
 boolean 
contains(E elem) {
 
for
 (
int
 i = 0; i < size(); i++)
  
if 
(!Objects.equals(elem, get(i)))
   
return
 
false
;
 
return
 
true
;
   }
}
Indicates that the
interface is
providing a 
default
implementation for
this method
Abstract Classes Revisited
10
public abstract class 
IntExpression {
   
private
 Integer value = null;
   
public int 
evaluate() {
 
if
 (value == null) value = eval();
 
return
 value.intValue();
   }
   
protected abstract 
int eval();
}
public class 
Zero 
extends
 IntExpression {
   
protected int 
eval() { 
return
 0; }
}
public class 
Sum 
extends
 IntExpression {
   
protected
 IntExpression left, right;
   
public
 Sum(…) {…}
   
protected int 
eval() { 
return
 left.eval() + right.eval(); }
}
Abstract class provides
common fields and
functionality
Abstract class leaves critical
methods abstract for
subclasses to implement
Comparison
11
Slide Note
Embed
Share

Explore the concepts of interfaces and abstract classes in the context of computer science with a detailed examination of their characteristics, implementations, and extensions. Learn how classes implement interfaces, utilize interface methods, and extend other interfaces to enhance functionality in programming. Delve into examples such as IntRange and List to grasp practical applications of these fundamental concepts.

  • Interfaces
  • Abstract Classes
  • Implementation
  • Programming Concepts
  • Computer Science

Uploaded on Oct 01, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. 1 CS/ENGRD 2110 SPRING 2016 Lecture 7: Interfaces and Abstract Classes http://courses.cs.cornell.edu/cs2110

  2. Announcements 2 Attendance for this week s recitation is mandatory! A2 is due Wednesday Get started on A3 do one method a day

  3. Overview 3 Big Demo! Interfaces Abstract Classes Normal Classes vs. Abstract Classes vs. Interfaces

  4. Interfaces Interfaces cannot by newed! 4 New keyword /** A mutable collection of E values */ publicinterface Collection<E> { /** Return true if this collection contains elem*/ boolean contains(Object elem); Always public /** Ensure that this contains elem. * Return true if the collection is changed by this. */ boolean add(E elem); } No constructors! No No fields! implementations!!!

  5. Implementing Interfaces 5 Classes implement interfaces /** A range of integers that always includes 0 */ publicclass IntRange implements Collection<Integer> { private int min = 0; // Represents the range min..max private int max = 0; // min <= max /** Return true if elem is an integer in the range. */ publicboolean contains(Object elem) { } /** Minimally extend the range to include elem. * Return true if the range had to be extended. */ publicboolean add(Integer elem) { } } Provides implementations for interface methods

  6. Using Interfaces 6 /** Returns whether the collection contains every * integer between and including min and max. * Precondition: ints is not null */ publicstatic boolean containsRange( Collection<Integer> ints, int min, int max) { for (int i = min; i <= max; i++) if (!ints.contains(i)) return false; return true; } Because ints has type Collection<Integer>, you can use any method declared in the Collection<Integer> interface. Works on any Collection!!! This includes your own DLinkedList from A3! Interfaces are types

  7. Extending Interfaces 7 Interfaces extend other interfaces /** A mutable indexed list of E values */ public interface List<E> extends Collection<E> { int size(); // return size of the list E get(int index); // return elem at index E set(int index, E elem); // change elem at index boolean add(int index, E elem); // insert elem at index E remove(int index); // remove and return elem at index } Implicitly includes all methods in Collection<E>

  8. Outdated use of abstract classes! - see next slide on defaults Abstract Classes 8 /** Provides default implementations for list methods */ public abstract class AbstractList<E> implements List<E> { public abstract int size(); public abstract E get(int index); public abstract E set(int index, E elem); public abstract boolean add(int index, E elem); public abstract E remove(int index); public boolean add(E elem) { return add(size(), elem); } public boolean contains(E elem) { for (int i = 0; i < size(); i++) if (!Objects.equals(elem, get(i))) returnfalse; returntrue; } } Abstract classes cannot be newed Indicates that subclasses are responsible for providing the implementation Only abstract classes can have abstract methods

  9. Defaults in Java 8 9 /** Provides default implementations for list methods */ public interface List<E> extends Collection<E> { int size(); E get(int index); E set(int index, E elem); boolean add(int index, E elem); E remove(int index); default boolean add(E elem) { return add(size(), elem); } default boolean contains(E elem) { for (int i = 0; i < size(); i++) if (!Objects.equals(elem, get(i))) returnfalse; returntrue; } } Indicates that the interface is providing a default implementation for this method

  10. Abstract Classes Revisited 10 public abstract class IntExpression { private Integer value = null; public int evaluate() { if (value == null) value = eval(); return value.intValue(); } protected abstract int eval(); } public class Zero extends IntExpression { protected int eval() { return 0; } } public class Sum extends IntExpression { protected IntExpression left, right; publicSum( ) { } protected int eval() { return left.eval() + right.eval(); } } Abstract class provides common fields and functionality Abstract class leaves critical methods abstract for subclasses to implement Subclasses provide case-dependent implementations

  11. Comparison 11 Normal Classes Abstract Classes Interfaces Features can be used as types and in casts can be newed have constructors can have fields can provide method implementations can have non-public methods can have abstract methods can be inherited multiply

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#