Abstract Method & Abstract Classes

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
CS2110: SW Development Methods
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
The Java Collections Framework (JCF) Introduction and review 1.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Programming in Java CSCI-2220 Object Oriented Programming.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Object Oriented Programming in Java Habib Rostami Lecture 7.
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Chapter 19 Java Data Structures
A tree set Our SearchTree class is essentially a set.
Implementing ArrayList Part 1
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Week 6 Object-Oriented Programming (2): Polymorphism
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
A tree set Our SearchTree class is essentially a set.
Collections in Java The objectives of this lecture are:
Collections James Brucker.
Grouped Data Arrays, and Array Lists.
CS2110: Software Development Methods
ArrayLists 22-Feb-19.
Collections Framework
Introduction to Inheritance
Based on slides by Alyssa Harding & Marty Stepp
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Chapter 14 Abstract Classes and Interfaces
ArrayLists 27-Apr-19.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Part of the Collections Framework
CSE 143 Lecture 21 Advanced List Implementation
Java Generics & Iterators
Presentation transcript:

Abstract Method & Abstract Classes Jim Brucker

What is an Abstract Method? An abstract method is a method declaration without a method body. An abstract method specifies behavior but no implementation. Example: In the Number class, intValue, longValue, ... are abstract. public abstract int intValue( ) ; public abstract long longValue( ) ; Methods declared to be abstract along with other qualifiers (public, int, "throws ..."). Use semi-colon to end the method declaration.

Interface Methods are Abstract All the methods in an interface are abstract: public interface Comparable { public int compareTo(Object o); } is the same as: public interface Comparable { public abstract int compareTo(Object o); }

Class with Abstract Method A class can have abstract methods. Example: The Number class has. are abstract methods intValue(), longValue(), and more public abstract class Number { public abstract int intValue( ) ; public abstract long longValue( ) ;

Abstract Classes A class with an abstract method is an abstract class. You must write "abstract class" in declaration. You cannot create objects (instances) of abstract class. Error: Number num = new Number( ); public abstract class Number { public abstract int intValue( ) ; public abstract long longValue( ) ; ...etc...

OK for type declaration This is OK because Double is a concrete subclass: Number pi = new Double(3.14159);

What Can You Put in Abstract Class? An abstract class can contain anything that a normal class can contain. public abstract class Money implements Comparable<Money> { static final String CURRENCY = "Baht"; public Money( ) { ... } public abstract int getValue( ); // not abstract public int compareTo(Money m) { ... }

Why Use Abstract Classes? So you don't have to sleep at the office.

Assignment: Write a List Your Boss: I want you to write a List that stores elements in the Cloud. Call it "CloudList". You: No problem. Your Boss: We need it tomorrow. <<interface>> List CloudList

At work in your cubicle... Easy... just implement a List using our CloudIO package <<interface>> List CloudList uses services of <<package>> CloudIO (company software)

Open up the List API doc ... 23 Methods Let's see... what do I have to implement ? <<interface>> List add( E ): bool add(int, E ): void addAll( Collection ) clear( ) contains(Object) containsAll(Collection) equals(Object): bool get(int): E hashCode( ): int indexOf(Object) isEmpty( ) iterator( ): Iterator<E> lastIndexOf(Object) remove(int): E ... 23 Methods Try it in Eclipse: create a class that implements List, using Java language level 7.0 (not 8.0)

Mission IMPOSSIBLE 23 Methods There HAS TO be an EASIER way! List <<interface>> List add( E ): bool add(int, E ): void addAll( Collection ) clear( ) contains(Object) containsAll(Collection) equals(Object): bool get(int): E hashCode( ): int indexOf(Object) isEmpty( ) iterator( ): Iterator<E> lastIndexOf(Object) remove(int): E ... 23 Methods

AbstractList to the Rescue <<interface>> List 23 Abstract Methods Extend AbstractList. It implements most methods for you. AbstractList get( ): E {abstract} size( ): int {abstract} Only 2 abstract methods CloudList add( E ): bool get( ): E remove( int ): bool size( ): int You should also override a few more, like add( ) and remove( ). In Java 8, you have to do more work than this.

Other Examples of Abstract Classes An interface specifies required behavior. An abstract class provides a skeleton or convenience class for implementing the interface. Interface Abstract Class that implements it... MouseListener (5 methods) MouseInputAdapter (0 abstract methods) Set (15 methods) AbstractSet (2 abstract methods) Action (6 methods) AbstractAction (1 abstract method)

Interface or Abstract Class? Q: What is the advantage of using an interface instead of an Abstract Class to specify behavior? abstract class AbstractFunction { /** function specification: no implementation */ abstract public double f( double x ) ; } Abstract method does not have a body. public class MyApplication extends AbstractFunction { /** implement the method */ public double f( double x ) { return x/(x+1); } ... }

Why Use Abstract Classes? Many applications are designed to work with objects of many different classes. The application (or framework) accepts objects of the base class as parameter. Application Abstract Base Class provides some behavior uses extends This application "thinks" it is using an instance of the base class; in fact it is using an instance of your class. Your Class extends and customizes the behavior

<<interface>> Depend on Interfaces A better design is for application to depend on interfaces, but also provide abstract base class to help programmer implement the interfaces. <<interface>> SomeInterface uses Application Abstract Base Class provides some behavior participates in Program to an interface, not to an implementation. Your Class extend and customize the behavior

Example of Abstract Classes A Java GUI application is built using objects of a class named java.awt.Component. Component is an abstract base class real components (Buttons, Boxes, ...) are subclasses of Component Containers that manage components "think" that all components look & behave like Component. //API: Container.add( Component c ) container.add( new JButton("Press me") ); container.add( new JLabel("Get a life.") ); container.add( new JComboBox( array ) );

Swing & Abstract Classes Each real component extends Component and overrides the behavior that it wants to specialize. Benefit: 1) any Component can be put in any Container (like JPanel) 2) we can create our own component by extending Component. We don't need to rewrite most methods from Component. Component Button Checkbox Label TextComponent JComponent JButton JCheckbox JLabel JTextComponent

Inheritance & Interface for Coin Purse Discuss and design in class: We want the Coin Purse to accept many kinds of money, such as Coin, BankNote, Check, and even KU Coupons (from KU Fair). How can we use interface to make Purse polymorphic? How can we use abstract classes to reduce coding and duplicate code?