0

I have declared interface:

interface IMenu {
    name: string;
    url: string;
}

And class that realizes this interfaace:

class Menu implements IMenu {
    public name;
    public url;
}

Such as properties in interface are always public, it means class that realizes this interface must contain public properties. Is it possible to make them protected or private?

POV
  • 11,293
  • 34
  • 107
  • 201
  • Possible duplicate of https://stackoverflow.com/questions/7767024/why-c-sharp-compiler-does-not-allows-private-property-setters-in-interfaces – Jay Buckman Feb 07 '19 at 14:18

2 Answers2

2

There is no way to do this. Interface implementations must be public (some languages allow a bit of hiding, such as C# with explicit interface implementations but the implemented properties are still accessible from outside the class).

Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
0
interface IMenu {
    name: string;
    url: string;
}

class Menu implements IMenu {
    private _name: string;
    private _url: string;

    constructor() {
        _name = "name";
        _url = "url";
    }

    get name(){
        // your implementation to expose name
    }

    set name(value){
        // your implementation to set name         
    }
 }

You can define Getter and Setter for make Public variable from Interface Private/Protected on a class.

Xexeno
  • 19
  • 3