- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What happens if a class does not implement all the abstract methods of an interface in java?
Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
In a separate class you need to implement this interface and provide body for all its abstract methods.
Once you implement an interface using a class, you must provide body (implement) to all of its abstract methods or, should declare the class as abstract. If you don’t a compile time error will be generated for each unimplemented method saying “InterfaceExample is not abstract and does not override abstract method method_name in interface_name”.
In the following java program, we have an interface with name MyInterface with 3 abstract methods and, a class implementing this interface, with name InterfaceExample.
In the class we are providing body for only one abstract method (display()).
Example
import java.util.Scanner; interface MyInterface{ public void display(); public void setName(String name); public void setAge(int age); } public class InterfaceExample implements MyInterface{ int age; String name; public void display() { if(18 < this.age && this.age < 26) { System.out.println("Hello "+this.name+" welcome"); } else { System.out.println("Under age"); } } public static void main(String args[]) { } }
Compile time error
On compiling the above program generates the following error
Output
InterfaceExample.java:7: error: InterfaceExample is not abstract and does not override abstract method setAge(int) in MyInterface public class InterfaceExample implements MyInterface{ ^ 1 error
To make this program work, you need to implement all the abstract methods in the class or, declare the class abstract, as shown below −
Example
import java.util.Scanner; interface MyInterface{ public void display(); public void setName(String name); public void setAge(int age); } public class InterfaceExample implements MyInterface{ int age; String name; public void display() { if(18 < this.age && this.age < 26) { System.out.println("Hello "+this.name+" welcome"); } else { System.out.println("Under age"); } } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); InterfaceExample obj = new InterfaceExample(); System.out.println("Enter name:"); obj.setName(sc.next()); System.out.println("Enter age:"); obj.setAge(sc.nextInt()); obj.display(); } }
Output
Enter name: Krishna Enter age: 25 Hello Krishna welcome