Abstract Classes

Download Report

Transcript Abstract Classes

Abstract Classes

An abstract class is a placeholder in a class hierarchy
that represents a generic concept

An abstract class cannot be instantiated

We use the modifier abstract on the class header to
declare a class as abstract

An abstract class often contains abstract methods (like
an interface does), though it doesn’t have to
Abstract Classes





The child of an abstract class must override the abstract
methods of the parent, or it too will be considered abstract
An abstract method cannot be defined as final (because it
must be overridden) or static (because it has no definition
yet)
The use of abstract classes is a design decision; it helps us
establish common elements in a class that is to general to
instantiate
Usually concrete classes extend abstract ones, but the
opposite is also possible.
Can an abstract method be private?
References and abstract classes

Suppose the following two classes were defined:


public abstract class Figure
public class Rectangle extends Figure

Are these instantiations correct?
Rectangle r = new Rectangle(...);
Figure f
= new Rectangle(...);
Figure f
= new Figure(...);
3
Interfaces

A Java interface is a collection of abstract methods and
constants

An abstract method can be declared using the modifier
abstract, but because all methods in an interface are
abstract, it is usually left off

An interface is used to formally define a set of methods
that a class will implement

A class can implement multiple interfaces

The interfaces are listed in the implements clause,
separated by commas
Interface example


We want to define an interface so that classes which
implement it have meaningful string representation
Will the following definition accomplish this purpose?
interface Printable {
String toString();
}

Is this one different?
interface Printable {}


Such interfaces are called marker interfaces
java.lang.Cloneable is another marker interface
5
Interface inheritance I



Just as class extends class an interface can extend
interface
Unlike classes interface can extend more than one
interface
Thus in Java we have
- single implementation inheritance
- multiple interface inheritance
6
Interface inheritance II
public interface MyInterface1 {
void method1();
}
public interface MyInterface2 {
String method2();
}
public interface MyInterface3 extends MyInterface1, MyInterface2 {
void method3();
}

If you write a class that implements MyInterface2 you will
have to implement method1() ,method2() and method3() .
7
When to use interfaces?

Interfaces allow multiple inheritance

Inheriting from a class allows implementation
inheritance but class can extend only one other class

Any major class should be an implementation of an
interface.
8
Polymorphism via Interfaces

An interface defines a type just like any class in java.

This means you can use interface names wherever you
used primitive or object types.

For example look at FunctionDrawingApplet of Ex. 12
9