Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Default Methods | Section
Java Object-Oriented Programming (OOP) Fundamentals

bookDefault Methods

Abstract class vs Interface: The Interface Strikes Back!

Previously, methods with implementations could only exist in abstract classes, but starting from Java 8, they also appeared in interfaces.

Let's take a look at the syntax for declaring a default method:

default dataType methodName(parameters) {
// implementation 
}

We need default methods for the same purposes as regular methods in Java. We use them when we need an implementation in the method and when we want to implement only the interface without additional classes. Let's consider a small example using default methods:

Main.java

Main.java

copy
12345678910111213141516171819202122232425
package com.example; public class Main { public interface Greeting { default void sayHello() { System.out.println("Hello from the interface!"); } void greet(String name); } public static class GreetingImpl implements Greeting { @Override public void greet(String name) { System.out.println("Hello, " + name + "!"); } } public static void main(String[] args) { GreetingImpl greetingImpl = new GreetingImpl(); greetingImpl.sayHello(); // Output: Hello from the interface! greetingImpl.greet("Alice"); // Output: Hello, Alice! } }

In this example, the interface Greeting contains a default method sayHello() with a default implementation. The class GreetingImpl implements this interface and overrides the greet() method. In the main method, an instance of GreetingImpl is created and both methods are called.

question mark

What is the purpose of a default method in a Java interface?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 27

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookDefault Methods

Свайпніть щоб показати меню

Abstract class vs Interface: The Interface Strikes Back!

Previously, methods with implementations could only exist in abstract classes, but starting from Java 8, they also appeared in interfaces.

Let's take a look at the syntax for declaring a default method:

default dataType methodName(parameters) {
// implementation 
}

We need default methods for the same purposes as regular methods in Java. We use them when we need an implementation in the method and when we want to implement only the interface without additional classes. Let's consider a small example using default methods:

Main.java

Main.java

copy
12345678910111213141516171819202122232425
package com.example; public class Main { public interface Greeting { default void sayHello() { System.out.println("Hello from the interface!"); } void greet(String name); } public static class GreetingImpl implements Greeting { @Override public void greet(String name) { System.out.println("Hello, " + name + "!"); } } public static void main(String[] args) { GreetingImpl greetingImpl = new GreetingImpl(); greetingImpl.sayHello(); // Output: Hello from the interface! greetingImpl.greet("Alice"); // Output: Hello, Alice! } }

In this example, the interface Greeting contains a default method sayHello() with a default implementation. The class GreetingImpl implements this interface and overrides the greet() method. In the main method, an instance of GreetingImpl is created and both methods are called.

question mark

What is the purpose of a default method in a Java interface?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 27
some-alt