I saw the following class declaration in vb.net:
Public Class [Interface] Implements TestInterface
What does the above mean? Is there an equivalent in c#?
It's a normal class that is named Interface.
Since Interface is also a keyword, it must be wrapped in brackets to force the compiler to parse it as an identifier.
In C#, you can write public class #interface : TestInterface (It must be lowercase to conflict with the C# keyword).
In either language, don't do this. It's confusing and serves no purpose.
Related
When I define an enum class in Kotlin
enum class Answer {
YES,
NO
}
It has a valueOf(value: String) attached to it.
val doYouWantBeerOrCoffee = Answer.valueOf("YES") // Answer.YES
But where is this function actually defined? It is definitely not in the Enum.Kt and using Idea's Go to Implementation tool only takes me back to my Answer enum definition.
It's generated by the compiler. That's what "synthetic" means in
Enum classes in Kotlin have synthetic methods allowing to list the defined enum constants and to get an enum constant by its name.
If you decompile Answer.class you'll see it, but it isn't written as Kotlin (or Java) source code anywhere.
This method is a part of JDK, and defined in Enum.java class.
Which is the common base class of all Java language enumeration types.
Kotlin uses the same class for enums
I included a library I'd like to use, but in accessing to one of its classes I get the error message,
"Cannot access '<init>': it is private in [class name]
Is there something I can do to rectify this on my side, or am I just stuck to not use the package?
The error means the constructor is private. Given your comment, I'm assuming you're using a library. If this is the case, you'll have to find a different way to initialize it. Some libraries have factories or builders for classes, so look up any applicable documentation (if it is a library or framework). Others also use the singleton pattern, or other forms of initialization where you, the developer, don't use the constructor directly.
If, however, it is your code, remove private from the constructor(s). If it's internal and you're trying to access it outside the module, remove internal. Remember, the default accessibility is public. Alternatively, you can use the builder pattern, factory pattern, or anything similar yourself if you want to keep the constructor private or internal.
I came across this issue when trying to extend a sealed class in another file. Without seeing the library code it is hard to know if that is also what you are attempting to do.
The sealed classes have the following unique features:
A sealed class can have subclasses, but all of them must be declared in the same file as the sealed class itself.
A sealed class is abstract by itself, it cannot be instantiated directly and can have abstract members.
Sealed classes are not allowed to have non-private constructors (their constructors are private by default).
Classes that extend subclasses of a sealed class (indirect inheritors) can be placed anywhere, not necessarily in the same file.
For more info, have a read at https://www.ericdecanini.com/2019/10/14/kotlins-sealed-class-enums-on-steroids/
Hopefully, this will help others new to Kotlin who are also encountering this issue.
Class constructors are package-private by default. Just add the public keyword before declaring the constructor.
By default constructor is public so need to remove internal keyword.
How do you allow people to implement a class without letting them see or change the code?
Implementation implies Interface. You can just define an Interface in a .NET assembly (.dll) which you provide to said people. There is no implementation in an interface. No code to change other than the property and method names and types.
Public Interface IFoo
Sub Bar()
End Interface
If this code is given to people in a code file, they can change the definition of the Interface.
There is also an abstract class, in VB it is called MustInherit. Other classes can inherit or derive from it (not implement, as your question said, but you might mean this).
Public MustInherit Class Foo
Sub Bar()
Console.WriteLine("Bar")
End Sub
MustOverride Sub Barr()
End Class
If you give this to people in a code file, they can change what happens in Bar() or really do anything they want to it. Again, putting this in a .NET assembly and giving the .dll to people would prevent this from happening. In most cases this is enough to keep people from changing your code.
You can do this by making the output type of your project a class library. This will compile into a .dll file.
Build the project. In the output directory will be a .dll file (if compilation was a success).
You, or other people, could then add a reference to the .dll file created by your class library, in a new project. You could declare a class which implements the public interface defined in it
Public Class MyIFoo
Implements WindowsApplication1.IFoo
Public Sub Bar() Implements WindowsApplication1.IFoo.Bar
End Sub
End Class
And/or make a class which inherits the abstract class you made in the .dll
Public Class MyFoo
Inherits WindowsApplication1.Foo
Public Overrides Sub Barr()
End Sub
End Class
In your new project, you cannot see or change the code of the Interface IFoo or abstract class Foo.
You should use the private keyword to prevent a client of the class from accessing or modifying fields directly. You expose the methods and properties you want the client to access via the public or protected keyword (for inheritance).
I was reading about private constructor and found a few points that I couldn't understand. It said, if you declare a constructor as private:
That class cannot be explicitly instantiated from another class
That class cannot be inherited
Should be used in classes containing only static utility methods
My first question: Point 2 says the class cannot be inherited. Well, if you declare a class private then it would still satisfy this property. Is it because, if a class is private, it can still be explicitly instantiated from outside by another class?
My second question: I don't understand point 3. If I have a helper class which is full of static methods, I would never have to instantiate that class to use the methods. So, what is the purpose of a constructor in that class which you are never going to instantiate?
Answer for Java
Question 1 You're confusing a private class, with a class that has a private constructor. Private constructors are used mainly for static classes that are not meant to be instatiated (i.e. they just have a bunch of static methods on them).
Question 2 Exactly there is no need for a constructor so you have to explicitly create a private constructor so that it does not get a default constructer that the JVM will provide if none is defined
An empty class with no methods defined will always be given a no argument constructor by the JVM by default
I take java and c++ as an examples (not the best OO languages known, but very popular) - since you are not defining which languge do you mean.
Ad.2. In these languages you must either call superclass constructor explicitly or it is implicitly called for you. From a subclass you cannot call private methods (only public and protected) - this rule applies to constructors as well. This means if the class has only private constructors, there is no way to call one in subclass constructor. So you cannot subclass such class.
Ad. 3. It is just to avoid confusion - since this class is only a container for utility methods, there is no point in instantiating it. This way you can enforce this rule at compile time.
I find myself creating a significant number of wrapper classes, purely because I want to mock out the behaviour of
Classes that don't lend themselves well to the RhinoMocks isolation model (for instance like DirectoryInfo or WindowsIdentity)
Native Win API methods (I normally collect all the methods I need into a single class and wrap the native calls as a class method)
I then find myself appending the class that is wrapped with a 'W' (to indicate that it's a wrapper) and so I end up with DirectoryInfoW (as opposed to DirectoryInfoWrapper which seems rather verbose). Similarly, I end up with wrapped native methods called NativeMethods.DuplicateTokenW.
What would be a good rule of thumb to follow when naming wrapper classes?
Naming conventions are whatever works for the team that you're working with. As long as everyone's ok with a particular convention, then it's ok.
I tend to prefer the more verbose version though, i.e. DirectoryInfoWrapper, rather than having a single letter that doesn't explain anything to anyone who's not familiar with the code. But that's just me.
I'll agree with aberrant80 , if everyone agrees with the convention you are using, then it'll work.
I personally prefer using names that are shorter and descriptive to the class's purpose. At least at the interface level. If you're using a mock framework, then IDirectory or IDirectoryInfo would be a decent set of names, while DirectoryInfoW or DirectoryInfoWrapper would be an interface implementer.
A better example might be wrapping an HttpRequest; define an IRequest to state 'this is what is important to my application', then Request, HttpRequestWrapper, Request, etc would be implementers.
So, to summarize, try and use descriptive, non-overly-verbose interface names.
Just as a side note, I found a more aesthetically pleasing (well, to me) way of wrapping native method calls:
public class NativeMethods
{
// made virtual so that it can be mocked - I don't really want
// an interface for this class!
public virtual bool RevertToSelf()
{
return WinApi.RevertToSelf();
}
...
private static class WinApi
{
[DllImport("advapi32.dll")]
public static extern bool RevertToSelf();
...
}
}
i.e. avoid name collision by encapsulating native method calls in a private nested class.
No 'good' solution to the wrapper class naming issue though, I'd probably go with aberrant80's suggestion and explicitly call my wrappers wrappers.
If you are using C++, you can use namespaces and then just re-use the same class name. For example:
namespace WrapperNamespace
{
class MyClass {...};
}
namespace InternalNamespace
{
class MyClass {...};
}