INTERFACE(Written by- Krishnamoorthy D) (Emailid: [email protected]) (Designation: SMTS in Oracle India Pvt
Ltd., Bangalore.) This tutorial gives the more detail about the interface and possible ways to use. Introduction:OOPs: Interface is the way to say external behavior to the particular object. This behavior may be used for other Object, which is created from some other class. Interface is a purely abstract class. Interface technique is found to implement the encapsulation. In Java, multiple inheritances are not possible. Interface gives the facility to implement this multiple inheritance. In modeling language, implementing Interface association is called as realization. The variables, which are declared in interface, are public, final, and static. Class
can inherit only one other class, but one or more interface can be implemented
for a class. This class should
implement all the methods, which are declared in interfaces. If the class is
abstract then there is no need to implement, but the class should implement
which is inheriting this abstract class. Simple Interface:Interface can contain methods and variables. Variables should be initialized.
Class within Interface:Interface can contain class also. This class has public visibility to the class, which implements this interface. Compiler for the class within the Interface implicitly assumes public access modifier. Static class also is in interface.
Nested Interface: Interface may contain another interface. This interface will be called using the parentinterface.chldinterface from outside the parent interface. If the interface implemented within the same interface, then parent interface name is not required to implement child interface.
Access modifier:Interface can be declared with the access modifier. If the interface is declared not within the class or interface, it can't be declared as private interface. It should be some other access modifier. Abstract Keyword:Interface can be declared with the keyword "abstract". This keyword does not give any additional interface to compiler. If this keyword is not given to interface, compiler will assumes itself. Interface within Class:Interface may be declared within the class, and Interface. Interface can't be declared within the method. If the interface is declared within the class, this interface will be implemented by some other class which are declared within same class or can be accessed through the other class. Interface declared within Class will be treated as static implicitly.
Interface extends Interface:Inheritance is possible in interface also. An interface can extend one or more interfaces using the keyword. Interface can't implement other interface.
Variables:All the variables what are declared in interface are public, final, and static. The value should be assigned in interface itself. This value can't be initialized in constructor method like normal final variable in class. Access Modifier/Access specifier:Access modifier and Access specifier is not required to explicit to interface variables. Ambiguous field:Two interfaces has same variable and both the interface are implemented in a class then we will get Ambiguous error.
Overriding Field:An Interface has one variable. Another one interface extends this interface and declares the same variable, this is called overriding field in interface.
Methods:Methods, which are defined in the interface, are abstract method and also public one. This method should be implemented in the class, which is implementing this interface. More than one interface can have same signature method and may these interfaces implemented in a single class. It won't give any ambiguous error. This is the good future in java. Interfaces implemented class's object can be assigned to interfaces. This object is called as Interface object and this will give the access to the method what are all defined with same signature within this interface.
In the above code, int getI() are the methods in both the interfaces. They were defined differently but both are same. getI() method will perform the same operation when this called from any of this two interface objects. Overriding Method:An Interface has one method. Another one interface extends this interface and defines method with the same signature, this is called overriding method in interface. In interface, Overriding is not really happened.
Now the object c is assigned to the super interface I1 of I2. I1 and I2 have same method. When ever object is assigned to I1, implemented getI() method will be mean as from I1. Implement Interface in Abstract Class:Interface may be implemented in Abstract class. There is no need to implement all the methods. These methods may be implemented later in the class which is extending this abstract class. Instead of implementing the method, can be declared as abstract method in abstract class.
|
|