Results 1 to 8 of 8
  1. #1
    BoomPony is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Don't understand interfaces for inheritance

    I'm trying to get my head around interfaces and how they can be used for inheritance. If I had a class for example called Lion, which I wanted to inherit from a class, Cat, containing int numTeeth, and from a class killer, containing int numKills. Do I need a seperate class for killer and cat, as well as two interfaces, or just an the interfaces? Do interfaces interface implement the functionality of getters and setters for numTeeth and numKills or does Lion have to define their body?

    This might sound like a really stupid question but I've tried reading tutorials on multiple inheritance in Java and I'm really struggling.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Lion is a Cat, so it would make sense to have a Cat class, possibly an abstract class, that is parent to Lion, Tiger, DomesticCat, etc...

    Killer is more nebulous a concept than Cat, and many animals can be killers, and I suppose that there are Cats that are not Killers, and so I would make this an interface and have Lion implement it. All the methods declared in an interface must be implemented by code in the class that states it is implementing that interface.

    The interface is one of Java's core concepts, and same as you, I had trouble grasping it at first, but after reading about it from many sources, and after making a couple of holes in the wall with my head, it sunk in eventually as I'm sure will happen with you.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by BoomPony View Post
    I'm trying to get my head around interfaces and how they can be used for inheritance. If I had a class for example called Lion, which I wanted to inherit from a class, Cat, containing int numTeeth, and from a class killer, containing int numKills. Do I need a seperate class for killer and cat, as well as two interfaces, or just an the interfaces? Do interfaces interface implement the functionality of getters and setters for numTeeth and numKills or does Lion have to define their body?

    This might sound like a really stupid question but I've tried reading tutorials on multiple inheritance in Java and I'm really struggling.
    Think of types; e.g. an int is a type (a primitive type to be exact) and types can do certain things, they 'understand' certain things; ints understand + or - and some more. An interface is also a type and a class is too but a class is a bit more: it also implements what is says it can do while an interface doesn't implement anything at all.

    Java supports multiple inheritance of types and it support single inheritance of implementation, so an interface can 'extend' multiple other interfaces while a class can only extend one other class but classes can implement interfaces, as many as it wants and an interface can extend as many other interfaces as it wants. You can turn it into a complete mess (as quite a lot of people do) or think about your types (and implementations thereof).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    BoomPony is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    so would something like this make sense?

    Java Code:
    interface cat {
            int numTeeth;
    }
    
    interface killer {
            int numKills;
            void Kill();
    }
    
    class Lion implements cat, killer{
            int getNumTeeth(){ return numTeeth;}
            int getNumKills(){ return numKills;}
            void Kill(){ numKills++; } 
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    That could work, but for my sensibilities (and this is opinion only, not fact), I think that the link between Lion and Cat is stronger than an interface allows, that there should be an inheritance relationship between the two.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    If I had a class for example called Lion, which I wanted to inherit from a class, Cat, containing int numTeeth, and from a class killer, containing int numKills.

    In Java you can't inherit implementation from two parent types.

    You can have Lion implement multiple interfaces - but these interfaces will not contain any implementation. In other words, an interface might contain "int getNumTeeth();" which a promise saying "whatever implements this interface will provide a getTeeth() method", but it will never contain a variable like "int numTeeth;".

    It is up to whatever class implements the interface to declare and use whatever variables it needs in order to deliver on the promise the interface makes.

    The same goes for Killer if it is an interface. Something like:

    Java Code:
    interface Killer {
        int getNumKills();
        void kill();
    }
    
    class Lion implements Killer {
        private int numKills;
    
        public int getNumKills() {
            return numKills;
        }
    
        public void kill() {
            numKills++;
        }
    }
    Notice how the interface has only method declaration. The variables only occur (and are private) in the class which implements the interface. The actual steps taken by the methods that the interface declares are spelled out in the class which implements the interface.

    The instance variables and the concrete steps that a method does are called the implementation.

    Interfaces have no implementation. Java has no syntax support for multiple inheritance of implementation.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Didn't see Fubarable's post above mine.

    I'll repeat that I'm uneasy about the numTeeth and numKills variables being declared within interfaces. You could claim that the number of feline teeth is part of the type and not an implementation detail: Java allows this but you must actually initialise such variables and they are not allowed to change. In the case of numKills is it certainly wrong: the Lion's kill() method want to increment numKills so it can't be the sort of final (can't be assigned to) int that Java allows in an interface.

    I didn't mention having constants declared in an interface because they can be tricky and may be a distraction at this stage. (The other sort of thing an interface may contain, even more so.)
    Last edited by pbrockway2; 11-28-2010 at 07:40 PM.

  8. #8
    BoomPony is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Interfaces (not GUI)
    By guilty in forum Advanced Java
    Replies: 1
    Last Post: 11-05-2010, 07:16 PM
  2. Interfaces
    By justin1980 in forum New To Java
    Replies: 9
    Last Post: 02-10-2010, 12:04 PM
  3. Interfaces
    By computerquip in forum New To Java
    Replies: 19
    Last Post: 09-08-2009, 04:58 PM
  4. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 09:57 PM
  5. Interfaces
    By Kavana Krishnappa in forum New To Java
    Replies: 7
    Last Post: 12-11-2007, 04:28 PM

Posting Permissions

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