Abstract Class & Interface: Oum Saokosal, Head of IT Department
Abstract Class & Interface: Oum Saokosal, Head of IT Department
1
Interface
2
Interface
• What is interface?
• How to define interface?
• How to use interface?
• Why not use abstract class instead of interface?
• UML of interface
• Importance of interface
• Different between interface and abstract class
• Notes for interface
• Class Design Guidline
• Last words
3
What is Interface?
4
How to define interface?
• Defining an interface:
<modifier> interface InterfaceName{
/* Constant declarations */
/* Method signatures */
}
• E.g:
public interface Moveable{
final int MAX_MOVE = 20;
final int MIN_MOVE = 1;
public void move();
}
5
How to use interface? (1)
• Use implements keyword to implement an interface.
• E.g. Assume we have King implementing Moveable.
public interface Moveable{
final int MAX_MOVE = 20;
final int MIN_MOVE = 1;
public String howToMove();
}
public class King implements Moveable{
@Override
public String howToMove(){
return “One step to every direction”;
}
} 6
How to use interface? (2)
You can do this also:
public class Test extends JFrame implements Moveable{
public Test(){
setTitle(howToMove());
setSize(500, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public String howToMove(){
return “One step to every direction”;
}
}
7
Why not use abstract class instead of
interface? (1)
8
Why not use abstract class instead of
interface? (2)
CB: OK! Let me start my question. What is interface?
SR: Just like I said. Interface is a kind of ideas in
OOP which is similar to abstract class.
CB: Can you review what the abstract class is?
SR: Come on... You’ve learned it, haven’t you? You
should know it...!
CB: Well, but ... you know...
SR: - OK, no but. I can tell some...
- Actually, the most important thing of abstract
class is just to GUARANTEE that its closed
subclasses MUST override its abstract methods. 9
Why not use abstract class instead of
interface? (3)
CB: So...
SR: you have to make one of these two classes
(JApplet and Vehicle) to be interface.
Because you can inherit one class and implement
another class.
CB: But which one should I choose to be interface?
SR: It’s not a question!
CB: Why not?
SR: JApplet is a Java API. How can you change it to
interface? You can only change your Vehicle from
abstract class to interface. 14
Why not use abstract class instead of
interface? (8)
Runnable Character
+ run():void + getCharacter():String
+ getRole(): String
20
UML of Interface (2)
21
UML of Interface (3)
<<interface>> Fish
Runnable
+ run():void
<<interface>>
ActionListener
+ actionPerformed(e:ActionEvent ):void
22
Importance of interface (1)
• To define an interface have to mark interface
keyword instead of class keyword.
• Interface can contain only constants and abstract
methods. Note that we DON’T need to mark method
with abstract and all the methods are public.
public interface Moveable{
//Constants
final int MAX_MOVE = 20;
final int MIN_MOVE = 1;
//abstract methods
public String howToMove();
} 23
Differences between interface and
abstract class (1)
35