
Interface in Java
What is Inheritance ? An interface is a completely abstract blueprint that defines what a class must do but not how it does it. It contains abstract methods that implementing classes must provide. It achieves 100% abstraction and supports multiple inheritance in Java. Example : interface Animal { void sound(); // automatically public abstract } class Dog implements Animal { @Override public void sound() { System.out.println("Bark"); } } By default methods are : public and abstract. By default variables are : public static final. Can you create constructor in interface ? No, because interfaces cannot be instantiated and do not have instance variables to initialize. Can you create object for interface ? No, because it is incomplete and contains abstract methods without implementation. An object requires complete method definitions, which are provided by implementing classes. Default methods in interface: It is a method inside an interface that has a body (implementation). Before Java 8 →
Continue reading on Dev.to
Opens in a new tab



