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 (20)

PPT
C++ oop
Sunil OS
 
PPT
C++ classes tutorials
FALLEE31188
 
PPTX
20.5 Java polymorphism
Intro C# Book
 
PPTX
2CPP14 - Abstraction
Michael Heron
 
PPT
Basic c#
kishore4268
 
PPTX
20.4 Java interfaces and abstraction
Intro C# Book
 
PDF
C# Summer course - Lecture 4
mohamedsamyali
 
PDF
C# Summer course - Lecture 3
mohamedsamyali
 
PPTX
20.3 Java encapsulation
Intro C# Book
 
PDF
Object Oriented Programming using C++ Part III
Ajit Nayak
 
PPTX
20.2 Java inheritance
Intro C# Book
 
PPTX
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Abu Saleh
 
PPT
C++ Inheritance
Jussi Pohjolainen
 
PPTX
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
PPT
Object-Oriented Programming Using C++
Salahaddin University-Erbil
 
PDF
Java ppt Gandhi Ravi ([email protected])
Gandhi Ravi
 
PPTX
classes and objects in C++
HalaiHansaika
 
PDF
Inheritance
Pranali Chaudhari
 
PDF
C sharp chap5
Mukesh Tekwani
 
PPT
Oops concept in c#
ANURAG SINGH
 
C++ oop
Sunil OS
 
C++ classes tutorials
FALLEE31188
 
20.5 Java polymorphism
Intro C# Book
 
2CPP14 - Abstraction
Michael Heron
 
Basic c#
kishore4268
 
20.4 Java interfaces and abstraction
Intro C# Book
 
C# Summer course - Lecture 4
mohamedsamyali
 
C# Summer course - Lecture 3
mohamedsamyali
 
20.3 Java encapsulation
Intro C# Book
 
Object Oriented Programming using C++ Part III
Ajit Nayak
 
20.2 Java inheritance
Intro C# Book
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Abu Saleh
 
C++ Inheritance
Jussi Pohjolainen
 
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
Object-Oriented Programming Using C++
Salahaddin University-Erbil
 
Java ppt Gandhi Ravi ([email protected])
Gandhi Ravi
 
classes and objects in C++
HalaiHansaika
 
Inheritance
Pranali Chaudhari
 
C sharp chap5
Mukesh Tekwani
 
Oops concept in c#
ANURAG SINGH
 

Viewers also liked (12)

PPT
Exception handling.41
myrajendra
 
PPT
Dead locks9cm604.39
myrajendra
 
PPT
Dealing exception.43
myrajendra
 
PPT
9 cm604.42
myrajendra
 
PPT
Inter threadcommunication.38
myrajendra
 
PPT
Starting jdbc
myrajendra
 
PPTX
Interface callable statement
myrajendra
 
PPTX
Properties
myrajendra
 
PPT
Runnable interface.34
myrajendra
 
PPTX
Java.sql package
myrajendra
 
PPT
Threadlifecycle.36
myrajendra
 
PPTX
Hibernate example1
myrajendra
 
Exception handling.41
myrajendra
 
Dead locks9cm604.39
myrajendra
 
Dealing exception.43
myrajendra
 
9 cm604.42
myrajendra
 
Inter threadcommunication.38
myrajendra
 
Starting jdbc
myrajendra
 
Interface callable statement
myrajendra
 
Properties
myrajendra
 
Runnable interface.34
myrajendra
 
Java.sql package
myrajendra
 
Threadlifecycle.36
myrajendra
 
Hibernate example1
myrajendra
 
Ad

Similar to Implementation of interface9 cm604.30 (20)

PPT
Multiple interfaces 9 cm604.31
myrajendra
 
PPTX
Java class 4
Edureka!
 
PDF
Object-oriented Basics
Jamie (Taka) Wang
 
PPS
Interface
kamal kotecha
 
PPTX
INTERFACES. with machine learning and data
dineshkesav07
 
PDF
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
PPTX
it is the quick gest about the interfaces in java
arunkumarg271
 
PPTX
Interface
vvpadhu
 
PPT
Interfaces
Jai Marathe
 
PDF
Java Class Design
Ganesh Samarthyam
 
DOC
Test Engine
guestcdaa2dc
 
PPT
Java interface
Arati Gadgil
 
PDF
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
PPT
pbo 5 ervan
aris
 
PDF
C# for Java Developers
Jussi Pohjolainen
 
PPTX
Interfaces c#
Nipam Medhi
 
PPT
oops with java modules i & ii.ppt
rani marri
 
PPTX
L04 Software Design 2
Ólafur Andri Ragnarsson
 
Multiple interfaces 9 cm604.31
myrajendra
 
Java class 4
Edureka!
 
Object-oriented Basics
Jamie (Taka) Wang
 
Interface
kamal kotecha
 
INTERFACES. with machine learning and data
dineshkesav07
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
it is the quick gest about the interfaces in java
arunkumarg271
 
Interface
vvpadhu
 
Interfaces
Jai Marathe
 
Java Class Design
Ganesh Samarthyam
 
Test Engine
guestcdaa2dc
 
Java interface
Arati Gadgil
 
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
pbo 5 ervan
aris
 
C# for Java Developers
Jussi Pohjolainen
 
Interfaces c#
Nipam Medhi
 
oops with java modules i & ii.ppt
rani marri
 
L04 Software Design 2
Ólafur Andri Ragnarsson
 
Ad

More from myrajendra (20)

PPT
Fundamentals
myrajendra
 
PPT
Data type
myrajendra
 
PPTX
Jdbc workflow
myrajendra
 
PPTX
2 jdbc drivers
myrajendra
 
PPTX
3 jdbc api
myrajendra
 
PPTX
4 jdbc step1
myrajendra
 
PPTX
Dao example
myrajendra
 
PPTX
Sessionex1
myrajendra
 
PPTX
Internal
myrajendra
 
PPTX
3. elements
myrajendra
 
PPTX
2. attributes
myrajendra
 
PPTX
1 introduction to html
myrajendra
 
PPTX
Headings
myrajendra
 
PPTX
Forms
myrajendra
 
PPT
Css
myrajendra
 
PPTX
Views
myrajendra
 
PPTX
Views
myrajendra
 
PPTX
Views
myrajendra
 
PPTX
Interface result set
myrajendra
 
PPTX
Interface database metadata
myrajendra
 
Fundamentals
myrajendra
 
Data type
myrajendra
 
Jdbc workflow
myrajendra
 
2 jdbc drivers
myrajendra
 
3 jdbc api
myrajendra
 
4 jdbc step1
myrajendra
 
Dao example
myrajendra
 
Sessionex1
myrajendra
 
Internal
myrajendra
 
3. elements
myrajendra
 
2. attributes
myrajendra
 
1 introduction to html
myrajendra
 
Headings
myrajendra
 
Forms
myrajendra
 
Views
myrajendra
 
Views
myrajendra
 
Views
myrajendra
 
Interface result set
myrajendra
 
Interface database metadata
myrajendra
 

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