কম্পিউটার টিউটোরিয়াল

জাভা ৯-এ ইন্টারফেসে প্রাইভেট মেথড ব্যবহার করা যায় কি?


হ্যাঁ, জাভা ৯以降 ইন্টারফেসে প্রাইভেট মেথড ব্যবহার করা যায়।

উদাহরণ

interface MyInterface { 
    public abstract void demo(); 
    public default void defaultMethod() { 
        privateMethod(); 
        staticPrivateMethod(); 
        System.out.println("This is a default method of the interface"); 
    } 
    public static void staticMethod() { 
        staticPrivateMethod(); 
        System.out.println("This is a static method of the interface"); 
    } 
    private void privateMethod(){ 
        System.out.println("This is a private method of the interface"); 
    } 
    private static void staticPrivateMethod(){ 
        System.out.println("This is a static private method of the interface"); 
    } 
} 
public class InterfaceMethodsExample implements MyInterface { 
    public void demo() { 
        System.out.println("Implementation of the demo method"); 
    } 
    public static void main(String[] args){ 
        InterfaceMethodsExample obj = new InterfaceMethodsExample(); 
        obj.defaultMethod(); 
        obj.demo(); 
        MyInterface.staticMethod(); 
    } 
}

আউটপুট

This is a private method of the interface 
This is a static private method of the interface 
This is a default method of the interface 
Implementation of the demo method 
This is a static private method of the interface 
This is a static method of the interface
  1. জাভাতে pack() মেথড কখন ব্যবহার করবেন

  2. জাভাতে কনস্ট্রাক্টরকে private ডিক্লেয়ার করা যায় কি?

  3. জাভাতে মেইন মেথড প্রাইভেট ঘোষণা করা যায় কি?

  4. জাভা ৮-এ ইন্টারফেস এনহ্যান্সমেন্ট