SlideShare a Scribd company logo
Recap
• An interface is a collection of method
  declarations
• Constants can also be defined as part of
  interface
• Interface feature in Java supports
   • Implementation should be separated
     from interface
• Main benefit of interface is
   • Implement multiple inheritance in Java


                                              1
Defining Interfaces



• To define an interface,
    public interface [InterfaceName] {
        //some methods without the body
    }
                            User defined
                            interface name

               Interface Keyword



                                             2
Defining Interfaces        contd..
Let us create an interface Shape
      // Definition of interface Shape
      public interface Shape {
         public double area();
         public double volume();
         public String getName();
      }



                                               3
Defining Interfaces      contd..
• Another example
• Interface to define relationships between two
  objects according to the “natural order” of the
  objects
      public interface Relation {
        public boolean isGreater( Object a, Object b);
        public boolean isLess( Object a, Object b);
        public boolean isEqual( Object a, Object b);
      }

                                                     4
Implementing Interface


• To implement an interface means
   • To create a concrete class that implements
     an interface
• Use the implements keyword
                       A class which can be instantiated




                                                           5
Implementing Interface            contd..
Syntax is :
class concreteClass implements InterfaceName {
   •   // Write code for all methods of interface
   •   // Write any code for any methods related to class
    }
• Remember
   Concrete class must implement ALL methods
    of the interface



                                                            6
Implementing Interface      Contd..


• Implementing class can have its own methods
• Implementing class can extend a single super
  class or abstract class




                                                 7
Example Program
public class Point implements Shape {
  protected int x, y; // coordinates
  // no-argument constructor
  public Point() { setPoint( 0, 0 ); }
                                           Usage of implements
  // constructor                           keyword
  public Point( int a, int b ) {
      setPoint( a, b ); }                  Point class provides code
  public void setPoint( int a, int b ) {   for all methods of Shape
                                           interface
       x = a;                                        area()
       y = b;                                        volume()
  }                                                  getName()
  public int getX() { return x; }
                                           Point class has some
  public int getY() { return y; }          additional methods also
  public String toString() {                        getX()
      return "[" + x + ", " + y + "]"; }            getY()
                                                    toString()
                                                                       8
Example Program   Contd..
    public double area() {
        return 0.0;
     }
    public double volume(){
        return 0.0;
    }
    public String getName() {
        return "Point";
    }
}




                                           9
Example Program Contd..
import javax.swing.JOptionPane;
public class TestPoint {
  public static void main(String args[]) {
    Point point1 = new Point( 7, 11 );
    Point point2 = new Point( 12, 24 );
    Point point3 = new Point( 4, 10 );
    Shape arrayOfShapes[];
    arrayOfShapes = new Shape[ 3 ];
    arrayOfShapes[ 0 ] = point1;
    arrayOfShapes[ 1 ] = point2;
    arrayOfShapes[ 2 ] = point3;
    String output = "";
    // Loop through arrayOfShapes and
    //print the name of object.

                                             10
Example Program Contd..
    for ( int i = 0; i <
            arrayOfShapes.length; i++ ) {
       output += "nn" +
        arrayOfShapes[ i ].getName() + “: “
            + arrayOfShapes[ i ].toString() ;
      }
      JOptionPane.showMessageDialog
        (null, output,"Implementing Interface",
JOptionPane.INFORMATION_MESSAGE                   Output

);
      System.exit( 0 );
   }
}

                                                           11
Another Example Program
public class Line implements Relation {
                                            Concrete class is Line
  private double x1;                        Interface is Relation
  private double x2;
  private double y1;
  private double y2;
  public Line(double x1, double x2, double y1, double y2){
       this.x1 = x1;
       this.x2 = x2;
       this.y1 = y1;
       this.y2 = y2;                         The methods of class Line is
  }                                          getLength()
  public double getLength(){
       double length = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)* (y2-y1));
       return length;
  }


                                                                            12
Another Example Program
 public boolean isGreater( Object a, Object b){
   double aLen = ((Line)a).getLength();
   double bLen = ((Line)b).getLength();
   return (aLen > bLen);                      The methods of interface
                                              Relation are
 }                                                     isGreater()
                                                       isLess()
 public boolean isLess( Object a, Object b){           isEqual()
   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);
 }
}


                                                                         13
Another Example Program Contd..
import javax.swing.JOptionPane;
class TestLine{
   public static void main(String[] args){
         Line line1 = new Line(7.0,3.0,15.0, 23.0);
         Line line2 = new Line(2.0,4.0,12.0, 53.0);
         String output = "";
         output += "Line1 is Greater than Line2 is " +line1.isGreater(line1,line2)
                       +"n";
         output += "Line1 is Less than Line2 is " +line1.isLess(line1,line2) +"n";
         output += "Line1 is Equal to Line2 is " +line1.isEqual(line1,line2) +"n";
         JOptionPane.showMessageDialog( null, output, "Implementing
                 Interface", JOptionPane.INFORMATION_MESSAGE ); Output
          System.exit( 0 );
   }
                               Line1 is less than Line2
}


                                                                                      14
Summary
• In this class we have discussed
   • How to define an interface
   • How to implement an interface
   • Simple programs using interfaces

• In the next lesson we look at
   • How to perform multiple inheritance
   • Scope of variable and some more concepts of
     interfaces

                                                   15
Frequently Asked Questions
1. Write the syntax for defining an interface
2. How to define an interface?
3. How to implement an interface ?




                                                16
Quiz
1.Which keyword is useful for implementing an
  interface ?
  1.   implement
  2.   implements
  3.   interface
  4.   extends




                                                17
Quiz Contd..

2. Which of the following statements is false ?
  1. Implementing class cannot have its own
     methods
  2. Implementing class can have its own
     methods
  3. Implementing class can extend from another
     class
  4. All
Assignments
• Write Java program to define an interface called
  Operator that does add, subtract and multiply
  operations
• Write Java program to Implement Operator
  interface for arithmetic operations
• Write Java program to Implement Operator
  interface for matrix operations




                                                     19

More Related Content

What's hot

C++ oop
C++ oopC++ oop
C++ oop
Sunil OS
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
FALLEE31188
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
Intro C# Book
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
Michael Heron
 
Basic c#
Basic c#Basic c#
Basic c#
kishore4268
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
Intro C# Book
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
mohamedsamyali
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
mohamedsamyali
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
Intro C# Book
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
Ajit Nayak
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
Intro C# Book
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Abu Saleh
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
Jussi Pohjolainen
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
Salahaddin University-Erbil
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Gandhi Ravi
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
C sharp chap5
C sharp chap5C sharp chap5
C sharp chap5
Mukesh Tekwani
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
ANURAG SINGH
 

What's hot (20)

C++ oop
C++ oopC++ oop
C++ oop
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Basic c#
Basic c#Basic c#
Basic c#
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
C sharp chap5
C sharp chap5C sharp chap5
C sharp chap5
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 

Viewers also liked

Exception handling.41
Exception handling.41Exception handling.41
Exception handling.41
myrajendra
 
Dead locks9cm604.39
Dead locks9cm604.39Dead locks9cm604.39
Dead locks9cm604.39
myrajendra
 
Dealing exception.43
Dealing exception.43Dealing exception.43
Dealing exception.43
myrajendra
 
9 cm604.42
9 cm604.429 cm604.42
9 cm604.42
myrajendra
 
Inter threadcommunication.38
Inter threadcommunication.38Inter threadcommunication.38
Inter threadcommunication.38
myrajendra
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
myrajendra
 
Interface callable statement
Interface callable statementInterface callable statement
Interface callable statement
myrajendra
 
Properties
PropertiesProperties
Properties
myrajendra
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
myrajendra
 
Java.sql package
Java.sql packageJava.sql package
Java.sql package
myrajendra
 
Threadlifecycle.36
Threadlifecycle.36Threadlifecycle.36
Threadlifecycle.36
myrajendra
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
myrajendra
 

Viewers also liked (12)

Exception handling.41
Exception handling.41Exception handling.41
Exception handling.41
 
Dead locks9cm604.39
Dead locks9cm604.39Dead locks9cm604.39
Dead locks9cm604.39
 
Dealing exception.43
Dealing exception.43Dealing exception.43
Dealing exception.43
 
9 cm604.42
9 cm604.429 cm604.42
9 cm604.42
 
Inter threadcommunication.38
Inter threadcommunication.38Inter threadcommunication.38
Inter threadcommunication.38
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 
Interface callable statement
Interface callable statementInterface callable statement
Interface callable statement
 
Properties
PropertiesProperties
Properties
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Java.sql package
Java.sql packageJava.sql package
Java.sql package
 
Threadlifecycle.36
Threadlifecycle.36Threadlifecycle.36
Threadlifecycle.36
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 

Similar to Implementation of interface9 cm604.30

Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31
myrajendra
 
Java class 4
Java class 4Java class 4
Java class 4
Edureka!
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
Jamie (Taka) Wang
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
Introduction to-java
Introduction to-javaIntroduction to-java
Interface
InterfaceInterface
Interface
vvpadhu
 
Interfaces
InterfacesInterfaces
Interfaces
Jai Marathe
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
Ganesh Samarthyam
 
Test Engine
Test EngineTest Engine
Test Engine
guestcdaa2dc
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
pbo 5 ervan
pbo 5 ervanpbo 5 ervan
pbo 5 ervan
aris
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
Jussi Pohjolainen
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
Nipam Medhi
 
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
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
Ólafur Andri Ragnarsson
 
Java
JavaJava
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 

Similar to Implementation of interface9 cm604.30 (20)

Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31
 
Java class 4
Java class 4Java class 4
Java class 4
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Interface
InterfaceInterface
Interface
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
Introduction to-java
Introduction to-javaIntroduction to-java
Introduction to-java
 
Interface
InterfaceInterface
Interface
 
Interfaces
InterfacesInterfaces
Interfaces
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Test Engine
Test EngineTest Engine
Test Engine
 
Java interface
Java interfaceJava interface
Java interface
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
pbo 5 ervan
pbo 5 ervanpbo 5 ervan
pbo 5 ervan
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
 
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
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Java
JavaJava
Java
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 

More from myrajendra

Fundamentals
FundamentalsFundamentals
Fundamentals
myrajendra
 
Data type
Data typeData type
Data type
myrajendra
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
myrajendra
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
myrajendra
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
myrajendra
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
myrajendra
 
Dao example
Dao exampleDao example
Dao example
myrajendra
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
myrajendra
 
Internal
InternalInternal
Internal
myrajendra
 
3. elements
3. elements3. elements
3. elements
myrajendra
 
2. attributes
2. attributes2. attributes
2. attributes
myrajendra
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
myrajendra
 
Headings
HeadingsHeadings
Headings
myrajendra
 
Forms
FormsForms
Forms
myrajendra
 
Css
CssCss
Views
ViewsViews
Views
myrajendra
 
Views
ViewsViews
Views
myrajendra
 
Views
ViewsViews
Views
myrajendra
 
Interface result set
Interface result setInterface result set
Interface result set
myrajendra
 
Interface database metadata
Interface database metadataInterface database metadata
Interface database metadata
myrajendra
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Interface result set
Interface result setInterface result set
Interface result set
 
Interface database metadata
Interface database metadataInterface database metadata
Interface database metadata
 

Implementation of interface9 cm604.30

  • 1. Recap • An interface is a collection of method declarations • Constants can also be defined as part of interface • Interface feature in Java supports • Implementation should be separated from interface • Main benefit of interface is • Implement multiple inheritance in Java 1
  • 2. Defining Interfaces • To define an interface, public interface [InterfaceName] { //some methods without the body } User defined interface name Interface Keyword 2
  • 3. Defining Interfaces contd.. Let us create an interface Shape // Definition of interface Shape public interface Shape { public double area(); public double volume(); public String getName(); } 3
  • 4. Defining Interfaces contd.. • Another example • Interface to define relationships between two objects according to the “natural order” of the objects public interface Relation { public boolean isGreater( Object a, Object b); public boolean isLess( Object a, Object b); public boolean isEqual( Object a, Object b); } 4
  • 5. Implementing Interface • To implement an interface means • To create a concrete class that implements an interface • Use the implements keyword A class which can be instantiated 5
  • 6. Implementing Interface contd.. Syntax is : class concreteClass implements InterfaceName { • // Write code for all methods of interface • // Write any code for any methods related to class } • Remember Concrete class must implement ALL methods of the interface 6
  • 7. Implementing Interface Contd.. • Implementing class can have its own methods • Implementing class can extend a single super class or abstract class 7
  • 8. Example Program public class Point implements Shape { protected int x, y; // coordinates // no-argument constructor public Point() { setPoint( 0, 0 ); } Usage of implements // constructor keyword public Point( int a, int b ) { setPoint( a, b ); } Point class provides code public void setPoint( int a, int b ) { for all methods of Shape interface x = a; area() y = b; volume() } getName() public int getX() { return x; } Point class has some public int getY() { return y; } additional methods also public String toString() { getX() return "[" + x + ", " + y + "]"; } getY() toString() 8
  • 9. Example Program Contd.. public double area() { return 0.0; } public double volume(){ return 0.0; } public String getName() { return "Point"; } } 9
  • 10. Example Program Contd.. import javax.swing.JOptionPane; public class TestPoint { public static void main(String args[]) { Point point1 = new Point( 7, 11 ); Point point2 = new Point( 12, 24 ); Point point3 = new Point( 4, 10 ); Shape arrayOfShapes[]; arrayOfShapes = new Shape[ 3 ]; arrayOfShapes[ 0 ] = point1; arrayOfShapes[ 1 ] = point2; arrayOfShapes[ 2 ] = point3; String output = ""; // Loop through arrayOfShapes and //print the name of object. 10
  • 11. Example Program Contd.. for ( int i = 0; i < arrayOfShapes.length; i++ ) { output += "nn" + arrayOfShapes[ i ].getName() + “: “ + arrayOfShapes[ i ].toString() ; } JOptionPane.showMessageDialog (null, output,"Implementing Interface", JOptionPane.INFORMATION_MESSAGE Output ); System.exit( 0 ); } } 11
  • 12. Another Example Program public class Line implements Relation { Concrete class is Line private double x1; Interface is Relation private double x2; private double y1; private double y2; public Line(double x1, double x2, double y1, double y2){ this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; The methods of class Line is } getLength() public double getLength(){ double length = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)* (y2-y1)); return length; } 12
  • 13. Another Example Program public boolean isGreater( Object a, Object b){ double aLen = ((Line)a).getLength(); double bLen = ((Line)b).getLength(); return (aLen > bLen); The methods of interface Relation are } isGreater() isLess() public boolean isLess( Object a, Object b){ isEqual() 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); } } 13
  • 14. Another Example Program Contd.. import javax.swing.JOptionPane; class TestLine{ public static void main(String[] args){ Line line1 = new Line(7.0,3.0,15.0, 23.0); Line line2 = new Line(2.0,4.0,12.0, 53.0); String output = ""; output += "Line1 is Greater than Line2 is " +line1.isGreater(line1,line2) +"n"; output += "Line1 is Less than Line2 is " +line1.isLess(line1,line2) +"n"; output += "Line1 is Equal to Line2 is " +line1.isEqual(line1,line2) +"n"; JOptionPane.showMessageDialog( null, output, "Implementing Interface", JOptionPane.INFORMATION_MESSAGE ); Output System.exit( 0 ); } Line1 is less than Line2 } 14
  • 15. Summary • In this class we have discussed • How to define an interface • How to implement an interface • Simple programs using interfaces • In the next lesson we look at • How to perform multiple inheritance • Scope of variable and some more concepts of interfaces 15
  • 16. Frequently Asked Questions 1. Write the syntax for defining an interface 2. How to define an interface? 3. How to implement an interface ? 16
  • 17. Quiz 1.Which keyword is useful for implementing an interface ? 1. implement 2. implements 3. interface 4. extends 17
  • 18. Quiz Contd.. 2. Which of the following statements is false ? 1. Implementing class cannot have its own methods 2. Implementing class can have its own methods 3. Implementing class can extend from another class 4. All
  • 19. Assignments • Write Java program to define an interface called Operator that does add, subtract and multiply operations • Write Java program to Implement Operator interface for arithmetic operations • Write Java program to Implement Operator interface for matrix operations 19