Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a interface method return an object of type of the class which implemented it

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.

like image 882
Mauro Valvano Avatar asked Apr 12 '14 14:04

Mauro Valvano


People also ask

How do I return an interface object in C#?

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 .

When an interface method is implemented in a class it must be declared as?

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.

Can we return object of interface in Java?

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.

Can we create an object of interface class?

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.


3 Answers

What you want to do here won't work for two reasons:

  1. Interfaces can't have static members
  2. Interfaces need to specify the return types of their methods. An interface shouldn't know the types of all the members implementing it, that defeats the point and in many cases would be unachievable.

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
    }
}
like image 60
Ben Aaronson Avatar answered Oct 25 '22 23:10

Ben Aaronson


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 ();
        }
    }
}
like image 2
Erik Eidt Avatar answered Oct 25 '22 22:10

Erik Eidt


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.

like image 1
Sriram Sakthivel Avatar answered Oct 25 '22 23:10

Sriram Sakthivel