|
| 1 | +### Interface in Java |
| 2 | + |
| 3 | +----- |
| 4 | + |
| 5 | +An interface in java is a blueprint of a class. It has static constants and abstract methods. |
| 6 | + |
| 7 | +The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. |
| 8 | + |
| 9 | +In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body. |
| 10 | + |
| 11 | +Java Interface also represents the IS-A relationship. |
| 12 | + |
| 13 | +It cannot be instantiated just like the abstract class. |
| 14 | + |
| 15 | +Since Java 8, we can have default and static methods in an interface. |
| 16 | + |
| 17 | +Since Java 9, we can have private methods in an interface. |
| 18 | + |
| 19 | + |
| 20 | +##### Why use Java interface? |
| 21 | + |
| 22 | +There are mainly three reasons to use interface. They are given below. |
| 23 | + |
| 24 | + - It is used to achieve abstraction. |
| 25 | + - By interface, we can support the functionality of multiple inheritance. |
| 26 | + - It can be used to achieve loose coupling. |
| 27 | + |
| 28 | +---- |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +---- |
| 33 | + |
| 34 | +##### How to declare an interface? |
| 35 | + |
| 36 | +An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface. |
| 37 | + |
| 38 | +```java |
| 39 | + interface <interface_name>{ |
| 40 | + |
| 41 | + // declare constant fields |
| 42 | + // declare methods that abstract |
| 43 | + // by default. |
| 44 | + } |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | +#### Internal addition by the compiler |
| 49 | + |
| 50 | +The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static and final keywords before data members. |
| 51 | + |
| 52 | +In other words, Interface fields are public, static and final by default, and the methods are public and abstract. |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +------ |
| 57 | + |
| 58 | +##### The relationship between classes and interfaces |
| 59 | + |
| 60 | +As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +##### Java Interface Example |
| 65 | + |
| 66 | +```java |
| 67 | + interface printable{ |
| 68 | + void print(); |
| 69 | + } |
| 70 | + class A6 implements printable{ |
| 71 | + public void print(){System.out.println("Hello");} |
| 72 | + |
| 73 | + public static void main(String args[]){ |
| 74 | + A6 obj = new A6(); |
| 75 | + obj.print(); |
| 76 | + } |
| 77 | + } |
| 78 | +``` |
| 79 | +Output |
| 80 | +``` |
| 81 | +Hello |
| 82 | +``` |
| 83 | + |
| 84 | +---- |
| 85 | + |
| 86 | +##### Java Interface Example: Drawable |
| 87 | + |
| 88 | +In this example, the Drawable interface has only one method. Its implementation is provided by Rectangle and Circle classes. In a real scenario, an interface is defined by someone else, but its implementation is provided by different implementation providers. Moreover, it is used by someone else. The implementation part is hidden by the user who uses the interface. |
| 89 | + |
| 90 | +```java |
| 91 | +//Interface declaration: by first user |
| 92 | +interface Drawable{ |
| 93 | +void draw(); |
| 94 | +} |
| 95 | +//Implementation: by second user |
| 96 | +class Rectangle implements Drawable{ |
| 97 | +public void draw(){System.out.println("drawing rectangle");} |
| 98 | +} |
| 99 | +class Circle implements Drawable{ |
| 100 | +public void draw(){System.out.println("drawing circle");} |
| 101 | +} |
| 102 | +//Using interface: by third user |
| 103 | +class TestInterface1{ |
| 104 | +public static void main(String args[]){ |
| 105 | +Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable() |
| 106 | +d.draw(); |
| 107 | +}} |
| 108 | +``` |
| 109 | +Output |
| 110 | +``` |
| 111 | +drawing circle |
| 112 | +``` |
| 113 | + |
| 114 | +---- |
| 115 | + |
| 116 | +##### Java Interface Example: Bank |
| 117 | + |
| 118 | +Let's see another example of java interface which provides the implementation of Bank interface. |
| 119 | + |
| 120 | +```java |
| 121 | + interface Bank{ |
| 122 | + float rateOfInterest(); |
| 123 | + } |
| 124 | + class SBI implements Bank{ |
| 125 | + public float rateOfInterest(){return 9.15f;} |
| 126 | + } |
| 127 | + class PNB implements Bank{ |
| 128 | + public float rateOfInterest(){return 9.7f;} |
| 129 | + } |
| 130 | + class TestInterface2{ |
| 131 | + public static void main(String[] args){ |
| 132 | + Bank b=new SBI(); |
| 133 | + System.out.println("ROI: "+b.rateOfInterest()); |
| 134 | + }} |
| 135 | +``` |
| 136 | +Output |
| 137 | +``` |
| 138 | +ROI: 9.15 |
| 139 | +``` |
| 140 | + |
| 141 | +---- |
| 142 | + |
| 143 | +##### Multiple inheritance in Java by interface |
| 144 | + |
| 145 | +If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance. |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +------- |
| 150 | + |
| 151 | +Example |
| 152 | +```java |
| 153 | + interface Printable{ |
| 154 | + void print(); |
| 155 | + } |
| 156 | + interface Showable{ |
| 157 | + void show(); |
| 158 | + } |
| 159 | + class A7 implements Printable,Showable{ |
| 160 | + public void print(){System.out.println("Hello");} |
| 161 | + public void show(){System.out.println("Welcome");} |
| 162 | + |
| 163 | + public static void main(String args[]){ |
| 164 | + A7 obj = new A7(); |
| 165 | + obj.print(); |
| 166 | + obj.show(); |
| 167 | + } |
| 168 | + } |
| 169 | +``` |
| 170 | +Output |
| 171 | +``` |
| 172 | +Hello |
| 173 | +Welcome |
| 174 | +``` |
| 175 | + |
| 176 | +----- |
| 177 | + |
| 178 | +##### Q) Multiple inheritance is not supported through class in java, but it is possible by an interface, why? |
| 179 | + |
| 180 | +As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class. For example: |
| 181 | + |
| 182 | +```java |
| 183 | + interface Printable{ |
| 184 | + void print(); |
| 185 | + } |
| 186 | + interface Showable{ |
| 187 | + void print(); |
| 188 | + } |
| 189 | + |
| 190 | + class TestInterface3 implements Printable, Showable{ |
| 191 | + public void print(){System.out.println("Hello");} |
| 192 | + public static void main(String args[]){ |
| 193 | + TestInterface3 obj = new TestInterface3(); |
| 194 | + obj.print(); |
| 195 | + } |
| 196 | + } |
| 197 | +``` |
| 198 | +Output |
| 199 | +``` |
| 200 | +Hello |
| 201 | +``` |
| 202 | + |
| 203 | +----- |
| 204 | + |
| 205 | +##### Interface inheritance |
| 206 | +```java |
| 207 | + interface Printable{ |
| 208 | + void print(); |
| 209 | + } |
| 210 | + interface Showable extends Printable{ |
| 211 | + void show(); |
| 212 | + } |
| 213 | + class TestInterface4 implements Showable{ |
| 214 | + public void print(){System.out.println("Hello");} |
| 215 | + public void show(){System.out.println("Welcome");} |
| 216 | + |
| 217 | + public static void main(String args[]){ |
| 218 | + TestInterface4 obj = new TestInterface4(); |
| 219 | + obj.print(); |
| 220 | + obj.show(); |
| 221 | + } |
| 222 | + } |
| 223 | +``` |
| 224 | +Output |
| 225 | +``` |
| 226 | +Hello |
| 227 | +Welcome |
| 228 | +``` |
0 commit comments