1

I have the scenario where we used to implement the interface members using explicit implementation in C#. Currently I need to achieve the same behavior in JAVA, but couldn’t explicitly call the interface members, hence it shows error to mark the implemented member as public. I am new to JAVA, kindly let me know how the achieve the same behavior in JAVA.

C# Code:

public class WF_33470: IData
{
    private string m_data = string.Empty;
    string IData.Element
    {
        get
        {
            return m_data;
        }
    }
    string IData.Data()
    {
        get
        {
            return "Hello World";
        }
    {
}

    internal interface IData
    {
        string Element
        {
            get;
        }
        string Data();
    }

Java Code:

public class WF_33470 implements  IData
{
    private  String m_data = "";
    String getElement()throws Exception{//throws error to change the modifier to be public, but i need to achieve explicit implementation here
        return m_data;
    }
    String  Data()throws Exception{//throws error to change the modifier to be public, but i need to achieve explicit implementation here
        return "Hello World";
    }
}

interface IData
{
    String getElement()throws Exception;
    String Data()throws Exception;
}
Bagus Tesa
  • 1,317
  • 2
  • 19
  • 42
Karthikk
  • 326
  • 2
  • 7
  • 1
    in java `interface` methods are automaticly defined as `public` and as you can´t lower the visibility when inheriting you need to define them as public, while implementing the interface, aswell (`public String Data()throws Exception{...}`) – SomeJavaGuy Dec 27 '16 at 07:09
  • what do you mean by `explicit implementation`? @KevinEsche is right, interface always public ([related QA](http://stackoverflow.com/questions/9614708/why-should-we-declare-interface-methods-as-public)). – Bagus Tesa Dec 27 '16 at 07:18
  • is there any possibility to lower the visibility of the inherited member? – Karthikk Dec 27 '16 at 07:20
  • @user2810266, what about using `abstract class` instead? although it cure *ought to be public* problem, it'll hinder you from doing `extends` on implementation class.. design decision.. – Bagus Tesa Dec 27 '16 at 07:48

2 Answers2

1

Interfaces are simply for exposing public functionality to other classes. You use interfaces to allow people from outside your code to interact with your code. To do this, you need to define your methods public.

If you would like to force someone to override a given set of private methods, you might want to declare an abstract class with a series of abstract protected methods.

Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
0

You can´t do what you are currently doing, reducing the visibility of a method:

given JLS 9.1.1

The access modifier public (§6.6) pertains to every kind of interface declaration.

Given the rules defined in JLS 8.4.8.3

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, as follows:

If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs. [....]

As you can see it´s defined that every method defined within an interface is implicity public, even if no access modifier is provided. Given the rules in the later JLS part it´s not possible to reduce the visibility of a given method, so the result of you defining the explicit implementation of the method in the class WF_33470without the public modifier will be a compile time error as you try to lower the methods visibility.

You need to define them as

public String getElement()throws Exception{
    return m_data;
}
public String  Data()throws Exception{
    return "Hello World";
}

If you would want to have a lower visibility you might want to go with an abstract class as following:

public class WF_33470 extends IData {
    private String m_data = "";

    String getElement() throws Exception {
        return m_data;
    }

    String Data() throws Exception {
        return "Hello World";
    }
}

abstract class IData {
    abstract String getElement() throws Exception;

    abstract String Data() throws Exception;
}
Community
  • 1
  • 1
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33