2
interface abc
{
 ...
}

Suppose I have methods which have a return type of Object and interface type abc.

class xyz
 {
 Object meth ()
 {
 ... 
 }
 abc meth2()
 {
 ...
 }
 public static void main (String args[])
 {
 xyz x = new xyz() ;
 Object a = x.meth() ;
 abc c = x.meth2() ;
 }
}

I don't want to implement the interface abc . The reference variable c is of type abc but what is the object type for c ? We cannot create objects of an interface , so what it could possibly be ?

For example : sup s = new sub() ; // where sub is subclass of sup . Here sup is the return type and sub is the object type .

anacron
6,7512 gold badges30 silver badges32 bronze badges
asked Jun 26, 2017 at 4:41
9
  • How do you plan to implement the method meth2 without creating an object of abc unless you return null? In the latter case, the value of c will be null. Commented Jun 26, 2017 at 4:44
  • 2
    Please stick to Java naming conventions (like Abc for a type) and ident your code Commented Jun 26, 2017 at 4:45
  • If you want an object of type abc, you must define a class that implements it (although it could be an anonymous class). I'm not clear on how you thought you could do anything useful without implementing the interface. Commented Jun 26, 2017 at 4:51
  • I am facing a similar trouble to understand the getAnnotations() method which has a return type of Annotation[] and Annotation is an interface . Commented Jun 26, 2017 at 4:52
  • There's a class somewhere that implements Annotation (maybe more than one such class). It might be a private class buried somewhere in the Java runtime library. Commented Jun 26, 2017 at 4:55

3 Answers 3

1

The underlying type of c can be anything that implements abc. The actual type depends on your implementation of meth2.

"But in my code, nothing implements abc though!" you said. If nothing implements abc, meth2 cannot be implemented. It can never return a correct value because it can't create a type that is compatible with abc, if no types implement abc.

However, in meth2, you can create what is called an "anonymous class":

return new abc() {
 // all the methods required by abc will be put here...
};

During compilation, this anonymous class will be turned into an inner class of the enclosing class that implements abc, and it will have the fully qualified name of something like yourpackage.EnclosingClass1ドル

answered Jun 26, 2017 at 6:41
Sign up to request clarification or add additional context in comments.

Comments

0

In order to answer your question you must first think about the possible ways to write the meth2(). You must need to implement the ABC interface somewhere. You can do something like this:

ABC meth2() {
 return new ABC(){
 // Implement the methods that are defined in ABC interface
 };
}
answered Jun 26, 2017 at 4:56

Comments

0

The method meth2 can only return null or an instance of a class implementing the interface abc.

answered Jun 26, 2017 at 5:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.