Results 1 to 6 of 6

Thread: interfaces..

  1. #1
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Question interfaces..

    Hi all,
    I saw this code in a book.
    Java 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.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default 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:
    Java 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
    Java Code:
    public class Car implements Moveble {
        private int position = 0;
        public Car(){
        }
        public void move(){
            position += 5;
        }
    }
    and
    Java 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:
    Java Code:
    Moveble myMovebleObject = new Animal();
    myMovebleObject.move();
    or
    Java 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.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    Thanks for your explanation,
    i have one more doubt,
    what is the difference between
    Java Code:
    Moveble myMovebleObject = new Animal();
    myMovebleObject.move();
    and

    Java 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 ?

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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.

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Quote Originally Posted by sireesha View Post
    Thanks for your explanation,
    i have one more doubt,
    what is the difference between
    Java Code:
    Moveble myMovebleObject = new Animal();
    myMovebleObject.move();
    and

    Java 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. :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  6. #6
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

Similar Threads

  1. Interfaces
    By Kavana Krishnappa in forum New To Java
    Replies: 7
    Last Post: 12-11-2007, 04:28 PM
  2. Using interfaces with Delegation
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 01:49 PM
  3. Help, someone clear up Interfaces for me
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 02:26 AM
  4. Interfaces
    By imran_khan in forum New To Java
    Replies: 5
    Last Post: 07-30-2007, 08:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •