Interfaces, classes, new keyword, behind the scenes

595 views

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

3 Answers

Anonymous 0 Comments

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.

You are viewing 1 out of 3 answers, click here to view all answers.