Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Implementing a method that returns a Class

There is an interface with a method that returns a Class, like so.

public interface MyInterface {
    
    public Class<? extends Object> returnsSomething ();
}

I have to create a class which implements the interface, like so.

public class MyClass implements MyInterface {
    
    public Class<? extends Object> returnsSomething () {
        
        return Object; // This is currently an error.
    }
}

The return line in the implementation of returnsSomething in MyClass is incorrect. The IDE hints "cannot find symbol Object".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What correction do I need to apply in returnSomething‘s body to compile successfully?

>Solution :

Object is just the name of the class.

Object.class is the instance of the Class<Object> class that represents the Object class. See Class.

So you need:

return Object.class;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading