Abstract Classes - PowerPoint PPT Presentation


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


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Abstract Classes


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
Tags: abstract | classes


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

Title: Abstract Classes

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

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

Abstract Classes
  • You have understood how overriding and
    overloading can support the specialization of
  • 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

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()

  • An interface is simply a named list of method
  • 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 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()
  • An interface defines a protocol (a set of
  • 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
  • In the chain of classes related by inheritance,
    the common protocol is imposed through subclassing

Comparable compareTo()
Date compareTo()
Integer compareTo()
String compareTo()
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

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
  • In a class declaration, the naming of the
    superclass precedes any interfaces supported by
    the class

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

public class Directory implements
File, Secure ...
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

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.

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
  • Interfaces cannot appear in a new expression

File r new File() // Error File f new
TextFile() // OK!
  • Interfaces can be extended
  • Interface hierarchy is independent of the class
  • 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
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

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

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

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

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