I'm new to c#, and I'm kinda struggling with implementing interfaces, gonna be very grateful if someone helps me with this, with an explanation. Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace interfejsi
interface Figura
{
String Plostina ();
String Perimeter ();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace interfejsi
{
class Kvadar : Figura
{
int a,b,c;
public String Perimetar(int a, int b, int c)
{
return (a + b + c).ToString();
}
public String Plostina(int a, int b, int c)
{
return (a * b * c).ToString();
}
}
}
The error thrown is usually “Cannot implement an interface member because it is not public”. If an interface member method is implemented in your class without an access modifier, it is by default private. To change the visibility, you can either change the interface from public to internal.
An interface defines a contract. Any class or struct that implements that contract must provide an implementation of the members defined in the interface. An interface may define a default implementation for members. It may also define static members in order to provide a single implementation for common functionality.
The internal interface is properties and methods that can be accessed only from other methods of the object, they are also called "private" (there are other terms, we will meet them further). The external interface is the properties and methods available outside the object, they are called "public".
You need to implement the exact functions that sit in the interface(with the same number of input params)..
So in you case change your interface to:
interface Figura
{
String Perimetar(int a, int b, int c)
String Plostina(int a, int b, int c)
}
Or change you implementation to functions with no params.
The Method definitions do not match. In the interface the Method String Plostina ()
is defined, but the class does not have that method. The method in the class has a different signature, in the class it looks like String Plostina(int a, int b, int c)
.
To implement an interface, the Name, return type and parameters (amount, order and type) must match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With