• Like
  • Save
Interface
Upcoming SlideShare
Loading in...5
×
 

Interface

on

  • 5,215 views

interface of java ppt define and implement interface, extending interface

interface of java ppt define and implement interface, extending interface

Statistics

Views

Total Views
5,215
Views on SlideShare
3,696
Embed Views
1,519

Actions

Likes
10
Downloads
0
Comments
2

2 Embeds 1,519

http://www.java2all.com 1402
http://java2all.com 117

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • White Space Characters
  • White Space Characters
  • White Space Characters

Interface Interface Presentation Transcript

  • Chapter 8Interface http://www.java2all.com
  • Interface http://www.java2all.com
  • 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. http://www.java2all.com
  • 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++. http://www.java2all.com
  • Defining Interface http://www.java2all.com
  • Syntax :[Access-specifier] interface interface-name{ Access-specifier return-type method-name(parameter-list); final type var1=value;} http://www.java2all.com
  • 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.  http://www.java2all.com
  • 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. http://www.java2all.com
  • 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 ( );} http://www.java2all.com
  • Implementing Interface http://www.java2all.com
  • 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 } http://www.java2all.com
  • 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. http://www.java2all.com
  • 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 }} http://www.java2all.com
  • 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(); }} http://www.java2all.com
  • Note : When implementing an interface method, it mustbe declared as public. It is possible for classes thatimplement interfaces to define additional members oftheir own. http://www.java2all.com
  • 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. http://www.java2all.com
  • 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(); }} http://www.java2all.com
  • Interface Variable http://www.java2all.com
  • 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. http://www.java2all.com
  • 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); }} http://www.java2all.com
  • 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. http://www.java2all.com
  • Extending Interface http://www.java2all.com
  • 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. http://www.java2all.com
  • 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();}} http://www.java2all.com
  • 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); } http://www.java2all.com
  • 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 http://www.java2all.com
  • We can make various forms of interfaceimplementation as below http://www.java2all.com
  • http://www.java2all.com