Home » Java programming language

Can we define private and protected modifiers for the members in interfaces in Java?

Here, we are going to learn that can we define private and protected modifiers for the members in interfaces in Java programming language?
Submitted by Preeti Jain, on September 17, 2019

No, it is not possible to define private and protected modifiers for the members in interfaces in Java.

  • As we know that, the members defined in interfaces are implicitly public or in other words, we can say the member defined in an interface is by default public.
  • It is possible to define public modifiers for the member in interfaces.
  • In the case of public modifiers, it is not mandatory to prefix "public" with members in interfaces because all the members of the interface are by default public.
  • We will discuss three cases in terms of modifiers for the members in interfaces.
    1. What will happen, if we define private modifiers for the members in an interface?
    2. What will happen, if we define protected modifiers for the members in the interface?
    3. What will happen, if we define public modifiers for the members in the interface?
    4. What will happen, if we define no modifiers for the members in the interface?

We will see each of the above cases one by one with the help of an example...

1) Defining private modifiers for the member in interface in Java

// Java program to demonstrate the example of
// defining private members for the interface 

interface PrivateMemberInterface {
    private String str = "There will be an error.";
    void display();
}

public class Main implements PrivateMemberInterface {
    // override display() of PrivateMemberInterface
    public void display() {
        System.out.print("Private modifiers not allowed");
        System.out.print("for Data Members");
    }

    public static void main(String[] args) {
        // class instantiation
        Main m = new Main();

        // calling display() of Main class
        m.display();

        // Accessing private member of an interface
        System.out.println(str);
    }
}

Output

/Main.java:5: error: modifier private not allowed here
    private String str = "There will be an error.";
                   ^
1 error

Conclusion: We cannot define private modifiers for the members in interface.

2) Defining protected modifiers for the member in interface in Java

// Java program to demonstrate the example of
// defining protected members for the interface 

interface ProtectedMemberInterface {
    protected String str = "There will be an error.";
    void display();

}

public class Main implements ProtectedMemberInterface {
    // override display() of ProtectedMemberInterface
    public void display() {
        System.out.print("Protected modifiers not allowed");
        System.out.print("for Data Members");
    }

    public static void main(String[] args) {
        // class instantiation
        Main m = new Main();
        // calling display() of Main class
        m.display();
        // Accessing protected member of an interface
        System.out.println(str);
    }
}

Output

/Main.java:5: error: modifier protected not allowed here
    protected String str = "There will be an error.";
                     ^
1 error

Conclusion: We cannot define protected modifiers also for the members in interface.

3) Defining public modifiers for the member in interface in Java

// Java program to demonstrate the example of
// defining public members for the interface 

interface PublicMemberInterface {
    public String str = "No error here...";
    void display();
}

public class Main implements PublicMemberInterface {
    // override display() of PublicMemberInterface
    public void display() {
        System.out.print("Public modifiers are allowed" + " ");
        System.out.print("for Data Members");
    }

    public static void main(String[] args) {
        // class instantiation
        Main m = new Main();
        // calling display() of Main class
        m.display();
        System.out.println();
        // Accessing public member of an interface
        System.out.println(str);
    }
}

Output

Public modifiers are allowed for Data Members
No error here...

Conclusion: We can define public modifiers for the members in an interface and it is valid in java.

4) Not defining any modifiers for the member in interface in Java

// Java program to demonstrate the example of
// not defining any modifier for the members in 
// interface 

interface NoModifierMemberInterface {
    String str = "No error here...";
    void display();
}

public class Main implements NoModifierMemberInterface {
    // override display() of NoModifierMemberInterface
    public void display() {
        System.out.print("No modifiers are allowed" + " ");
        System.out.print("for Data Members");
    }

    public static void main(String[] args) {
        // class instantiation
        Main m = new Main();
        // calling display() of Main class
        m.display();
        System.out.println();
        // Accessing no modifiers member of an interface
        System.out.println(str);
    }
}

Output

No modifiers are allowed for Data Members
No error here...

Conclusion: We can define no modifiers for the members in the interface and it is valid in java because by default modifier for the member is public.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.