N
N
nevro2015-08-31 14:55:34
Java
nevro, 2015-08-31 14:55:34

Casting to an interface type: what's the point?

If the method has an input parameter of an interface type, for example, ISome, then we can pass an object of any type to it, the main thing is that the object implements this interface.
void method(ISome obj){.....}
Convenient - uniform work with objects of any type. But I don’t understand the mechanics: after casting to obj, we now have a link to the block of methods (more precisely, signatures) ISome, and these are only signatures without implementation, and besides, there is no access to the fields of the obj object! But it turns out I'm wrong. Explain the correct mechanics.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alex Chistyakov, 2015-08-31
@alexclear

> But I don't understand the mechanics: after casting to obj, we now have a link to the block of methods (more precisely, signatures) of ISome, and these are only signatures without implementation
. Where did the implementation go? Originally she was.
Type casting does not change the internal structure of the object, as well as meta-information about it.
What type he was originally - this remained. You can reflexively check.
> and besides, there is no access to the fields of the obj object!
No access from where exactly?
> But it turns out I'm wrong.
I do not fully understand what exactly you imagined wrong, but you apparently presented something wrong. The object remains the same after type conversion.

S
Stanislav Makarov, 2015-08-31
@Nipheris

You don't understand the mechanics of polymorphism and virtual calls.
The interface is the window through which you look at the real object. The mechanism of late binding, namely virtual functions, allows you to have a reference of type ISome, which, nevertheless, refers to a REAL object with IMPLEMENTATIONS of methods of the ISome interface, and call these methods through the interface.
Note that the compiler won't let you write new ISome; (you can try), because. interface cannot be instantiated. This is just an agreement on interaction, and in order for the interaction to occur, it is necessary that any real objects obey this agreement.
When you cast a reference type from SomeObject to ISome, the reference itself DOES NOT CHANGE (at least you won't see it), the new type variable refers to the SAME object, not a "method block". References always point to specific objects, it is impossible to refer to a "block of methods" that has no implementation, the language does not work that way. The essence of interfaces is that they combine the idea of ​​late binding and strong typing at the same time. By using an ISome reference instead of SomeObject, it's like you're looking at the same object through a different "window" and seeing it has a different set of methods. This is the most important stage in the abstraction procedure - to find similar features in objects, and implement a number of algorithms based on this. As you yourself have already noticed - it is uniform to work with objects of different types.

I
IceJOKER, 2015-08-31
@IceJOKER

One of my first questions on the Toaster is OOP - How to understand Interfaces in java?

L
LeEnot, 2015-08-31
@LeEnot

Google "dynamic polymorphism".
In short: when calling an interface method, the java machine checks the type of the object actually used and calls its method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question