- 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 we define a concrete method in an interface in java?
Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods int it.
Concrete methods in an interface
All the methods in an interface must be abstract, you cannot have a concrete method (the one which has body) if you try to do so, it gives you a compile time error saying “interface abstract methods cannot have body”.
Example
In the following Java program, we are trying to write a method with a body (concrete) in an interface.
public interface MyInterface{ public static final int num =40; public void demo(){ System.out.println("This is a concrete method in the interface"); } }
Compile time error
On compiling, this program generates the following compile time error.
MyInterface.java:3: error: interface abstract methods cannot have body public void demo(){ ^ 1 error
- Related Articles
- What happens if we overload default methods of an interface in java?
- What happens if we does not initialize variables of an interface in java?
- Can we define constructor inside an interface in java?
- Can we define an interface inside a Java class?
- Why can't we define a static method in a Java interface?
- What is overloading? What happens if we overload a main method in java?
- Can we define an enum inside a method in Java?
- Can we define a class inside a Java interface?
- What happens if a class does not implement all the abstract methods of an interface in java?
- Can we declare the method of an Interface final in java?
- Can we have a private method or private static method in an interface in Java 9?\n
- What happens if we try to extend a final class in java?
- Can we declare an interface with in another interface in java?
- Default method vs static method in an interface in Java?
- How to call an interface method in Java?

Advertisements