Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: About abstract, interfaces, and when i should use this.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default About abstract, interfaces, and when i should use this.

    Hello everybody. I am new in Java Programming, but i have a very strong interest with this Java Programming.

    I want to ask about abstract, interfaces, and when i should use this. I have learned and read the references that explained the teory, but i wanna know in reality. Meaby there are one of you that will explain me about my confussion above, oh ya, meaby i am easier to understand with example than theory.

    Thank you very much for the explanation to me.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: About abstract, interfaces, and when i should use this.

    There is a relatively old book which coined the phrase "program to an interface, not an implementation" - its great advice. The reasons why are numerous, suffice it to say it loosely couples things together, making code easier to change, reuse, etc...

    Java has many great examples worth studying. For example listeners found in Swing - an article that describes a way to write this sort of thing can be found at http://www.javaprogrammingforums.com...r-pattern.html
    Another example is sorting provided by the Collections class. All the sort methods cares about is the Comparable interface - generally speaking, this is an example of how one can write algorithms around a single interface, which allows that algorithm to operate on just about anything (so long as that 'anything' implements the interface). Another example would be the Collections framework - List, Map, Set, each has multiple implementations however often all you are about are the base methods provided by the interface.

    BTW The quote above is from a book called Design Patterns by the "Gang of Four"...google 'java design patterns' for more information.
    Last edited by copeg; October 2nd, 2011 at 11:05 AM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Yoga Herawan (October 5th, 2011)

  4. #3

    Default Re: About abstract, interfaces, and when i should use this.

    An interface is something used when you want to implement functionality in the future. Interfaces must have their methods defined by the classes that implement them. Abstract classes allow you to implement their methods in the abstract class and then re-define them if you wish in sub-classes.

    When copeg said, "Program to an interface, not an implementation," he meant that an object should be written in an abstract form (not necessarily with abstract classes) so that it can easily be reused. As a rule, class hierarchies should be avoided:

    class Foo
    class SuperFoo extends Foo

    should be:

    class Foo
    class SuperFoo
    {
    Foo foo;
    }

    Always favor object composition over inheritance.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  5. The Following User Says Thank You to kenster421 For This Useful Post:

    Yoga Herawan (October 5th, 2011)

Similar Threads

  1. what is the use of interfaces and abstract classes?
    By sagar474 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 18th, 2011, 02:34 PM
  2. Interfaces Vs Abstract classes
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:49 AM
  3. Interfaces
    By leyland in forum Java Theory & Questions
    Replies: 4
    Last Post: April 5th, 2011, 08:51 PM
  4. Can't understand why Interfaces are there
    By vortexnl in forum Object Oriented Programming
    Replies: 9
    Last Post: February 14th, 2011, 01:06 PM
  5. Java: interfaces and abstract classes
    By pinansonoyon in forum Object Oriented Programming
    Replies: 1
    Last Post: May 6th, 2010, 10:17 AM