Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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 Advertisements
