Your SlideShare is downloading. ×
Implementation of interface9 cm604.30
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Implementation of interface9 cm604.30

84

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
84
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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 Programpublic 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 Programpublic 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 Questions1. Write the syntax for defining an interface2. How to define an interface?3. How to implement an interface ? 16
  • 17. Quiz1.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

×