Published on

interface of java ppt define and implement interface, extending interface

Published in: Education, Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • Interface

    1. 1. Chapter 8Interface
    2. 2. Interface
    3. 3. Interfaces are similar to abstract classes, butdiffer in their functionality. In interfaces, none of the methods areimplemented means interfaces defines methodswithout body. Interfaces are syntactically similar to classes, butthey lack instance variables, and their methods aredeclared without any body. But, it can contain final variables, which mustbe initialized with values.
    4. 4. Once it is defined, any number of classes canimplement an interface. One class can implement any number ofinterfaces If we are implementing an interface in a classwe must implement all the methods defined in theinterface as well as a class can also implement itsown methods. Interfaces add most of the functionality that isrequired for many applications which wouldnormally resort to using multiple inheritance in C++.
    5. 5. Defining Interface
    6. 6. Syntax :[Access-specifier] interface interface-name{ Access-specifier return-type method-name(parameter-list); final type var1=value;}
    7. 7. Where, Access-specifier is either public or it isnot given. When no access specifier is used, it results intodefault access specifier and if interface has defaultaccess specifier then it is only available to othermembers of the same package. When it is declared as public, the interface canbe used by any other code of other package.        Interface-Name: name of an interface, it can be any valid identifier.
    8. 8. Any class that includes an interface mustimplement all of the methods. Variables can bedeclared inside interface declarations. They are implicitly final and static, means theycan not be changed by implementing it in a class.They must also be initialized with a constant value.
    9. 9. EX :interface Item{ static final int code = 100; static final String name = "Fan"; void display ( );}interface Area{ static final float pi = 3.14F; float compute ( float x, float y ); void show ( );}
    10. 10. Implementing Interface
    11. 11. Once an interface has been defined, one ormore classes can implement that interface. To implement an interface, include theimplements clause in a class definition, and thencreate the methods declared by the interface. The general form of a class that includes theimplements clause looks like this: Access-specifier class classname [extendssuperclass] [implements interface, [, interface..]]{ // class body }
    12. 12. If a class implements from more than oneinterface, names are separated by comma. If a class implements two interfaces that declarethe same method, then the same method will be usedby clients of either interface. The methods that implement an interface mustbe declared as public. The type-signature of implementing methodmust match exactly the type signature specified in theinterface.
    13. 13. interface religion{ String city = new String("Amritsar"); void greet(); void pray();}class gs implements religion{ public void greet() { System.out.println("We greet - ABCD"); } public void pray() { System.out.println("We pray at " + city + " XYZ "); }}class iface1 Output :{ public static void main(String args[]) { gs sikh = new gs(); We greet - ABCD sikh.greet(); sikh.pray(); We pray at Amritsar XYZ }}
    14. 14. Implementing interface having commonfunction name interface i1{ void disp();}interface i2{ void disp();} class c implements i1, i2{ public void disp() { System.out.println("This is display .. "); }} Output : class iface7{ public static void main(String args[]) { This is display .. c cobj = new c(); cobj.disp(); }}
    15. 15. Note : When implementing an interface method, it mustbe declared as public. It is possible for classes thatimplement interfaces to define additional members oftheir own.
    16. 16. Partial Implementation of Interface : If we want to implement an interface in a classwe have to implement all the methods defined in theinterface. But if a class implements an interface but doesnot fully implement the method defined by thatinterface, then that class must be declared asabstract.
    17. 17. interface i1{ void disp1(); Output : void disp2();}abstract class c1 implements i1{ This is display of 1 public void disp1() { This is display of 2 System.out.println("This is display of 1"); }}class c2 extends c1{ public void disp2() { System.out.println("This is display of 2"); }}class iface{ public static void main(String args[]) { c2 c2obj = new c2(); c2obj.disp1(); c2obj.disp2(); }}
    18. 18. Interface Variable
    19. 19. Accessing interface variable : One can declare variable as object referencesthat uses an interface rather than a class type. When you call a method through one of thesereferences, the correct version will be called based onthe actual instance of the interface being referred to.
    20. 20. nterface AreaCal{ final double pi = 3.14; double areacalculation(double r);}class Circle implements AreaCal{ public double areacalculation(double r) { double ar; ar = pi*r*r; Output : return ar; }} Area of Circle is : 329.89625class iface3{ public static void main(String args[]) { double area; AreaCal ac = new Circle(); area = ac.areacalculation(10.25); System.out.println("Area of Circle is : " + area); }}
    21. 21. Here variable ac is declared to be of the interfacetype AreaCal,it was assigned an instance of circle. Although ac can be used to access thereacalculation() method,it cannot access any othermembers of the client class. An interface reference variable only hasknowledge of the method declared by its interfacedeclaration.
    22. 22. Extending Interface
    23. 23. Extending interfaces : One interface can inherit another by use of thekeyword extends. The syntax is the same as forinheriting classes. When a class implements an interface thatinherits another interface, It must provideimplementation of all methods defined within theinterface inheritance.Note : Any class that implements an interface mustimplement all methods defined by that interface,including any that inherited from other interfaces.
    24. 24. interface if1{ void dispi1();}interface if2 extends if1{ void dispi2();}class cls1 implements if2 Output :{ public void dispi1() { This is display of i1 System.out.println("This is display of i1"); } This is display of i2 public void dispi2() { System.out.println("This is display of i2"); }}public class Ext_iface{ public static void main(String args[]) { cls1 c1obj = new cls1(); c1obj.dispi1(); c1obj.dispi2();}}
    25. 25. Multiple inheritance using interface..class stu{ int rollno; String name = new String(); int marks; stu(int r, String n, int m) { rollno = r; name = n; marks = m; }}interface i{ void display();}class studerived extends stu implements i{ studerived(int r, String n, int m) { super(r,n,m); }
    26. 26. public void display() { System.out.println("Displaying student details .. "); System.out.println("Rollno = " + rollno); System.out.println("Name = " + name); System.out.println("Marks = " + marks); }}public class Multi_inhe_demo{ public static void main(String args[]) { studerived obj = new studerived(1912, "Ram", 75); obj.display(); }} Output : Displaying student details .. Rollno = 1912 Name = Ram Marks = 75
    27. 27. We can make various forms of interfaceimplementation as below
    28. 28.