
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can we override default methods in Java?
An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
Since Java8 static methods and default methods are introduced in interfaces. Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.
In short, you can access the default methods of an interface using the objects of the implementing classes.
Example
interface MyInterface{ public static int num = 100; public default void display() { System.out.println("display method of MyInterface"); } } public class InterfaceExample implements MyInterface{ public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.display(); } }
Output
display method of MyInterface
Overriding default methods
you can override a default method of an interface from the implementing class.
Example
interface MyInterface{ public static int num = 100; public default void display() { System.out.println("display method of MyInterface"); } } public class InterfaceExample implements MyInterface{ public void display() { System.out.println("display method of class"); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.display(); } }
Output
display method of class
- Related Articles
- Can we override final methods in Java?
- Can we override private methods in Java\n
- Can we Overload or Override static methods in Java?
- Why can’t we override static methods in Java?
- Is it mandatory to override the default methods of an interface in Java?
- Can we override a protected method in Java?
- Can we override a start() method in Java?
- Can we override the static method in Java?
- Can we override the equals() method in Java?
- Can we override the main method in java?
- Can we to override a catch block in java?
- Default methods in Java
- Can we override a private or static method in Java
- Can we overload or override a static method in Java?\n
- Can we override only one method while implementing Java interface?

Advertisements