Results 1 to 10 of 10

Thread: Interfaces

  1. #1
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default Interfaces

    Hi, could someone explain to me the difference between a regular class and an interface and why you would use it. i know there is a million things on this already if you Google it. But, I have been looking and I'm not understanding it fully. I also need to know about abstract classes and the difference between them to. Maybe this is to much to ask? Maybe you have some links to somewhere that explains it really well?
    Thank you!

    From what I am reading, an abstract class seams the same as using a superclass and sub classes in that it is used to pass methods down to subclasses. Thants why I am confused. Im trying to figure out the real differances between all of these.
    Last edited by justin1980; 01-20-2010 at 03:57 AM. Reason: Add more

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    ok lets break it down.

    a regular class is a class where every method can be implemented

    an interface is like a container which just contains the method signatures but are not implemented.These are useful when you write a large amount of code where different classes keep using a similar type of method signature. So we put the method signature inside in an interface and make it implement to the desired class.

    Abstract class is a mix of interface and regular class, as it has both the features in it. It can just declare the method and also have implemented methods.

    These are just a basic definitions. if your interested
    Definitions

    Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    When to use
    interface vs abstract class : Java Glossary

    Hope it helped

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

    Default

    There's so much to this subject that to try to summarize it in the confines of a forum is not doing it justice. I strongly believe that your best bet is to read aggressively (as opposed to passively) as much as you can about these beasts and try to use them. I'll tell you this: I didn't fully appreciate the power of interfaces until I started studying design patterns such as the observer pattern (which Swing coding makes much use of). Once I learned this, the light clicked on in my eyes, and I thought, "Interfaces, .... oh yeah...[cue bright sun light shining through and swelling music], so THAT's their power..."

    Hopefully with time, you'll see the light. Praise Java, amen.

  4. #4
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    haha I am studying this because I started an advanced Java class today and its called Advanced Java and Design Patterns lol
    He wants us to do a speach on them next week!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    A quick summary, because that's all you can possibly hope for here:

    An interface is contract. It essentially says that anything that implements that interface will provide this functionality (ie these methods). Look at Collections (ie List) to see that in action.

    An abstract class provides some pre-done functionality, usually stuff that is likely to be standard for all (or most) extending classes, but also provides additional methods which will (as with an interface) need to be implemented by any child class. So it's a contract with some predefined implementations.

    A concrete class is the only one that can be instantiated because it fulfills all its contractual obligations.

  6. #6
    mindblaster's Avatar
    mindblaster is offline Member
    Join Date
    Dec 2009
    Location
    Pakistan
    Posts
    18
    Rep Power
    0

    Cool

    Quote Originally Posted by [RaIdEn] View Post
    ok lets break it down.

    a regular class is a class where every method can be implemented

    an interface is like a container which just contains the method signatures but are not implemented.These are useful when you write a large amount of code where different classes keep using a similar type of method signature. So we put the method signature inside in an interface and make it implement to the desired class.

    Abstract class is a mix of interface and regular class, as it has both the features in it. It can just declare the method and also have implemented methods.

    These are just a basic definitions. if your interested
    Definitions

    Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    When to use
    interface vs abstract class : Java Glossary

    Hope it helped
    Great Described, i also had the problem to understand the diff btw interface and abstract class before read this.

    thanks mates.
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The strong point of interfaces is hiding implementation, and that makes your code easily maintained. By using interfaces, you can write methods, that survive intact, if you do extensive changes to the class, that implements them. Consider:
    Java Code:
    public interface SomeStruct {
       public abstract void add(Object o);
       public abstract Object remove(Object o);
    }
    Now, you write up your "real" class:
    Java Code:
    public class MyStruct implements SomeStruct {
       private Object[] contents; //initialized with constructor
       public void add(Object o) {
               //add the object into the array
       }
       public Object remove(Object o) {
              //remove and return object from array
       }
    }
    Now, you make up another class that uses a different structure to hold data:
    Java Code:
    public class AnotherStruct implement SomeStruct {
       private Vector contents;
       public void add(Object o) {
              //insert object into vector
       }
       public Object remove(Object o) {
             //remove and return object from vector
       }
    }
    Now, you have 2 classes, that use different methods for storage, but to the outside observer act the same! So instead of:
    Java Code:
    public void doSomething(MyStruct m) {
       //do something with the struct
    }
    You can simply write:
    Java Code:
    public void doSomething(SomeStruct s) {
        //do something with the struct
    }
    This function will work, if you pass it a MyStruct or AnotherStruct object the same, because they both implement the same set of methods.
    Abstract classes are similar to interfaces, but they allow you to fully designate a method(or methods), that it's subclasses will inherit, while interfaces only allow method signatures.

  8. #8
    mindblaster's Avatar
    mindblaster is offline Member
    Join Date
    Dec 2009
    Location
    Pakistan
    Posts
    18
    Rep Power
    0

    Default

    Interfaces are used for multiple Inheritance.
    Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    Java does not have multiple inheritance.

  10. #10
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by mindblaster View Post
    Interfaces are used for multiple Inheritance.
    i know what you mean but i guess its a kinda poorly phrased.

    he probably meant, java class can "implements" multiple interfaces

    inheritance is related to the keyword "extends"(classes) rather than interfaces

Similar Threads

  1. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 09:57 PM
  2. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 05:52 PM
  3. Interfaces
    By Kavana Krishnappa in forum New To Java
    Replies: 7
    Last Post: 12-11-2007, 04:28 PM
  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
  •