• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
i am a beginner in java programming, but have a knowledge of programming in C/C++. Thus i am studying from 'A complete reference : Herbert Schildt'.
I just want to ask why are the interfaces used in a java program?
Does it help the program run better?

Another question:
what is ''accessing implementations through interface references''?
Can you explain me this LOC :

interface_variable=class_object;

where interface_variable is an variable of the created interface and class_object is an object of a class which implements above referred interface.
Thank you.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to know about coming from C++ is that Java has no multi-class inheritance. One reason for Interfaces is so you can implement many one them on one class.

Another reason for them is they declare an API. You can have many classes that share an API but work differently. One example that comes to mind is a DAO Interface and several DAO's that implement it. The Interface guarantees that each DAO will have, say, basic CRUD (create, read, update, delete), but operate on different databases.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Accessing implementations through interface references."

An interface has only the API declared. You implement a class using an interface. Then you reference that implementation using the interface, not the implementing class.

For example, Java has an interface (abstract class?) of java.util.List. Now how do I want to implement a List. How about with arrays? So there's a java.util.ArrayList.

This is how you should access it:



Notice that "list" is type List, the interface, not the implementation. This is so if you want to some other implementation, you can with one change.

This is accessing implementations through interface references.
 
Suyash Gulati
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you knute snortum,
i very well understood your first reply but having a difficulty in understanding the latter.

The reason could be i am not fimiliar with java.util.List or ArrayList. As i am beginner, havent worked upon these terms.

If you could give another example(simpler), it will be really helpful! Thank you!
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you have an interface of Plane. You can start the engine, throttle up and down, turn left, right, up, down. All of this is in the interface but the is not implementation. Is this a small Cessna or a Boeing 777? We don't know. I want to operate the plane, but I don't care what kind of plane it is as long as it flies. So I say:



Now I know that plane can start its engine, steer, whatever the interface tells me. What kind of plane is it? Who cares? I know how to operate it. I don't need to go fast for now, so I say:



As long as the class Cessna implements Plane, I'm fine. I can write plane.startEngine() and plane.turnRight().

Two years later, I need a faster, bigger plane. Do I need to rewrite my code? Not if the new plane implements Plane. If so, I just change:



I know I can still write plane.startEngine() because this is what implementing Plane means. None of the rest of the code changes.

They are other reasons to use interfaces too but they all have to do with guaranteeing that you can use different objects in the same way.

 
Suyash Gulati
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so very much!
it cleared my doubt!
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suyash Gulati wrote:i am a beginner in java programming, but have a knowledge of programming in C/C++.

This is probably way too late but, if you know C++, then the question can probably be turned around:

Have you ever created a completely abstract class in C++? (ie, one where ALL its methods were virtual)

If so: why did you do it?

IMO, the "multiple inheritance" thing is a bit of a red-herring since, when I was using C++, I detested it, and all code that spawned it.

Indeed, one of the reasons I latched onto Java was that the interface keyword just made sense.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!