Interfaces, classes, new keyword, behind the scenes

284 views

[ad_1]

Hello, I’m having a hard time to understand why it works, I know it works and how it works, but I don’t really grasp the ‘behind the scenes.’

Let’s have a code for the subject’s sake:

`interface X {void wassup();}`

`class Y implements X {public Y() {System.out.println(“y”);}`

`public void wassup() {System.out.println(“wassup”);}}`

`class HelloWorld {`

`public static void main(String[] args) {`

`returnInterface();}`

`public static X returnInterface() {System.out.println(“Hello World”);return new Y();}}`

How and why can we return a class even though it expects an interface?

`X test = new Y();`

`Y test = new Y();`

What’s the difference? One is type of interface, one is type of class, is that all?

For the `X test = new Y();` , is ‘test’ variable a type of interface? Just like string, int or float?

So ‘test’ variable has all the features of X class has, like its variables, methods?

And does ‘test’ variable execute its features/codes/methods in the context of Y?

Does ‘new’ keyword goes and allocate memory in the ram, amount of the variable and methods contains in Y class or X interface?

In: Engineering
[ad_2]

**How and why can we return a class even though it expects an interface?**

The compiler will cast the object of type Y to the interface X. This works because you have said that class Y implements interface X. The compiler is smart enough to cast the type even if you did not ask for it.

**X test = new Y();**

**Y test = new Y();**

**What’s the difference? One is type of interface, one is type of class, is that all?**

In your first example the variable test is of type X while in the second one it is of type Y. If you had implemented additional public meathods in the class Y this would be available to anyone using the variable in the second case but not the first. That is basically the only difference.

**For the** **X test = new Y();** **, is ‘test’ variable a type of interface? Just like string, int or float?**

Yes, the variable is of type X, which is the interface.

**So ‘test’ variable has all the features of X class has, like its variables, methods?**

Exactly. All public meathods and variables in the interface is available. However when the variable is of type X you do not have access to the public methods and varables in the class Y.

**And does ‘test’ variable execute its features/codes/methods in the context of Y?**

Yes. When you call a public meathod it will get passed to the object that the varible points to and this is of type Y. So even though the variable is of type X the object within it is of type Y which is what determines what code will run.

**Does ‘new’ keyword goes and allocate memory in the ram, amount of the variable and methods contains in Y class or X interface?**

The content of the memory will be an object of class Y. There is not actually any objects of type X because this is an interface. Only variables can have type X and not the actual objects that the variables point to.

Interface is like…. a specification. There’s no content inside it.

Class Y is an implementation of that interface, which means it has to follow the specification.
You could for example have a class Z, which is also implementation of interface X. It would also have method wassup(), but you could make it return something else.

>How and why can we return a class even though it expects an interface?

The class is an implementation of that interface, so it’s valid. Actually, returning interface always returns an implementation of it (a class)

>What’s the difference? One is type of interface, one is type of class, is that all?

Yes, if you were to debug the code, you’d see both of the objects are class Y.

X test = new Y();

this creates a new object of class Y

I could go on but I will finish here: You will maybe look at this all and think –

>wait a minute – interfaces are useless, I can just use classes without ever using interfaces.

And yes, you technically can. But interfaces are far from useless. So just get used to them.

Think of a bureaucracy.

An interface is the standard way of describing how you talk to that bureaucracy – there is a helpline 1800 number, an office with a receptionist and a mailbox.

A class is the entire type of bureaucracy with all its inner workings. This is a motor vehicle licensing bureaucracy which has mechanisms for paying fines, renewing licenses, etcetera. There are procedures, forms, employee training materials and so on. .

A variable is an instance of a class – this is a he California department of motor vehicles. That one is the New York department of motor vehicles.