How to Create an Object of an Interface

How can we create object of interface in java?

With that syntax, you create an anonymous class, which is perfectly legal.

Internally, anonymous classes are compiled to a class of their own, called EnclosingClass$n where the enclosing class' name precedes the $ sign. and n increases for each additional anonymous class. This means that the following class is being created:

class Interface$1 implements check {
public void message() {
System.out.println("Method defined in the interface");
}
}

Then, the code in main compiles to internally use the newly-defined anonymous class:

check t = new Interface$1();
t.message();

How can we create an object of interface with Spring to autowire it but it's wrong to create the same object in java?

We cannot create an object of an interface in Java.

Interfaces in java can be defined as a full abstract class with fields (public , static & final) and empty methods (public and abstract). Although from Java 8 we can define default methods which can have their body and in Java 9 we can even have private methods.

But the point is , in an interface we don't have fields present at the object level since all of them are by-default static. Hence its not logical or meaningful to have a constructor

which means --> No object creation.

When we use spring @Autowired for initializing an interface , spring does not actually create an object of that interface rather an object of its child class is created.

In this case since only one child class exists for interface ABC which is class ABCImpl.

So Spring will do something like this when we use @Autowired :

ABC abc = new AbcImpl(); 

The abc is just a reference variable storing the object of child class (one of interface ABC implementation).
Things get interesting if we have multiple child classes and we use @Autowired. Then spring resolves this ambiguity by first checking the type and then name. If both are same then we can go for something like @Qualifier to add some additional tag to differentiate it.

Is it possible to create an object of an interface in java?

You never create an instance of just the interface. You can have a field/parameter/local variable of that interface type, but that's fine - the value that is assigned to such a variable will always be either null or a reference to an instance of some concrete implementation of the interface. The point is that code which deals only with the interface shouldn't need to care what the implementation is.

A good example is Collections.shuffle(List) - I can provide that any list implementation, and it will only use the methods declared in the interface. The actual object will always be an instance of some concrete implementation, but the shuffle method doesn't need to know or care.

What happens when we create an object of interface?

Actually you cannot create an instance of an interface.

You create an instance of some class, implementing the interface. Actually there can be dozens of classes, implementing one interface. So when you use a variable of interface type, the only thing you are guaranteed is that the object, which is in fact referenced by the variable, implements the interface and you can use any of the interface methods, properties, etc.

interface IFoo
{
void DoFoo();
}

class Foo1: IFoo
{
public DoFoo()
{
//one implementation
}
}

class Foo2: IFoo
{
public DoFoo()
{
//the other implementation
}
}

IFoo tmp = new Foo1();
tmp = new Foo2();

You may see a deep explanation in SO: Using Interface variables

How can I create an object based on an interface file definition in TypeScript?

If you are creating the "modal" variable elsewhere, and want to tell TypeScript it will all be done, you would use:

declare const modal: IModal;

If you want to create a variable that will actually be an instance of IModal in TypeScript you will need to define it fully.

const modal: IModal = {
content: '',
form: '',
href: '',
$form: null,
$message: null,
$modal: null,
$submits: null
};

Or lie, with a type assertion, but you'll lost type safety as you will now get undefined in unexpected places, and possibly runtime errors, when accessing modal.content and so on (properties that the contract says will be there).

const modal = {} as IModal;

Example Class

class Modal implements IModal {
content: string;
form: string;
href: string;
$form: JQuery;
$message: JQuery;
$modal: JQuery;
$submits: JQuery;
}

const modal = new Modal();

You may think "hey that's really a duplication of the interface" - and you are correct. If the Modal class is the only implementation of the IModal interface you may want to delete the interface altogether and use...

const modal: Modal = new Modal();

Rather than

const modal: IModal = new Modal();

When I create an object from a class that extends a specific interface, can I use this object in the place where I use this interface?

Yes, you can do that.
Interfaces are a way in Java to avoid multiple inheritance, i.e. declaring a class Foo to extend a class Bar and implement an interface Bar means that we have a "is a" relationship between Foo and Bar/Baz. So, "Foo is a Bar" and "Foo is a Baz" are true or in your case Final is a One. So, if a type is a subtype (or in Java implements an interface) the subtype can be used in place of the type or the interface (see the Liskov substitution principle).

When you declare a method m1(One one), you require the first parameter to be of type One and as Final is a One this is obviously true.

Please note, that even though you pass in an object of type Final the method only "sees" the interface part of the object without casting it.

Creating object with reference to Interface

But in my code is displaying displayName()method undefined.

Right, because displayName is not defined in the Printable interface. You can only access the methods defined on the interface through a variable declared as having that interface, even if the concrete class has additional methods. That's why you can call sysout, but not displayName.

The reason for this is more apparent if you consider an example like this:

class Bar {
public static void foo(Printable p) {
p.sysout();
p.displayName();
}
}

class Test {
public static final void main(String[] args) {
Bar.foo(new Parent());
}
}

The code in foo must not rely on anything other than what is featured in the Printable interface, as we have no idea at compile-time what the concrete class may be.

The point of interfaces is to define the characteristics that are available to the code using only an interface reference, without regard to the concrete class being used.



Related Topics



Leave a reply



Submit