Interfaces
- Eclipse: Oxygen
- Java: 1.8
This tutorial deals with interfaces. The interfaces are somewhat similar to a class but have very interesting properties. Interfaces serve as an outline of the class ‘implementing it'(inherit). The members declared in it are public, static by default.
Hence we can have a constructor for an abstract class (which will be discussed further) but not for the interface as it contains static members.
If any method (abstract by default) is not declared in the class implementing it then the class has to declared as abstract.
Filename: Iphone8.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Iphone8 implements Phone{ @Override public String processor() { return "A11"; } @Override public String OS() { return "IOS"; } @Override public int spaceInGB() { return 64; } } |
Filename: Oneplus5.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class OnePlus5 implements Phone{ @Override public String processor() { return "SD835"; } @Override public String OS() { return "Android"; } @Override public int spaceInGB() { return 64; } } |
Filename: Phone.java
1 2 3 4 5 6 |
public interface Phone { String processor(); String OS(); int spaceInGB(); } |
Filename: Hello.java
1 2 3 4 5 |
public static void main(String[] args) { Phone phone = new OnePlus5(); System.out.println("Processor: "+phone.processor()); System.out.println("Space in GB: "+phone.spaceInGB()); } |
Output:
1 2 |
Processor: SD835 Space in GB: 64 |
Contributed by: Poonam Tomar