I have searched for an answer to accomplish this, but I haven't found anything: I want an interface's method to return an object of the type of the class which implemented it. For example:
interface InterfaceA {
public static returnValue getObjectFromDatabase(); //What do i need to put as returnValue?
}
Then, if I have two classes (for example, ClassA and ClassB) that implement it, I would like to have:
ClassA obj1 = ClassA.getObjectFromDatabase(); //return object of class ClassA
ClassB obj2 = ClassB.getObjectFromDatabase(); //return object of class ClassB
Thank you, in advance.
It's because an interface is a "Type" description like an int, or class, but an interface can have multiple implementation of it's type. Thus when you are returning the "interface" you are actually returning the implementation of said interface. if you do a "IExample example = new ExampleImpl();" adn then "example .
To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.
Yes, you can. If you implement an interface and provide body to its methods from a class. You can hold object of the that class using the reference variable of the interface i.e. cast an object reference to an interface reference.
Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "Animal" object in the MyMainClass) Interface methods do not have a body - the body is provided by the "implement" class. On implementation of an interface, you must override all of its methods.
What you want to do here won't work for two reasons:
Moreover, if you did manage to do this, it still wouldn't be good design, because it violates the single responsibility principle. You can find plenty of information on this by googling it or looking around this site, but the idea- as indicated by the name- is that a class should only have a single purpose which it is responsible for.
So imagine that your class was, for example, an Employee
class. That class has a pretty clear responsibility, it should be responsible for holding information and functionality related to an Employee
in a company. It might have members like FirstName
, GivePromotion()
, etc. So it'd be strange to now make this class also take responsibility for its own database access.
So how this is achieved would be with another class which is responsible for retrieving objects from the database. One common design pattern for this is the repository pattern. You'll also probably want to take advantage of generics. So you repository interface might look like:
public interface IRepository<T>
{
T GetFromDatabase()
}
Which you can then implement with a generic repository:
public class Repository<T> : IRepository<T>
{
T GetFromDatabase()
{
//Your actual code for database retrieval goes here
}
}
Or, if the database retrieval code is very different for some or all classes, you can implement with a specific repository:
public class EmployeeRepository : IRepository<Employee>
{
Employee GetFromDatabase()
{
//Your actual code for database retrieval goes here
}
}
One approach is to use generics:
class Program
{
interface MyInterface<SomeType>
{
SomeType getObjectFromDatabase ();
}
class A : MyInterface<A> { public A getObjectFromDatabase () { return new A (); } }
class B : MyInterface<B> { public B getObjectFromDatabase () { return new B (); } }
class Program2
{
static void Main ()
{
A a1, a2;
a1 = new A ();
a2 = a1.getObjectFromDatabase ();
B b1, b2;
b1 = new B ();
b2 = b1.getObjectFromDatabase ();
}
}
}
I want an interface's method to return an object of the type of the class which > implemented it
You seem to miss the point of interfaces: an Interface shouldn't have any knowledge about its implementers. Interface exposes contracts and that's it.
Also, from your example, I can see that you are trying to create a static
method but interfaces and static are far away from each other. Interfaces are tied with instances, not the type.
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