Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java Default Methods

  • Java 8 introduced the concept of Default Methods in interfaces. Default methods are those methods in an interface that have a default implementation and are not abstract methods. Although it is possible, doing so is not required when implementing classes.

Java Default Method Example

public interface Greeting {
    
    void greet(String name);
    
    default void sayHello(String name) {
        System.out.println("Hello, " + name);
    }
}

public class HelloWorld implements Greeting {
    
    @Override
    public void greet(String name) {
        System.out.println("Welcome, " + name);
    }
    
    @Override
    public void sayHello(String name) {
        System.out.println("Hi, " + name);
    }
    
    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.greet("John");
        helloWorld.sayHello("John");
    }
}

Output:

Welcome, John
Hi, John 


Static Methods inside Java 8 Interface

In the interface, static methods can also be defined. Utility methods are defined using static methods.

public interface MyInterface {
    void myMethod();

static void myStaticMethod() {
  System.out.println("This is a static method inside MyInterface.");
}
}

public class MyClass implements MyInterface {
public void myMethod() {
  System.out.println("This is MyClass implementation of myMethod.");
}
}

public class Main {
public static void main(String[] args) {
  MyClass obj = new MyClass();
  obj.myMethod();

  // call the static method without creating an object
  MyInterface.myStaticMethod();
}
}

In the above example, we declare a static method myStaticMethod() inside the interface MyInterface. We can call this static method without creating an object of any implementing class, using the interface name followed by the method name (MyInterface.myStaticMethod()).


It is significant to note that interface static methods can only be accessed by their interface name and cannot be overridden by implementing classes. Furthermore, because they are not connected to any specific instance of the interface, static methods cannot access instance variables or instance methods of the interface.


Static methods in interfaces can be useful for providing utility methods that are related to the interface but are not specific to any implementing class.


Output:

This is MyClass implementation of myMethod.
This is a static method inside MyInterface.
 

Java for Each() Method Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
Java for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.