Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Interfaces (http://www.programmingforums.org/showthread.php?t=14888)

namsu Jan 7th, 2008 5:34 PM

Interfaces
 
Hi, I am very confused with interfaces. I understand how to use them and when to use them. I understand the difference between the abstract class and interfaces. Can someone explain to me the real importance of using an interface, because according to me it seems as if we declare an interface so all other classes can use the same name for a particular method. Why don't people just declare the same name in each class rather than having to go through something just to use the name?

Sane Jan 7th, 2008 6:12 PM

Re: Interfaces
 
I am even more confused over how you're managing to skip from learning about "For Loops" and "Conditional Operators", to learning about Object Oriented Programming with Interfaces in Java.

Either you're going too quickly, or you're trying to understand things you don't yet need to. Regardless, maybe you need to spend more time on the square before this one, to gain further insight into programming before you ask these questions. I can promise the answers will make more sense that way, and you may not even need to ask this question when you get to that point.

namsu Jan 7th, 2008 6:15 PM

Re: Interfaces
 
Hi Sane I appreciate your help. I don't think I am going to fast, I just need an explanation, do you mind giving me one.

titaniumdecoy Jan 7th, 2008 6:34 PM

Re: Interfaces
 
Quote:

Originally Posted by namsu (Post 139306)
Why don't people just declare the same name in each class rather than having to go through something just to use the name?

For the same reason you need to declare the relevant variables and methods in the base class when using polymorphism. Perhaps an example will help clear things up:

:

  1. // FirstClass and SecondClass both implement MyInterface
  2. MyInterface obj1 = new FirstClass();
  3. MyInterface obj2 = new SecondClass();
  4.  
  5. // MyInterface defines the doSomething() instance method
  6. System.out.println(obj1.doSomething());
  7. System.out.println(obj2.doSomething());

It does not matter what type of object obj1 and obj2 are; however, since they implement MyInterface you know that they both implement the doSomething method.

In Java, you can implement any number of interfaces but extend only a single class, which is another reason interfaces are used (other languages such as C++ allow for multiple inheritance).

To put it simply, you should only extend a class when the class you are creating has an "is a" relationship with the class you are extending; for example, a GraduateStudent "is a" Student. In contrast, a Student might be able to fly, and would implement the Flyable interface. (Clearly, the Student does not have an "is a" relationship with his/her ability to fly.)

null_ptr0 Jan 7th, 2008 6:59 PM

Re: Interfaces
 
Used like callbacks and function pointer replacements.

titaniumdecoy Jan 7th, 2008 9:07 PM

Re: Interfaces
 
Quote:

Originally Posted by null_ptr0 (Post 139311)
Used like callbacks and function pointer replacements.

Care to explain? :icon_confused:

null_ptr0 Jan 7th, 2008 9:29 PM

Re: Interfaces
 
Quote:

Originally Posted by titaniumdecoy (Post 139314)
Care to explain? :icon_confused:

When you have a callback, in c++ event handling, you pass a function ptr so it calls it each time when an event occurs.
In java, there are no function ptrs.
So, we use interfaces with a method like eventCallback(Event e) that each sub/class has to implement and just call that method.


All times are GMT -5. The time now is 3:27 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC