Upload
stacy-vasquez
View
33
Download
1
Tags:
Embed Size (px)
DESCRIPTION
So You Think You Know Inheritance…. Courtesy of Saqib Ashraf. You guys have already learned abstract, sub and super classes. Now lets take it up one more level!. Interfaces!. Courtesy of Saqib Ashraf. What is an interface?. - PowerPoint PPT Presentation
Citation preview
So You Think You Know Inheritance…
Courtesy of Saqib Ashraf
Springbrook High School page 2
You guys have already learned abstract, sub and super classes
Now lets take it up one more level!
Interfaces!
Courtesy of Saqib Ashraf
Springbrook High School page 3
What is an interface?-An interface is a type of an abstract
class that contains abstract methods. The methods in each interface provide a framework of behavior for any class.
-Interfaces are classes that may be adapted by more than one type of class.
-Interfaces usually represent something that “able” can be added to
Courtesy of Saqib Ashraf
Springbrook High School page 4
For Example
proSkaterObject
Rail Skateboard Curb
Grindable interface Each pro skater
object which is grindable,
implements the Grindable interface.
[little side note, this interface has “able” at
the end]
Courtesy of Saqib Ashraf
Springbrook High School page 5
How do we define interfaces?
public interface Grindable{
void showDamage();
Boolean isGrinded();
}
Declare it an “interface” class. Even though the class is abstract, for the class to be an interface, it needs to be declared
as one!
Create the methods and keep them un-instantiated. Since the class is an interface, the methods are understood to be abstract and do not need to be declared so. IMPORTANT
Interfaces do NOT contain instance
variables!!!!Courtesy of Saqib Ashraf
Springbrook High School page 6
How do we use interfaces?
public class Rail extends proSkaterObject implements Grindable
{
//variables and methods
}
We use an interface by using the key word
implements
The extends clause must precede the implements
clause
The class has its own methods and variables, as well as the methods
from the interface that are overridden
Courtesy of Saqib Ashraf
Springbrook High School page 7
An interface hierarchy usually looks like this:
Abstract Class
Subclass of
abstract class
Subclass of
abstract class
interface
Courtesy of Saqib Ashraf
Springbrook High School page 8
Let’s apply interfaces in the
real world
Courtesy of Saqib Ashraf
Springbrook High School page 9
Tony Hawk’s Pro Skater games are a great example of how interfaces are
used
Remember that pro skater hierarchy, lets revisit that…
Sick Grind
noob score!
Courtesy of Saqib Ashraf
Springbrook High School page 10
Refresher
pSObject
Rail Skateboard Curb
Grindable interface
This looks really confusing, but its not. Let’s break it down to
see where the interface is used
Courtesy of Saqib Ashraf
Springbrook High School page 11
Interface Hierarchy
Rail Curb
Grindable interface
This is just your ordinary inheritance hierarchy. Now, lets see how each class specifically uses the
interface
Courtesy of Saqib Ashraf
Springbrook High School page 12
A Closer Look[Gotta love those UML Diagrams]
interface Grindable
No instance variables allowed!
boolean isGrinded();
void showDamage();
Rail extends pSObject implements Grindable
//instance variables
boolean isGrinded();//check if object is grinded
void showDamage();//show sparks
Curb extends pSObject implements Grindable
//instance variables
boolean isGrinded();//check if object is grinded
void showDamage();//chipped pieces
Each subclass overrides the
methods of the interface
Interface is referenced by
using the implements
keyword
Courtesy of Saqib Ashraf
Springbrook High School page 13
interface Grindable
No instance variables allowed!
boolean isGrinded();
void showDamage();
Curb extends pSObject implements Grindable
//instance variables
boolean isGrinded();//check if object is grinded
void showDamage();//chipped pieces
Important:
Interfaces do NOT have instance
variables
Important:
The extends clause is ALWAYS before the implements clause
A Closer Look[Gotta love those UML Diagrams]
//instance variables
Rail extends pSObject implements Grindable
boolean isGrinded();//check if object is grinded
void showDamage();//show sparks
//instance variables
Courtesy of Saqib Ashraf
Springbrook High School page 14
-Interfaces are abstract, and their methods MUST remain un-instantiated
A couple of things to remember about interfaces
Courtesy of Saqib Ashraf
-Interfaces are abstract, and subclasses that don’t override their methods become abstract too!
-Classes can implement as many interfaces as they want
-The extends clause must ALWAYS come before the implements clause
Springbrook High School page 15
The Comparable Interface
The standard java.lang package includes the Comparable interface, which contains a very
useful method for comparing objects
public interface Comparable
{
int compareTo(Object obj);
}
Any class that implements
Comparable must override the
compareTo method!
Courtesy of Saqib Ashraf
Springbrook High School page 16
Analysis of the compareTo
methodpublic interface Comparable
{
int compareTo(Object obj);
}
-This method compares an implicit object[this]
with the parameter object “obj”.
-This method will return a negative integer, zero, or a positive integer depending
on whether the implicit object is less than, equal, or
larger than the “obj”
Quick side note: If the two objects being compared are not type
compatible, then a ClassCastException is
thrown.
Courtesy of Saqib Ashraf
Springbrook High School page 17
Courtesy of Saqib Ashraf
Springbrook High School page 18
Courtesy of Saqib Ashraf
Springbrook High School page 19
Courtesy of Saqib Ashraf