Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2011
    Posts
    28
    Rep Power
    0

    Default Abstraction and interfaces

    Hey so I'm currently in a Java class at my university. We just got into abstract classes and methods and interfaces. I'm not quite grasping what these are and why/when they would be used. I kind of understand the Abstract class concept. From what I've read, it looks like they are used if you need a super class that you do not what any instances of. But why? I guess I'm just not understand these concepts.

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

    Default Re: Abstraction and interfaces

    Have you ever seen a bird? I haven't; I have seen sparrows and hummingbirds and eagles etc. granted, they all belong to the species 'bird', but they aren't 'birds'. The class 'bird' is the abstract super class and the hummingbirds etc. extend from that class; we can treat them as if they were birds an even say that 'they are' birds, but they're more than just that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Nov 2011
    Posts
    28
    Rep Power
    0

    Default Re: Abstraction and interfaces

    Ok and interfaces? Are they the same thing?

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

    Default Re: Abstraction and interfaces

    Quote Originally Posted by ResidentBiscuit View Post
    Ok and interfaces? Are they the same thing?
    Nope, an interface is more like a 'flying creature' and the abstract 'bird' class implements it, but also bats,flies, wasps and some fish implement this interface. The concept 'interface' is an artifact in Java because it doesn't have multiple inheritance of implementation. In languages such as C++ (that do have MI) there is no need for a separation of classes and interfaces.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Nov 2011
    Posts
    28
    Rep Power
    0

    Default Re: Abstraction and interfaces

    Hmm so this all kind of a way to get around the lack of multiple inheritance in java? And interfaces are just more "broad" or "general" than abstract classes?

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

    Default Re: Abstraction and interfaces

    Quote Originally Posted by ResidentBiscuit View Post
    Hmm so this all kind of a way to get around the lack of multiple inheritance in java? And interfaces are just more "broad" or "general" than abstract classes?
    Sort of; single inheritance makes all defined classes to be a node (or location) in a tree; the root of the tree is the Object class (see your textbook). Sometimes we don't want that and the interface concept helps here: arbitrary classes at arbitrary locations in the inheritance tree can implement a certain interface and can be treated as such, e.g. birds, bats, flees, etc. are nodes in an inheritance tree (at different locations) and implement the 'flying creature' interface. If some code deals with 'flying creatures' it can handle all those birds, bats, flees etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Abstraction Assignment Help!
    By CoolKidsClub in forum New To Java
    Replies: 1
    Last Post: 04-27-2011, 02:12 AM
  2. Help with abstraction
    By Shephard in forum New To Java
    Replies: 5
    Last Post: 03-05-2011, 09:43 PM
  3. Are abstraction and encapsulation concepts not in C?
    By hitesh_public in forum New To Java
    Replies: 1
    Last Post: 08-20-2010, 02:23 PM
  4. Abstraction
    By maya700 in forum New To Java
    Replies: 4
    Last Post: 07-04-2010, 06:26 PM
  5. Abstraction
    By anurag in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 06:15 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
  •