• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Interface & Abstract Classes

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Java Gurus,
I am new to java. Can somebody tell me the pros and cons of using Interfaces and Abstract Classes. Any example would be really helpful for me.
Thanks In Advance,
New2Java
 
Author
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface defines just that - an interface that is specified by a set of public methods, so use an interface rather than an abstract class whenever this is what you need.
Use an abstract class as a base when you need field inheritance and or protected method inheritance as well as public methods.
Java only permits single class inheritance so a class can only have one base class. On the other hand a class can implement any number of interfaces so interfaces provide a way to have multiple-inheritance of public methods.
You can also use an interface to encapsulate a set of constants that you want to access in several different classes. Just make each class implement the interface.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi new2java,
If you want to keep hangin' around the Ranch you need to conform to the naming policy around here which is a first and last name for display.
Now for your question. For old programmers like me Interfaces are the greatest thing since breaking the 1 Meg RAM barrier. What makes OO programing so powerful, and JAVA in particular is the use of interfaces. By using an interface you are essentially creating a contract that all who wish to implement the interface have to sign (metaphorically that is.) That leaves the implementor (class that implements the interface) to fulfill the contract anyway he wants. Clients who use the interface don't have to (nor do they need to) know anything but the interface. The implication of all this is, the implementing class can change (even the inteface can be extended) without breaking any existing clients.
Abstract classes are more for large hierarchies of classes, where you put as much commanality as possible into the abstract class.
Hope this helps
Michael Morris
SCJP
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gee, thanks, Michael I couldn't have said it better.
JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each.
You need to have a compiant display name to win a book.
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface --
(1)Only contains set of abstract method. Doesn't contains instance methods.
(2)Only contains Public methods.
(3)Class has to implements all abstract methods of interface class.
Abstract --
(1) Can contains abstract methods as well as instance methods
(2) To use Abstract class, subclass needs to extends it.
(3) Can contain private,coderanch, protected, static methods.
Thanks,
Angela
 
Tom Keith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Sorry.I made the changes according to the naming policy.
I still have a question.
When we implement the interface, all the methods have to be overwritten by all the classes that use this interface right? But are we not doing the same thing by extending an abstract class where in, if the class does not explicitly override the methods, still all of the methods are available?
Please do correct me. I am still in confusion? Can somebody give an example please?
Thanks,
Tom Keith.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's some example code:




Not a lot of commentary, but perhaps it's enough. I recommend that you actually program, compile, and otherwise play with these classes to see how things work. Try to make them not work and figure out why what you did didn't work - make some fields private, or final, or static, try to give some methods implementations, etc.
Good Luck.
[ March 26, 2002: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I nitpick at the author's response, am I then excluded from winning the book?

Originally posted by Ivor Horton:
interfaces provide a way to have multiple-inheritance of public methods.


Just to point out one glaring fact...
Public methods in an interface have no implementation. So, no implementation is inherited. As has been stated, the implementing class is agreeing to implement the methods in the interface - to provide the implementation.
I've run into more than one person new to Java and OOP for whom this concept was a bit confusing at first, because the book they were reading said something like: "...implementing provides multiple inheritance in Java..." - at least that's how the newbie interpreted what the book said.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic