Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Java Interface


Java Interface

In Java, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. It defines a contract that classes can implement, specifying a set of methods that implementing classes must provide. Interfaces enable multiple inheritance in Java and are used to achieve abstraction and polymorphism. Here's an overview of Java interfaces:

Declaring Interfaces:

Example of an interface:


        public interface Drawable {
            void draw(); // Abstract method
        }
      

Implementing Interfaces:

Example of implementing an interface:

        public class Circle implements Drawable {
            @Override
            public void draw() {
                System.out.println("Drawing a circle");
            }
        }
      

Interface Default Methods:

Example of a default method in an interface:


        public interface Drawable {
            void draw(); // Abstract method
            
            default void resize() {
                System.out.println("Resizing the shape");
            }
        }
      

Interface Static Methods:

Example of a static method in an interface:


        public interface Drawable {
            void draw(); // Abstract method
            
            default void resize() {
                System.out.println("Resizing the shape");
            }
            
            static void display() {
                System.out.println("Displaying the shape");
            }
        }
     

Benefits of Interfaces:



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java