Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameters and inheritance

I understand optional parameters, and I quite like them, but I'd just like to know a bit more about using them with an inherited interface.

Exhibit A

interface IMyInterface
{
    string Get();
    string Get(string str);
}

class MyClass : IMyInterface
{
    public string Get(string str = null)
    {
        return str;
    }
}

Now I would've thought that the Get method in MyClass inherits both of the interface's methods, but...

'MyClass' does not implement interface member 'MyInterface.Get()'

Is there a good reason for this?

Perhaps I should go about this by putting the optional parameters in the interface you say? But what about this?

Exhibit B

interface IMyInterface
{
    string Get(string str= "Default");
}

class MyClass : IMyInterface
{
    public string Get(string str = "A different string!")
    {
        return str;
    }
}

And this code compiles fine. But that can't be right surely? Then with a bit more digging, I discovered this:

  IMyInterface obj = new MyClass();
  Console.WriteLine(obj.Get()); // writes "Default"

  MyClass cls = new MyClass();
  Console.WriteLine(cls.Get()); // writes "A different string!"

It would seem to be that the calling code is getting the value of the optional parameter based on the objects declared type, then passing it to the method. This, to me, seems a bit daft. Maybe optional parameters and method overloads both have their scenarios when they should be used?

My question

My calling code being passed an instance of IMyInterface and needs to call both methods here at different points.

Will I be forced to implement the same method overload in every implementation?

public string Get()
{
  return Get("Default");
}
like image 465
Connell Avatar asked Nov 11 '11 13:11

Connell


People also ask

What is a optional parameter?

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list aren't supported.

What is an optional parameter in a function?

By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.

How do you pass optional parameters?

By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method.

Can we pass optional parameters in Java?

Unlike some languages such as Kotlin and Python, Java doesn't provide built-in support for optional parameter values. Callers of a method must supply all of the variables defined in the method declaration.


1 Answers

What I also didn't realise, is that optional parameters don't change the method signature. So the following code is perfectly legal and was actually my solution:

interface IMyInterface
{
    string Get(string str = "Default");
}

class MyClass : IMyInterface
{
    public string Get(string str)
    {
        return str;
    }
}

So if I have an instance of MyClass, I must call Get(string str), but if that instance has been declared as the base interface IMyInterface, I can still call Get(), which gets the default value from IMyInterface first, then invokes the method.

like image 52
Connell Avatar answered Nov 14 '22 20:11

Connell