1

As per multiple articles in Java Interface and Class are completely different. Let me write an Interface.

package com.main.service;

public interface SomeService{
   public void someMethod();
}

But why below code is allowed in Java?

 com.main.service.SomeService.class;

I am using this code to get beans from Spring application context like below:-

 SomeService someservice = applicationContext
            .getBean(com.main.service.SomeService.class);
4
  • 1
    Interfaces and classes both are compiled to Java bytecode classes. Dependency Injection Containers allows you to pass an interface and it finds an implementation to be provided. Commented Nov 17, 2017 at 8:17
  • 1
    applicationContext .getBean returns you an implementation of the interface. Commented Nov 17, 2017 at 8:17
  • 1
    .class is a built-in language feature (a class literal) that looks like a public static final field Commented Nov 17, 2017 at 8:20
  • @aKilleR your comment is really to the point. But please explain more about .class feature and how it is working with Interface. Commented Nov 17, 2017 at 8:22

3 Answers 3

2

The .class syntax at the end of a type (com.main.service.SomeService.class; in this case) references the class literal, which is an object of type Class. You can use this on any Java type, be it a concrete class, abstract class or an interface.

As per the Javadoc from the link above:

Instances of the class Class represent classes and interfaces in a running Java application

This may be confusing if you're new, and are used to having the distinction between class and interface drummed into you, but simply speaking it's how the underlying system works (all classes and interfaces are compiled to bytecode class files.)

You commonly see the syntax used in dependency injection (or other uses where you need to pass the "type" of something around), as it's the easiest way of doing so.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I will give it a read and post my questions if any. Thank you.
2

A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class.

ClassLiteral:
TypeName {[ ]} . class
NumericType {[ ]} . class
boolean {[ ]} . class
void . class

The type of C.class, where C is the name of a class, interface, or array type, is Class.

A class literal evaluates to the Class object for the named type (or for void) as defined by the defining class loader of the class of the current instance.

Comments

0

You should look at the Java docs of the method that you are using:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/BeanFactory.html#getBean-java.lang.Class-

Importantly the javadoc states:

Return the bean instance that uniquely matches the given object type, if any.

NoUniqueBeanDefinitionException - if more than one bean of the given type was found

So if you have more that one beans extending the interface you will get exception.

1 Comment

No offence. I am not asking anything about spring. I am asking about interface.

Your Answer

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.