Object initialization using InterfaceType
java
Solution
You're just calling a static method in the `ArgumentParsers` class. You should look at the implementation of `ArgumentParsers.newArgumentParser` to see which implementation of `ArgumentParser` is used.
This isn't peculiar to Java, either. For example, methods like `File.OpenRead` and `XmlReader.Create` are both declared with return types which are abstract classes - the same thing would work with interfaces.
For example, if you wanted to translate the Java code to C#, this would be valid:
public interface IArgumentParser
{
Configuration Parse(string[] arguments);
}
public static class ArgumentParsers
{
public static IArgumentParser(string file)
{
return new FileArgumentParser(file);
}
}
internal class FileArgumentParser : IArgumentParser
{
private readonly string file;
internal FileArgumentParser(string file)
{
this.file = file;
}
public Configuration Parse(string[] arguments)
{
// Presumably use the file somewhere...
}
}
Problem
Iam new to java programming language.I was programming in c# for last two years.When i went through the java program i find a code as follows. ``` ArgumentParser parser = ArgumentParsers.newArgumentParser("text"); ``` where ArgumentParser is an InterfaceType and ArgumentParsers is a class.But i couldn't find any implementation for ArgumentParser in ArgumentParsers.How can we create an object of an interfacetype by initilizing the object with a class which doesnot implement that interface. I don't know it is possible in c#.Please help me explain this Thanks