You are on page 1of 19

Abstract Classes

Interfaces Interfaces
Lecture08
Abstract Classes Abstract Classes
A class must be declared abstract if any of the
following condition is true
Th l h b t t th d The class has one or more abstract methods
The class inherits one or more abstract methods for which it
does not provide implementation
The class declares that it implements an interface but does
not provide implementation for every method of that
interface
An abstract class cannot be instantiated
Abstract classes defer the implementation to subclasses
The subclass must provide the implementation of the abstract
method or declare itself to be abstract in which case the
implementation is deferred once again
You can declare a variable of an abstract class
Example
public abstract class LivingThing { public abstract class LivingThing {
public void breath(){
System out println("Living Thing breathing "); } System.out.println( Living Thing breathing... ); }
public void eat(){
System.out.println("Living Thing eating..."); } y p ( g g g ); }
public abstract void walk();
}
public class Human extends LivingThing {
public void walk(){
System.out.println("Human walks..."); }
}
superclass of abstract class may be concrete p y
A b l b b t t if it l i t A subclass can be abstract even if its superclass is concrete.
class is concrete, but its subclasses, Object For example, the
, may be abstract. GeometricObject such as , y j
4
4
What is an Interface?
It defines a standard and public way of specifying the
behavior of classes behavior of classes
All methods of an interface are abstract methods
Defines the signatures of a set of methods without the Defines the signatures of a set of methods, without the
body (implementation of the methods)
A concrete class must implement the interface (all
the abstract methods of the Interface)
It allows classes, regardless of their locations in the
class hierarchy, to implement common behaviors
Example: Interface Example: Interface
public interface Relation { public interface Relation {
public boolean isGreater( Object a Object b); public boolean isGreater( Object a, Object b);
public boolean isLess( Object a, Object b);
public boolean isEqual( Object a Object b); public boolean isEqual( Object a, Object b);
}}
Implementing Interfaces Implementing Interfaces
public class Line implements Relation {
private double x1; private double x1;
private double x2;
private double y1;
private double y2;
public Line(double x1, double x2, double y1, double y2){
this.x1 = x1;
thi 2 2 this.x2 = x2;
this.y1 = y1;
this y2 = y2; } this.y2 = y2; }
public double getLength(){
double length = Math.sqrt((x2x1)*(x2x1) +
(y2 y1)* (y2 y1)); (y2y1) (y2y1));
return length; }
public boolean isGreater( Object a Object b){ public boolean isGreater( Object a, Object b){
double aLen = ((Line)a).getLength();
double bLen = ((Line)b).getLength();
t ( L > bL ) } return (aLen > bLen); }
public boolean isLess( Object a, Object b){
d bl L ((Li ) ) tL th() double aLen = ((Line)a).getLength();
double bLen = ((Line)b).getLength();
return (aLen < bLen);}
public boolean isEqual( Object a, Object b){
double aLen = ((Line)a).getLength();
double bLen = ((Line)b).getLength();
return (aLen == bLen); } }
Omitting Modifiers in Interfaces
public and all methods are static final public All data fields are
in an interface. For this reason, these modifiers can be abstract
omitted, as shown below:

public interface T1 {
public static final int K = 1;

public abstract void p();
Equivalent
public interface T1 {
int K = 1;

void p();
}
}
A constant defined in an interface can be accessed using A constant defined in an interface can be accessed using
). .K 1 T (e.g., InterfaceName.CONSTANT_NAME syntax
9
9
The final Modifier
The final class cannot be extended:
fi l l M th { final class Math {
...
}
The final variable is a constant:
fi l t ti d bl PI 3 14159 final static double PI = 3.14159;
The final method cannot be overridden by its y
subclasses.
10
NOTE
The modifiers are used on classes and class
members (data and methods) except that the members (data and methods), except that the
modifier can also be used on local variables final
in a method. A final local variable is a constant in a method. A final local variable is a constant
inside a method.
11
Why do we use Interfaces?
Reason #1 Reason #1
To reveal an object's programming interface
(f i li f h bj ) i h li (functionality of the object) without revealing
its implementation
This is the concept of encapsulation
The implementation can change without affecting
h ll f h f the caller of the interface
The caller does not need the implementation at
th il ti the compile time
It needs only the interface at the compile time
During runtime actual object instance is associated During runtime, actual object instance is associated
with the interface type
Why do we use Interfaces?
Reason #2
To have unrelated classes implement similar methods
(behaviors)
One class is not a subclass of another
Example: Example:
Class Line and class MyInteger
They are not related through inheritance
Y b h i l i h d You want both to implement comparison methods
checkIsGreater(Object x, Object y)
checkIsLess(Object x, Object y)
h kI E l(Obj t Obj t ) checkIsEqual(Object x, Object y)
Define Comparison interface which has the three abstract
methods above
Why do we use Interfaces?
Reason #3
To model multiple inheritance To model multiple inheritance
A class can implement multiple interfaces while it
can extend only one class can extend only one class
Interface vs. Abstract Class
All methods of an Interface are abstract methods while
some methods of an Abstract class are abstract
methods
Abstract methods of abstract class have abstract modifier
An interface can only define constants while abstract
class can have fields
Interfaces have no direct inherited relationship with Interfaces have no direct inherited relationship with
any particular class, they are defined independently
Interfaces themselves have inheritance relationship
among themselves
Interface as a Type Interface as a Type
If you define a reference variable whose type is an If you define a reference variable whose type is an
interface, any object you assign to it must be an
instance of a class that implements the interface
Let's say Person class implements PersonInterface
interface
Person p1 = new Person();
PersonInterface pi1 = p1;
PersonInterface pi2 = new Person();
Example: Implementing Multiple
f Interfaces
public class ComputerScienceStudent extends Student implements
PersonInterface, AnotherInterface, Third interface
{{
// All abstract methods of all interfaces
// need to be implemented.
}}
An interface can only be implemented by classes or extended by other
interfaces
Inheritance Among Interfaces Inheritance Among Interfaces
Interfaces are not part of the class hierarchy Interfaces are not part of the class hierarchy
However, interfaces can have inheritance
relationship among themselves relationship among themselves
public interface PersonInterface { public interface PersonInterface {
void doSomething();}
public interface StudentInterface extends PersonInterface {
void doExtraSomething();} void doExtraSomething();}
Homework
Q1:Problem of Rewriting an Existing Interface.
S Ch i f d dd Suppose we Change an interface and add
some functions in it. What would happen to
h l i l i hi i f P id the class implementing this interface. Provide
a solution.
Q2: When to use abstract classes over
interface. Explain

You might also like