Abstract Classes - PowerPoint PPT Presentation

Loading...

PPT – Abstract Classes PowerPoint presentation | free to download - id: 8db03-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Abstract Classes

Description:

We need abstract classes ... makes sense to not allow certain classes to be instantiated ... Its subclasses, Button, Scrollbar, Checkbox are concrete classes. ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 20
Provided by: dUmn
Learn more at: http://www.d.umn.edu
Category:
Tags: abstract | classes

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Abstract Classes


1
Abstract Classes Interfaces
  • FMIS 3202 Enterprise System Architectures
  • Nik R. Hassan
  • Spring 2007

2
Unit Objectives
  • To learn why
  • We need abstract classes
  • We need interfaces (not to be confused with
    graphical user interfaces or GUIs)
  • To learn how
  • To handle abstract classes
  • And when to use interfaces
  • To create your own interfaces

3
Abstract Classes
  • You have understood how overriding and
    overloading can support the specialization of
    classes
  • Sometimes, it makes sense to not allow certain
    classes to be instantiated
  • E.g. you want every programmer to create Account
    classes, but you dont want them to create just
    plain Account. Because you want each Account
    class to have a specific behavior (e.g Current,
    Savings, CD accounts) you create an abstract
    Account class that cannot be instantiated into an
    object. That way no one is able to create just a
    plain Account.
  • E.g. In the Java API, the GUI Component is an
    abstract class. Its subclasses, Button,
    Scrollbar, Checkbox are concrete classes.
  • The opposite of abstractconcrete classesare
    classes that are specific enough to be
    instantiated into objects
  • The keyword to declare an abstract class is
    abstract placed before the class declaration
  • So an abstract class has virtually no use, no
    value, unless it is extended

4
More on abstract classes
  • An abstract class may have methods that contain
    implementations, but often, abstract classes
    contain abstract methods
  • An abstract method is a method that does not
    define a body (i.e. no curly braces, it only has
    a semi-colon)
  • Abstract classes are useful when it has some part
    implemented and certain parts that need to be
    implemented by its subclasses.
  • The implemented methods can be defined as final
    to enforce the implementation and not allow it to
    be overridden.
  • Any class that has an abstract method MUST be
    declared abstract

public abstract class Bank abstract void
doCheck() public final audit(Account
anyAccount) for (int i1 1 lt
anyAccount.max i) doCheck()

5
Interfaces
  • An interface is simply a named list of method
    declarations
  • Does not implement methods, only declares them
  • An interface is not a class, although it looks
    remarkably like an abstract class
  • Declared types in Java are either classes or
    interfaces
  • Interfaces represent a promise of support
    services to the objects which implement the
    interface a contract

public interface File public void
open(String name) public void close()
6
Protocols
  • An interface defines a protocol (a set of
    methods)
  • If a class implements a given interface, then
    that class implements that protocol
  • An interface can impose a common protocol on a
    group of classes that are not related by
    inheritance
  • In the chain of classes related by inheritance,
    the common protocol is imposed through subclassing

Comparable compareTo()
Date compareTo()
Integer compareTo()
String compareTo()
7
Declaring an Interface
  • Interface definition has two components
  • Interface declaration
  • Interface body
  • The interface declaration declares various
    attributes about the interface
  • Name
  • Whether it extends other interfaces

8
Implementing Interface Methods
  • Methods declared in an interface are implemented
    in the classes which support that interface

public class TextFile implements File public
void open(String name) // implementation
of open method public void close()
// implementation of close method
9
Syntax
  • In a class declaration, the naming of the
    superclass precedes any interfaces supported by
    the class

public class Directory extends Secure
implements File ...
10
Multiple Interfaces
  • If a class implements multiple interfaces, the
    interfaces are all listed, separated by commas

public class Directory implements
File, Secure ...
11
Implementing an Interface
  • A class which implements an interface must
    implement every method defined by that interface
  • If one or more methods is not implemented, Java
    will generate a compiler error
  • Subclasses automatically implement all interfaces
    that their superclass implements

12
Cloneable Interface
  • Some interfaces, such as Cloneable, do not
    contain any methods
  • In the case of Cloneable, it alerts the JVM that
    objects of the implementing class can be copied
    via the clone() method
  • These act as a signal to Java that a class
    observes a certain protocol that may not be
    expressible as a specific set of methods
  • Only the objects that implement the Cloneable
    interface can be copied, or cloned.

13
Typing and Interfaces
  • A variable's type can be an interface
  • Stipulations
  • Only objects whose class implements that
    interface can be bound to that variable
  • Only messages defined by the interface can be
    used
  • Interfaces cannot appear in a new expression

File r new File() // Error File f new
TextFile() // OK!
14
Subinterfaces
  • Interfaces can be extended
  • Interface hierarchy is independent of the class
    hierarchy
  • The interface which extends another interface
    inherits all of its method declarations

interface File public void open(String
name) public void close() interface
ReadableFile extends File public byte
readByte() interface WritableFile extends File
public void writeByte(byte b) interface
ReadWriteFile extends ReadableFile,
WritableFile public void seek(int
position)
15
Using Interfaces
  • Using interfaces allows
  • Cross-hierarchy polymorphism
  • Access to methods in separate class trees
  • Substitution of an object for another object
    which is not related via the class hierarchy
  • Classes that implement the same interface
    understand the same messages
  • Regardless of their location in the class
    hierarchy

16
More Advantages of Using Interfaces
  • Allows more control over how objects are used
  • The programmer can define a method's parameters
    as interfaces
  • This restricts the use of those parameters
  • The programmer knows which message the objects
    will respond to
  • Improves reusability of code

17
Naming Conventions for Interfaces
  • Make "able" interfaces
  • Cloneable, Serializable, ...
  • Name interfaces using proper nouns and provide
    "Impl" implementation of your interfaces
  • Bank, BankImpl, BankAccount, BankAccountImpl
  • With this convention, the interface typically
    contains a definition for all (or most) of the
    implementation class' public methods
  • Prefix interface names with "I" and use proper
    nouns for your classes
  • IBank, Bank, IBankAccount

18
Distributed Programming
  • Java's Remote Method Invocation (RMI) makes
    extensive use of Interfaces
  • The idea is to make the distinction between local
    and remote objects transparent
  • If you declare all variables to be of the
    interface type, then you don't have to care where
    the object is located!
  • Web Services takes advantage of a similar
    paradigm

LOCAL
REMOTE
client
service
stub
skeleton
19
What You Have Learned
  • How to define an interface
  • How classes implement interfaces
  • Interfaces allow developers to take advantage of
    cross-hierarchy polymorphism
  • Interfaces give developers more control over how
    their objects can be used
About PowerShow.com