SlideShare a Scribd company logo
Chapter 9 Abstract Class & Interface Oum Saokosal , Head of IT Department National Polytechnic Institute of Cambodia Tel: (855)-12-417214 E-mail: oum_saokosal@yahoo.com
Interface
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
What is Interface? In many ways, Interface is very similar to abstract class but it marks with  interface  keyword instead of  abstract class  keyword. public  interface  InterfaceName { } Unlike abstract class which can also contain nonabstract methods, interface contain  ONLY   abstract methods  and  constants .
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(); }
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”; } }
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”; } }
Why not use abstract class instead of interface? (1) Hey Rabbit! Today’s class is about interface. The word “interface” here means GUI, Graphical User Interface? Not at all, Ball. GUI is GUI. Interface here is a kind of ideas in OOP which is similar to abstract class. I’ll tell ya what...
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.
Why not use abstract class instead of interface? (3) CB: Why guarantee? SR: Because of polymorphism. Do you remember the form of polymorphism. CR: Like this: Vehicle v = new Jeep(); v.getGun(); SR: And what wrong if  Jeep  didn’t override  getGun()  method? CR: Oh I see. Then it will call  getGun()  from the parent  Vehicle . Because Jeep didn’t..., I see!
Why not use abstract class instead of interface? (4) SR: So how to guarantee or to force ITS CLOSED SUBCLASSES to override every methods in superclass? CB: Hmmm, we make the superclass to be abstract class and so do the methods. SR: That’s right. The interface acts the SAME things. CR: I see. I think if the abstract class and interface are the same, why we need interface?  And WHY DON’T WE USE ABSTRACT CLASS INSTEAD OF INTERFACE?
Why not use abstract class instead of interface? (5) SR: - It’s such a good question?   - You know, in some other languages like C++, a class can inherit multiple superclasses which is known  multiple inheritance .  Java, however, does not allow multiple inheritance. That is, a class can only have a  single inheritance . CB: So what’s wrong with the single inheritance? SR: Well, A class can inherit only one superclass. Like: public class Jeep  extends  Vehicle { }
Why not use abstract class instead of interface? (6) CB: Yes. It is right.  Vehicle  must be a parent of  Jeep . SR: What about if we want to use  JApplet  in  Vehicle  as well ? CB: Then we can do like this: public class Jeep  extends JApplet, Vehicle { } SR: That’s what I’m talking about! You cannot have multiple inheritance in Java. If you still want to do like this, you will get  compile error.   And You know, you can’t do this in other languages like ActionScript or C# as well. You cannot!!!
Why not use abstract class instead of interface? (7) 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.
Why not use abstract class instead of interface? (8) CB:OK. Can you change  Vehicle  to interface for me? SR: Not again. You should know to do it !!! CB: I don’t know. That’s why I ask you! SR: OK. Here is the code of  abstract class : public  abstract class  Vehicle { public  abstract  String getTradeMark(); public  abstract  double getSpeed();   public  abstract  boolean hasWeapon(); }
Why not use abstract class instead of interface? (9) SR: And here is the change to interface: public  interface  Vehicle { public String getTradeMark(); public double getSpeed();  public boolean hasWeapon(); } CB: I think you didn’t mark methods with  abstract   keyword! SR: That’s OK. Mark it or not, it’s the same. It’s always abstract automatically.
Why not use abstract class instead of interface? (10) CB: Any change with the  Jeep  class? SR: Of course, but  a bit. Just from  extends  to  implements . public class Jeep  implements  Vehicle {   public String getTradeMark(){   return “Jeep”;   } public double getSpeed(){   return 250;   }  public boolean hasWeapon(){   return true;   } }
Why not use abstract class instead of interface? (11) CB: I see. It’s great! SR: Even greater than this, Java allow to have MULTIPLE INTERFACE. That is, you can inherit only one class, but you can have many interfaces.  E.g.: public class Fish  extends  ChessCharacter  implements  Runnable, ActionListener,  KeyListener  { /*Overridden methods are here*/ } CB: Wow, wonderful. Now I know  why we use interface instead of abstract class .
Why not use abstract class instead of interface? (12) SR: However, You should know that in  interface , you CANNOT include  nonabstract methods  at all; whereas in  abstract class , you can MIX  nonabstract and abstract methods together .  CB: You mean, if we make an  abstract class , then its  subclasses could   reuse some nonabstract  methods without override. SR: That’s right... and... CB: On the other hands, if we make an  interface , then classes that implement the interface  MUST override every method . SR: Yes.. You’re right.
UML of Interface (1) In some old books, you could see UML notation to descript an interface like this: Interface abstract class Runnable +  run():void Character +  getCharacter():String + getRole(): String
UML of Interface (2) But in UML 2.0, to make a difference from the abstract class and the interface notation, they do like this: Interface abstract class << interface >> Runnable +  run():void Character +  getCharacter():String + getRole(): String
UML of Interface (3) And  a line  to indicate that a class implements an interface is  dotted line  with  white arrow head . <<interface>> Runnable +  run():void Fish <<interface>> ActionListener +  actionPerformed (e:ActionEvent ):void
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(); }
Differences between interface and abstract class (1) Like abstract class,   if a class implement an interface, you have to  override  the interface’s methods in the class. You cannot create instances from an interface by using  new  operator. Interface can be a  type  as well. Runnable r; the purpose of creating interface is because of  polymorphism .
Differences between interface and abstract class (2) Unlike abstract class, You can have  multiple interface  in one class. To implement those interfaces, use  implements   keyword and separate interfaces by  comma . public class Test  implements  Runnable ,   ActionListener ,  MouseMotionListener { /* Overridden interfaces methods */ } 2. Interface uses  interface  keyword. Interface is NOT designed to be  superclass . Interface is designed to  add some behaviors  to a class.
Different between interface and abstract class (3) In the relationships, we say that: 4.1.  A relationship between  class/abstract class and class   is a strong relationship. It is known as  IS-A  relationship.  E.g:  A duck is a bird .  It clearly means the duck is really a bird. So the bird can be a superclass of a duck. It could be either  concrete  or  abstract class . 4.2.  A relationship between  class and interface   is a weak relationship. It is known as  Is-kind-of  relationship.  E.g:  A   duck is flyable .  Flyable can never ever be the superclass of the duck. It just means this duck can fly. So flyable is  interface .
Notes for Interface (1) Because the interface is just designed to add some behaviors or some features to classes, usually it contains only  one or two general methods . E.g.: public interface Runnable { void run(); }  The reason for this is that interface is not a superclass. So it doesn’t specify who can use its methods. Generally,  its method might be used by everyone .
Notes for Interface (2) By Java code conversion, the name of interface is usually  adjective . Because adjective adds some meaning to a noun. That’s why you’ll see: - Runnable - Comparable - Cloneable - Accessible The interface names for event driven listener are usually ended with  Listener . E.g. - ActionListener - MouseMotionListener - KeyListener
Class Design Guildlines  p. 358-360 A class should descript a  single  entity or a single thing. Classes are usually designed for  reuse  by  many  different customers and developers. If we want  polymorphism , you have to use  inheritance  design (of course,  overridden method ).  If NO need polymorphism, use  composite  design. If a relationship is  strong  like apple and fruit, please use  abstract class  design. If a relationship is  weak  like apple and eatable, please use  interface  design.
Last Words (1) CB: Hi Rabbit. After I know Class Encapsulation, Inheritance, Polymorphism, Abstract Class and Interface, I think I know OOP. What should I do after all? SR: Well. First, you can learn Java of course. I mean you can understand Java faster than ever.  CB: Oh really? SR: Yeah... Because the difficult parts of Java is to understand OOP. I means you have to know OOP in order to learn Java. CB: What about people claim that they can write Java programs but they say they don’t need to know OOP. SR: It sounds cheating, you know! Of course, it is possible if they really work day-in day-out with Java. But they don’t know what is going on inside. Also, it takes so much time to be able to write Java without understand OOP.
Last Words (2) CB: I quess so but anyway. Back to my questions, which Java topics should I learn further? SR: You know, Java is huge. The reason for this is because Java wants every thing work in Java.  CB: What can work in Java? SR: You use, for example, JavaSE for Desktop Application and Applet. JavaEE is very big and it is for big enterprise. You can make JSP, Servlet, Web Applications, Web Services, JavaBean, Java Server Face and many more. CB: Wow, I’ve never heard all of these! SR: You can also use JavaME for mobile devices, PDA, etc. There are always a lot more to learn! Is it scary?
Last Words (3) CB: Oh my...  It is more enormous than I’ve ever imagined. SR: Actually, if you really want to learn them all, you will take your whole life to learn. But ... but... you know, after OOP, the rest of it is only how to understand and to use Java API library.  CB: What do you mean? SR: I mean there’s no more strange things like what is inheritance, polymorphism, abstract class or interface. Because you know all of it. CB: Yes. but API... SR: After you knew OOP, you can use Java API. For example, to make a button clickable, you have to use an interface called ActionListener. And them use polymorphism to make it work.
Last Words (4) CB: Heu heu... SR: And to use JFrame, JApplet, and MIDlet, you have to use inheritance. CB: Oh yeah... SR: And something like this. The key point is to realize which way to use this API. I mean it is to use inheritance, polymorphism or interface? CB: OK. I got it. SR: One more suggestion. After you know OOP, you should learn a specific topic in Java rather learn them all.
Last Words (4) CB: How can I choose? SR: For example, if you like to work with Web Application, please go to Serlvet, JSP and Database Programming. CB: What about if I like to make online game. SR: This one? You could go to Swing, Applet, Multiple Threading and distributed computing. But it is hard one. CB: Any thing else? SR: Of course. You can also learn JavaME for Mobile Phone. It is a hot topic for today. Everybody always ask me. CB: Really. Maybe I could make a decision now. I choose JavaME. SR. Yehh I choose the good one.  So let go get ‘em!
Bye... Bye..
Ad

More Related Content

What's hot (20)

8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
Tuan Ngo
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Java interface
Java interfaceJava interface
Java interface
Md. Tanvir Hossain
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
Linh Lê
 
Interface in java
Interface in javaInterface in java
Interface in java
Lovely Professional University
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
Haris Bin Zahid
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
Edureka!
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interface
Mazharul Sabbir
 
Abstract class
Abstract classAbstract class
Abstract class
Hoang Nguyen
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
Abishek Purushothaman
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
Edureka!
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
Ahmed Nobi
 
Interface java
Interface java Interface java
Interface java
atiafyrose
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Class Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum SaokosalClass Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum Saokosal
OUM SAOKOSAL
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
Tuan Ngo
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
Linh Lê
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
Haris Bin Zahid
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
Edureka!
 
Abstract class and interface
Abstract class and interfaceAbstract class and interface
Abstract class and interface
Mazharul Sabbir
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
Edureka!
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
Ahmed Nobi
 
Interface java
Interface java Interface java
Interface java
atiafyrose
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Class Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum SaokosalClass Diagram | OOP and Design Patterns by Oum Saokosal
Class Diagram | OOP and Design Patterns by Oum Saokosal
OUM SAOKOSAL
 

Viewers also liked (20)

Actionscript 3 - Session 7 Other Note
Actionscript 3 - Session 7 Other NoteActionscript 3 - Session 7 Other Note
Actionscript 3 - Session 7 Other Note
OUM SAOKOSAL
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
Actionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 InteractivityActionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 Interactivity
OUM SAOKOSAL
 
Actionscript 3 - Session 5 The Display Api And The Display List
Actionscript 3 - Session 5 The Display Api And The Display ListActionscript 3 - Session 5 The Display Api And The Display List
Actionscript 3 - Session 5 The Display Api And The Display List
OUM SAOKOSAL
 
SkillsMatter - In-the-Brain session - What's new in ActionScript 3.0
SkillsMatter - In-the-Brain session - What's new in ActionScript 3.0SkillsMatter - In-the-Brain session - What's new in ActionScript 3.0
SkillsMatter - In-the-Brain session - What's new in ActionScript 3.0
Peter Elst
 
Actionscript 3 - Session 1 Introduction To As 3
Actionscript 3 - Session 1 Introduction To As 3Actionscript 3 - Session 1 Introduction To As 3
Actionscript 3 - Session 1 Introduction To As 3
OUM SAOKOSAL
 
Actionscript 3 - Session 3 Action Script And Flash
Actionscript 3 - Session 3 Action Script And FlashActionscript 3 - Session 3 Action Script And Flash
Actionscript 3 - Session 3 Action Script And Flash
OUM SAOKOSAL
 
Action Script
Action ScriptAction Script
Action Script
Angelin R
 
Measuring And Defining The Experience Of Immersion In Games
Measuring And  Defining The  Experience Of  Immersion In GamesMeasuring And  Defining The  Experience Of  Immersion In Games
Measuring And Defining The Experience Of Immersion In Games
OUM SAOKOSAL
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
Saurabh Narula
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
OUM SAOKOSAL
 
ITS (Intelligent Teleportation System)
ITS (Intelligent Teleportation System)ITS (Intelligent Teleportation System)
ITS (Intelligent Teleportation System)
OUM SAOKOSAL
 
Terminology In Telecommunication
Terminology In TelecommunicationTerminology In Telecommunication
Terminology In Telecommunication
OUM SAOKOSAL
 
Tutorial 1
Tutorial 1Tutorial 1
Tutorial 1
Bible Tang
 
Kimchi Questionnaire
Kimchi QuestionnaireKimchi Questionnaire
Kimchi Questionnaire
OUM SAOKOSAL
 
Rayleigh Fading Channel In Mobile Digital Communication System
Rayleigh Fading Channel In Mobile Digital Communication SystemRayleigh Fading Channel In Mobile Digital Communication System
Rayleigh Fading Channel In Mobile Digital Communication System
OUM SAOKOSAL
 
Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - Swing
OUM SAOKOSAL
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with ExamplesJavascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
OUM SAOKOSAL
 
Actionscript 3 - Session 7 Other Note
Actionscript 3 - Session 7 Other NoteActionscript 3 - Session 7 Other Note
Actionscript 3 - Session 7 Other Note
OUM SAOKOSAL
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
Actionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 InteractivityActionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 Interactivity
OUM SAOKOSAL
 
Actionscript 3 - Session 5 The Display Api And The Display List
Actionscript 3 - Session 5 The Display Api And The Display ListActionscript 3 - Session 5 The Display Api And The Display List
Actionscript 3 - Session 5 The Display Api And The Display List
OUM SAOKOSAL
 
SkillsMatter - In-the-Brain session - What's new in ActionScript 3.0
SkillsMatter - In-the-Brain session - What's new in ActionScript 3.0SkillsMatter - In-the-Brain session - What's new in ActionScript 3.0
SkillsMatter - In-the-Brain session - What's new in ActionScript 3.0
Peter Elst
 
Actionscript 3 - Session 1 Introduction To As 3
Actionscript 3 - Session 1 Introduction To As 3Actionscript 3 - Session 1 Introduction To As 3
Actionscript 3 - Session 1 Introduction To As 3
OUM SAOKOSAL
 
Actionscript 3 - Session 3 Action Script And Flash
Actionscript 3 - Session 3 Action Script And FlashActionscript 3 - Session 3 Action Script And Flash
Actionscript 3 - Session 3 Action Script And Flash
OUM SAOKOSAL
 
Action Script
Action ScriptAction Script
Action Script
Angelin R
 
Measuring And Defining The Experience Of Immersion In Games
Measuring And  Defining The  Experience Of  Immersion In GamesMeasuring And  Defining The  Experience Of  Immersion In Games
Measuring And Defining The Experience Of Immersion In Games
OUM SAOKOSAL
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
Saurabh Narula
 
ITS (Intelligent Teleportation System)
ITS (Intelligent Teleportation System)ITS (Intelligent Teleportation System)
ITS (Intelligent Teleportation System)
OUM SAOKOSAL
 
Terminology In Telecommunication
Terminology In TelecommunicationTerminology In Telecommunication
Terminology In Telecommunication
OUM SAOKOSAL
 
Kimchi Questionnaire
Kimchi QuestionnaireKimchi Questionnaire
Kimchi Questionnaire
OUM SAOKOSAL
 
Rayleigh Fading Channel In Mobile Digital Communication System
Rayleigh Fading Channel In Mobile Digital Communication SystemRayleigh Fading Channel In Mobile Digital Communication System
Rayleigh Fading Channel In Mobile Digital Communication System
OUM SAOKOSAL
 
Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - Swing
OUM SAOKOSAL
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with ExamplesJavascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
Javascript & DOM - Part 1- Javascript Tutorial for Beginners with Examples
OUM SAOKOSAL
 
Ad

Similar to Chapter 9 Interface (20)

Lecture 18
Lecture 18Lecture 18
Lecture 18
talha ijaz
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
Richa Gupta
 
Java 6.pptx
Java 6.pptxJava 6.pptx
Java 6.pptx
usmanusman720379
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)
Professor Lili Saghafi
 
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdfFINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
VGaneshKarthikeyan
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
rani marri
 
Java interface
Java interface Java interface
Java interface
HoneyChintal
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Sujit Kumar
 
Oop
OopOop
Oop
MD.ANISUR RAHMAN
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
rajshreemuthiah
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
Opps
OppsOpps
Opps
Lalit Kale
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
dhrubo kayal
 
Basic_Java_10.pdf
Basic_Java_10.pdfBasic_Java_10.pdf
Basic_Java_10.pdf
KumarUtsav24
 
9 abstract interface
9 abstract interface9 abstract interface
9 abstract interface
Abhijit Gaikwad
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programming
samthemonad
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
icarter09
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming
deffa5
 
Evolve Your Code
Evolve Your CodeEvolve Your Code
Evolve Your Code
RookieOne
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
Richa Gupta
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)
Professor Lili Saghafi
 
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdfFINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
FINAL_DAY11_INTERFACES_Roles_and_Responsibility.pdf
VGaneshKarthikeyan
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
rani marri
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
rajshreemuthiah
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programming
samthemonad
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
icarter09
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming
deffa5
 
Evolve Your Code
Evolve Your CodeEvolve Your Code
Evolve Your Code
RookieOne
 
Ad

More from OUM SAOKOSAL (18)

Android app development - Java Programming for Android
Android app development - Java Programming for AndroidAndroid app development - Java Programming for Android
Android app development - Java Programming for Android
OUM SAOKOSAL
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Java OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to JavaJava OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to Java
OUM SAOKOSAL
 
Aggregate rank bringing order to web sites
Aggregate rank  bringing order to web sitesAggregate rank  bringing order to web sites
Aggregate rank bringing order to web sites
OUM SAOKOSAL
 
How to succeed in graduate school
How to succeed in graduate schoolHow to succeed in graduate school
How to succeed in graduate school
OUM SAOKOSAL
 
Google
GoogleGoogle
Google
OUM SAOKOSAL
 
E miner
E minerE miner
E miner
OUM SAOKOSAL
 
Data preparation for mining world wide web browsing patterns (1999)
Data preparation for mining world wide web browsing patterns (1999)Data preparation for mining world wide web browsing patterns (1999)
Data preparation for mining world wide web browsing patterns (1999)
OUM SAOKOSAL
 
Consumer acceptance of online banking an extension of the technology accepta...
Consumer acceptance of online banking  an extension of the technology accepta...Consumer acceptance of online banking  an extension of the technology accepta...
Consumer acceptance of online banking an extension of the technology accepta...
OUM SAOKOSAL
 
When Do People Help
When Do People HelpWhen Do People Help
When Do People Help
OUM SAOKOSAL
 
Mc Nemar
Mc NemarMc Nemar
Mc Nemar
OUM SAOKOSAL
 
Correlation Example
Correlation ExampleCorrelation Example
Correlation Example
OUM SAOKOSAL
 
Sem Ski Amos
Sem Ski AmosSem Ski Amos
Sem Ski Amos
OUM SAOKOSAL
 
Sem+Essentials
Sem+EssentialsSem+Essentials
Sem+Essentials
OUM SAOKOSAL
 
Path Spss Amos (1)
Path Spss Amos (1)Path Spss Amos (1)
Path Spss Amos (1)
OUM SAOKOSAL
 
How To Succeed In Graduate School
How To Succeed In Graduate SchoolHow To Succeed In Graduate School
How To Succeed In Graduate School
OUM SAOKOSAL
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core Concept
OUM SAOKOSAL
 
Android app development - Java Programming for Android
Android app development - Java Programming for AndroidAndroid app development - Java Programming for Android
Android app development - Java Programming for Android
OUM SAOKOSAL
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Java OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and ObjectJava OOP Programming language (Part 3) - Class and Object
Java OOP Programming language (Part 3) - Class and Object
OUM SAOKOSAL
 
Java OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to JavaJava OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to Java
OUM SAOKOSAL
 
Aggregate rank bringing order to web sites
Aggregate rank  bringing order to web sitesAggregate rank  bringing order to web sites
Aggregate rank bringing order to web sites
OUM SAOKOSAL
 
How to succeed in graduate school
How to succeed in graduate schoolHow to succeed in graduate school
How to succeed in graduate school
OUM SAOKOSAL
 
Data preparation for mining world wide web browsing patterns (1999)
Data preparation for mining world wide web browsing patterns (1999)Data preparation for mining world wide web browsing patterns (1999)
Data preparation for mining world wide web browsing patterns (1999)
OUM SAOKOSAL
 
Consumer acceptance of online banking an extension of the technology accepta...
Consumer acceptance of online banking  an extension of the technology accepta...Consumer acceptance of online banking  an extension of the technology accepta...
Consumer acceptance of online banking an extension of the technology accepta...
OUM SAOKOSAL
 
When Do People Help
When Do People HelpWhen Do People Help
When Do People Help
OUM SAOKOSAL
 
Correlation Example
Correlation ExampleCorrelation Example
Correlation Example
OUM SAOKOSAL
 
Path Spss Amos (1)
Path Spss Amos (1)Path Spss Amos (1)
Path Spss Amos (1)
OUM SAOKOSAL
 
How To Succeed In Graduate School
How To Succeed In Graduate SchoolHow To Succeed In Graduate School
How To Succeed In Graduate School
OUM SAOKOSAL
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core Concept
OUM SAOKOSAL
 

Recently uploaded (20)

The Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI IntegrationThe Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI Integration
Re-solution Data Ltd
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
The Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI IntegrationThe Future of Cisco Cloud Security: Innovations and AI Integration
The Future of Cisco Cloud Security: Innovations and AI Integration
Re-solution Data Ltd
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...Transcript: Canadian book publishing: Insights from the latest salary survey ...
Transcript: Canadian book publishing: Insights from the latest salary survey ...
BookNet Canada
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
Web and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in RajpuraWeb and Graphics Designing Training in Rajpura
Web and Graphics Designing Training in Rajpura
Erginous Technology
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and MLGyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
GyrusAI - Broadcasting & Streaming Applications Driven by AI and ML
Gyrus AI
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 

Chapter 9 Interface

  • 1. Chapter 9 Abstract Class & Interface Oum Saokosal , Head of IT Department National Polytechnic Institute of Cambodia Tel: (855)-12-417214 E-mail: oum_saokosal@yahoo.com
  • 3. 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
  • 4. What is Interface? In many ways, Interface is very similar to abstract class but it marks with interface keyword instead of abstract class keyword. public interface InterfaceName { } Unlike abstract class which can also contain nonabstract methods, interface contain ONLY abstract methods and constants .
  • 5. 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(); }
  • 6. 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”; } }
  • 7. 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”; } }
  • 8. Why not use abstract class instead of interface? (1) Hey Rabbit! Today’s class is about interface. The word “interface” here means GUI, Graphical User Interface? Not at all, Ball. GUI is GUI. Interface here is a kind of ideas in OOP which is similar to abstract class. I’ll tell ya what...
  • 9. 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.
  • 10. Why not use abstract class instead of interface? (3) CB: Why guarantee? SR: Because of polymorphism. Do you remember the form of polymorphism. CR: Like this: Vehicle v = new Jeep(); v.getGun(); SR: And what wrong if Jeep didn’t override getGun() method? CR: Oh I see. Then it will call getGun() from the parent Vehicle . Because Jeep didn’t..., I see!
  • 11. Why not use abstract class instead of interface? (4) SR: So how to guarantee or to force ITS CLOSED SUBCLASSES to override every methods in superclass? CB: Hmmm, we make the superclass to be abstract class and so do the methods. SR: That’s right. The interface acts the SAME things. CR: I see. I think if the abstract class and interface are the same, why we need interface? And WHY DON’T WE USE ABSTRACT CLASS INSTEAD OF INTERFACE?
  • 12. Why not use abstract class instead of interface? (5) SR: - It’s such a good question? - You know, in some other languages like C++, a class can inherit multiple superclasses which is known multiple inheritance . Java, however, does not allow multiple inheritance. That is, a class can only have a single inheritance . CB: So what’s wrong with the single inheritance? SR: Well, A class can inherit only one superclass. Like: public class Jeep extends Vehicle { }
  • 13. Why not use abstract class instead of interface? (6) CB: Yes. It is right. Vehicle must be a parent of Jeep . SR: What about if we want to use JApplet in Vehicle as well ? CB: Then we can do like this: public class Jeep extends JApplet, Vehicle { } SR: That’s what I’m talking about! You cannot have multiple inheritance in Java. If you still want to do like this, you will get compile error. And You know, you can’t do this in other languages like ActionScript or C# as well. You cannot!!!
  • 14. Why not use abstract class instead of interface? (7) 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.
  • 15. Why not use abstract class instead of interface? (8) CB:OK. Can you change Vehicle to interface for me? SR: Not again. You should know to do it !!! CB: I don’t know. That’s why I ask you! SR: OK. Here is the code of abstract class : public abstract class Vehicle { public abstract String getTradeMark(); public abstract double getSpeed(); public abstract boolean hasWeapon(); }
  • 16. Why not use abstract class instead of interface? (9) SR: And here is the change to interface: public interface Vehicle { public String getTradeMark(); public double getSpeed(); public boolean hasWeapon(); } CB: I think you didn’t mark methods with abstract keyword! SR: That’s OK. Mark it or not, it’s the same. It’s always abstract automatically.
  • 17. Why not use abstract class instead of interface? (10) CB: Any change with the Jeep class? SR: Of course, but a bit. Just from extends to implements . public class Jeep implements Vehicle { public String getTradeMark(){ return “Jeep”; } public double getSpeed(){ return 250; } public boolean hasWeapon(){ return true; } }
  • 18. Why not use abstract class instead of interface? (11) CB: I see. It’s great! SR: Even greater than this, Java allow to have MULTIPLE INTERFACE. That is, you can inherit only one class, but you can have many interfaces. E.g.: public class Fish extends ChessCharacter implements Runnable, ActionListener, KeyListener { /*Overridden methods are here*/ } CB: Wow, wonderful. Now I know why we use interface instead of abstract class .
  • 19. Why not use abstract class instead of interface? (12) SR: However, You should know that in interface , you CANNOT include nonabstract methods at all; whereas in abstract class , you can MIX nonabstract and abstract methods together . CB: You mean, if we make an abstract class , then its subclasses could reuse some nonabstract methods without override. SR: That’s right... and... CB: On the other hands, if we make an interface , then classes that implement the interface MUST override every method . SR: Yes.. You’re right.
  • 20. UML of Interface (1) In some old books, you could see UML notation to descript an interface like this: Interface abstract class Runnable + run():void Character + getCharacter():String + getRole(): String
  • 21. UML of Interface (2) But in UML 2.0, to make a difference from the abstract class and the interface notation, they do like this: Interface abstract class << interface >> Runnable + run():void Character + getCharacter():String + getRole(): String
  • 22. UML of Interface (3) And a line to indicate that a class implements an interface is dotted line with white arrow head . <<interface>> Runnable + run():void Fish <<interface>> ActionListener + actionPerformed (e:ActionEvent ):void
  • 23. 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(); }
  • 24. Differences between interface and abstract class (1) Like abstract class, if a class implement an interface, you have to override the interface’s methods in the class. You cannot create instances from an interface by using new operator. Interface can be a type as well. Runnable r; the purpose of creating interface is because of polymorphism .
  • 25. Differences between interface and abstract class (2) Unlike abstract class, You can have multiple interface in one class. To implement those interfaces, use implements keyword and separate interfaces by comma . public class Test implements Runnable , ActionListener , MouseMotionListener { /* Overridden interfaces methods */ } 2. Interface uses interface keyword. Interface is NOT designed to be superclass . Interface is designed to add some behaviors to a class.
  • 26. Different between interface and abstract class (3) In the relationships, we say that: 4.1. A relationship between class/abstract class and class is a strong relationship. It is known as IS-A relationship. E.g: A duck is a bird . It clearly means the duck is really a bird. So the bird can be a superclass of a duck. It could be either concrete or abstract class . 4.2. A relationship between class and interface is a weak relationship. It is known as Is-kind-of relationship. E.g: A duck is flyable . Flyable can never ever be the superclass of the duck. It just means this duck can fly. So flyable is interface .
  • 27. Notes for Interface (1) Because the interface is just designed to add some behaviors or some features to classes, usually it contains only one or two general methods . E.g.: public interface Runnable { void run(); } The reason for this is that interface is not a superclass. So it doesn’t specify who can use its methods. Generally, its method might be used by everyone .
  • 28. Notes for Interface (2) By Java code conversion, the name of interface is usually adjective . Because adjective adds some meaning to a noun. That’s why you’ll see: - Runnable - Comparable - Cloneable - Accessible The interface names for event driven listener are usually ended with Listener . E.g. - ActionListener - MouseMotionListener - KeyListener
  • 29. Class Design Guildlines p. 358-360 A class should descript a single entity or a single thing. Classes are usually designed for reuse by many different customers and developers. If we want polymorphism , you have to use inheritance design (of course, overridden method ). If NO need polymorphism, use composite design. If a relationship is strong like apple and fruit, please use abstract class design. If a relationship is weak like apple and eatable, please use interface design.
  • 30. Last Words (1) CB: Hi Rabbit. After I know Class Encapsulation, Inheritance, Polymorphism, Abstract Class and Interface, I think I know OOP. What should I do after all? SR: Well. First, you can learn Java of course. I mean you can understand Java faster than ever. CB: Oh really? SR: Yeah... Because the difficult parts of Java is to understand OOP. I means you have to know OOP in order to learn Java. CB: What about people claim that they can write Java programs but they say they don’t need to know OOP. SR: It sounds cheating, you know! Of course, it is possible if they really work day-in day-out with Java. But they don’t know what is going on inside. Also, it takes so much time to be able to write Java without understand OOP.
  • 31. Last Words (2) CB: I quess so but anyway. Back to my questions, which Java topics should I learn further? SR: You know, Java is huge. The reason for this is because Java wants every thing work in Java. CB: What can work in Java? SR: You use, for example, JavaSE for Desktop Application and Applet. JavaEE is very big and it is for big enterprise. You can make JSP, Servlet, Web Applications, Web Services, JavaBean, Java Server Face and many more. CB: Wow, I’ve never heard all of these! SR: You can also use JavaME for mobile devices, PDA, etc. There are always a lot more to learn! Is it scary?
  • 32. Last Words (3) CB: Oh my... It is more enormous than I’ve ever imagined. SR: Actually, if you really want to learn them all, you will take your whole life to learn. But ... but... you know, after OOP, the rest of it is only how to understand and to use Java API library. CB: What do you mean? SR: I mean there’s no more strange things like what is inheritance, polymorphism, abstract class or interface. Because you know all of it. CB: Yes. but API... SR: After you knew OOP, you can use Java API. For example, to make a button clickable, you have to use an interface called ActionListener. And them use polymorphism to make it work.
  • 33. Last Words (4) CB: Heu heu... SR: And to use JFrame, JApplet, and MIDlet, you have to use inheritance. CB: Oh yeah... SR: And something like this. The key point is to realize which way to use this API. I mean it is to use inheritance, polymorphism or interface? CB: OK. I got it. SR: One more suggestion. After you know OOP, you should learn a specific topic in Java rather learn them all.
  • 34. Last Words (4) CB: How can I choose? SR: For example, if you like to work with Web Application, please go to Serlvet, JSP and Database Programming. CB: What about if I like to make online game. SR: This one? You could go to Swing, Applet, Multiple Threading and distributed computing. But it is hard one. CB: Any thing else? SR: Of course. You can also learn JavaME for Mobile Phone. It is a hot topic for today. Everybody always ask me. CB: Really. Maybe I could make a decision now. I choose JavaME. SR. Yehh I choose the good one. So let go get ‘em!
  翻译: