Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 7th, 2008, 5:34 PM   #1
namsu
Newbie
 
Join Date: Jan 2008
Posts: 15
Rep Power: 0 namsu is on a distinguished road
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?
namsu is offline   Reply With Quote
Old Jan 7th, 2008, 6:12 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Jan 7th, 2008, 6:15 PM   #3
namsu
Newbie
 
Join Date: Jan 2008
Posts: 15
Rep Power: 0 namsu is on a distinguished road
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.
namsu is offline   Reply With Quote
Old Jan 7th, 2008, 6:34 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Interfaces

Quote:
Originally Posted by namsu View Post
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:

Java Syntax (Toggle Plain Text)
  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.)

Last edited by titaniumdecoy; Jan 7th, 2008 at 6:45 PM.
titaniumdecoy is offline   Reply With Quote
Old Jan 7th, 2008, 6:59 PM   #5
null_ptr0
12 years old
 
Join Date: Nov 2007
Posts: 94
Rep Power: 1 null_ptr0 is on a distinguished road
Re: Interfaces

Used like callbacks and function pointer replacements.
null_ptr0 is offline   Reply With Quote
Old Jan 7th, 2008, 9:07 PM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Interfaces

Quote:
Originally Posted by null_ptr0 View Post
Used like callbacks and function pointer replacements.
Care to explain?
titaniumdecoy is offline   Reply With Quote
Old Jan 7th, 2008, 9:29 PM   #7
null_ptr0
12 years old
 
Join Date: Nov 2007
Posts: 94
Rep Power: 1 null_ptr0 is on a distinguished road
Re: Interfaces

Quote:
Originally Posted by titaniumdecoy View Post
Care to explain?
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.
null_ptr0 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming Interfaces surf3rb0y Visual Basic 1 May 20th, 2007 11:11 AM
interfaces paulchwd C# 0 Feb 18th, 2007 1:45 PM
Java-style interfaces in C++ gizzy C++ 24 Feb 19th, 2006 1:33 AM
Aren't interfaces useless? Simon Gray C# 15 Sep 1st, 2005 4:40 PM
Graphical User Interfaces Clotters Other Web Development Languages 11 Jul 18th, 2005 4:20 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:25 AM.

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