- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- What happens if the subclass does not override abstract methods in java?
- What happens if we does not initialize variables of an interface in java?
- Must we implement all the methods in a class that implements an interface in Java?
- What happens if we overload default methods of an interface in java?
- Can the abstract methods of an interface throw an exception in java?
- Can we define an abstract class with no abstract methods in Java?
- Difference between Abstract Class and Interface in Java
- Differences between abstract class and interface in Java
- What is the difference between an interface and an abstract class in C#?
- Declare static variables and methods in an abstract class in Java
- Difference Between Interface and Abstract Class in Java & C#
- What happens if we define a concrete method in an interface in java?
- When to use an abstract class and when to use an interface in Java?
- How to implement an interface using an anonymous inner class in Java?
- what are abstract methods in Java?
