An interface is used in java to outline specific behaviors of a group of classes that is Comparable (able to be compared). Interface is like set of promises which need to be kept when it is used in a class. These common behaviors translated into methods. All methods in interface are public and abstract (unimplemented). No method in interface can have body. It is outline of methods where method signature is decided but implementation is left when it is used. It makes sense to implement a Comparable class, since many different objects have vastly different ways to compare each other; thus, the behavior is outlined, and future classes can implement said behaviors (i.e. methods).
All methods in interfaces are abstract. Any class implements interface must be called abstract until the class provides definition to all its interface.
Interface Sample :
public interface CalcInter //Inter is name of method { int add ( int firstValue, int secondValue ); //all methods of public by default int substract ( int firstValue, int secondValue ); int mutiply (int firstValue, int secondValue); float divide ( int firstValue, int secondValue ); int value = 12; // variable are public, static and final by default. };
Interface may contain methods and variable. Variables are public, static (initialized at compile time ) and final ( can not be changed). One interface can extend another interface. It allows all types of extends ( as given below) 1. One level of Inheritance 2. Multi Level of Inheritance 3. Multiple Inheritance
interface TestInterface { int testMethod( int firstValue, int secondValue ); //all methods of public by default } interface Iter1 { int add ( int firstValue, int secondValue ); //all methods of public by default } interface Inter2 extends Iner1 //one level of interhitence { float divide ( int firstValue, int secondValue ); } interface Inter3 extends Inter2 //multi level of inheritence { int substract ( int firstValue, int secondValue ); } interface Inter4 extends TestInterface, Inter3 //multiple inheritence { void someMethod(); }
Please let me know your feedback on this post. Your feedback is really important for us.
0 comments:
Post a Comment