Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-14-2008, 10:45 PM
Member
 
Join Date: Nov 2007
Posts: 45
sireesha is on a distinguished road
interfaces..
Hi all,
I saw this code in a book.
Code:
interface Lion extends Animal {} Lion king = ...;
In the above code Lion is an interface which extends Animal interface.
But in the second line ,
Is King is the object of type Lion ?
Can we create Objects for interfaces ?
I am very confused about it.
Please tell me...
Thankq very much.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-15-2008, 12:39 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Interfaces
Hello sireesha.

An interface is like a list of methods. Those classes that implement that interface must define those methods. An interface on it own is not an object because it is not a subclass of the Object class. A nice feature about interfaces is that you can give functionality to classes that are not related but have similar behavior. Lets say we have the interface Movable:
Code:
public interface Moveble{ public abstract void move(); }
Note that method definitions of interfaces are empty. Now, lets say we have a class Car and a class Animal that implements the Moveble interface. They are not related but they can both move.
So now we have
Code:
public class Car implements Moveble { private int position = 0; public Car(){ } public void move(){ position += 5; } }
and
Code:
public class Animal implements Moveble { private double position = 0; public Animal (){ } public void move(){ position += 2.5; } }
Note that the classes are very different but have the move() method in common. Now to answer this:
Quote:
Originally Posted by sireesha
Can we create Objects for interfaces ?
No, but you can create a variable for classes that implement that interface and use the methods of that interface. Any object that is an instance of a class that implements that interface can be assigned to that variable:
Code:
Moveble myMovebleObject = new Animal(); myMovebleObject.move();
or
Code:
Moveble myMovebleObject = new Car(); myMovebleObject.move();
I hope this cleared things up.
This code has been checked by hand. Please correct me if I made a mistake.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-15-2008, 10:59 PM
Member
 
Join Date: Nov 2007
Posts: 45
sireesha is on a distinguished road
Thanks for your explanation,
i have one more doubt,
what is the difference between
Code:
Moveble myMovebleObject = new Animal(); myMovebleObject.move();
and

Code:
Animal a=new Animal(); a.move();
In both cases we are calling move method of Animal class.
then what is the difference between them ?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-16-2008, 12:51 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 260
roots is on a distinguished road
I have always felt learning language comes with this baggage that "You need to understand all to start with". I expect this question be raised again and again, for now Dr. Dobb's | Design by Interface | February 1, 1999 , Keywords: Design By Contract, Loose Coupling might give you some hints in understanding the difference between your codes above.
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-16-2008, 11:00 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Quote:
Originally Posted by sireesha View Post
Thanks for your explanation,
i have one more doubt,
what is the difference between
Code:
Moveble myMovebleObject = new Animal(); myMovebleObject.move();
and

Code:
Animal a=new Animal(); a.move();
In both cases we are calling move method of Animal class.
then what is the difference between them ?
Hello

The difference is that the variable, a, can only keep instances of the Animal class, including all its subclasses, while the variable, myMovebleObject, can only keep classes that implement the Moveble interface, including all the subclasses of those classes.

Hope this clears things up.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-16-2008, 06:52 PM
Member
 
Join Date: Nov 2007
Posts: 45
sireesha is on a distinguished road
thankq very much,now i got it.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interfaces Kavana Krishnappa New To Java 7 12-11-2007 05:28 PM
Using interfaces with Delegation Java Tip Java Tips 0 12-06-2007 02:49 PM
Using Interfaces JavaForums Java Blogs 0 11-08-2007 02:30 PM
Help, someone clear up Interfaces for me mathias New To Java 1 08-06-2007 03:26 AM
Interfaces imran_khan New To Java 5 07-30-2007 09:11 AM


All times are GMT +3. The time now is 01:38 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org