• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Interfaces and abstract classes

Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could somebody provide a definition for interfaces and abstract classes. I know how to work with it but was unable to provide a definition for both when asked(in an interview). Be prepared for such questions when u face a java interview.
Thanx in advance
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An Abstract class is a class which declares the existence of methods but not the implementation and can contain member variables and non-abstract methods.
An Interface can define only static final member variables and can only have abstract methods. An interface gives the illusion of bending the Java single inheritance rule. While a class definition can extend only a single class, it can implement as many interfaces as needed.
Hope this helps! Thanks for the interview tip!
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai i hope this will help u
An abstract class cannot be instantiated. Only its subclasses can be instantiated. You indicate that a class is abstract with the abstract keyword like this:
public abstract class Container extends Component {
Abstract classes may contain abstract methods. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. It has no body. For example,
public abstract float price();
Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do.
Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract.
For more details, see section of the Java Language Specification.
What's an interface?
An interface is an idea taken from Objective C. It describes the public methods that a class implements and their calling conventions without saying anything about how those methods are implemented. It is the responsibility of each class that implements an interface to provide code to handle the cases where the methods of the interface are called.
For example suppose you're writing an inventory database. The inventory may include many different items of many different types and classes. However each item in the warehouse needs to be able to tell you its price. Normally you would implement this by having each class extend a common superclass. However that's not always convenient. Instead you can declare an interface called Price with a price() method like this:
public interface Price {
public float price();
Any class which implements the Price interface must contain a method with the signature public float price(). The code of the price() method is included separately in each separate class which implements Price, not in the Price interface itself.
Different classes in your warehouse can each implement the Price interface like this:
public class Monopoly extends BoardGame implements Price {
// other methods
public float price() {
return 14.95;
When other code is passed an object, it can test whether the object implements Price with the instanceof operator. For example,
if (o instanceof Price) System.out.println("Subtotal is " + o.price());
In fact, interfaces can be used to tag objects. The java.rmi.Remote interface declares no methods. Its sole purpose is to indicate that an object is a remote object. In general, sub-interfaces of java.rmi.Remote will declare remote methods, however. For example,
public interface Hello extends java.rmi.Remote {
public String sayHello();
public class HelloImpl extends UnicastRemoteServer implements Hello {
public String sayHello() {
return "Hello";
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Well, I just realized I wasn't answering the question you asked, but I'll leave this in anyway.....]
The interesting thing about an interface is that a class can be referred to by its interface. For instance, if you have a bunch of completely different classes that all implement interface "chewable" (containing method "chewOnMe()"):
Vector chewables = new Vector();
and fill it with all different kinds of objects, as long as they are chewable:
Candy starburst = new Candy();
RubberToy squeaker = new RubberToy();
VeryUnfamiliarConcept javaInterface = new VeryUnfamiliarConcept();
MouthCancerSource skoal = new MouthCancerSource();
Then you can chew on any of them, by casting them to chewable:
for (int i = 0; i < 10; i++) {
An interface is much "looser" than inheritance. The only similarity that objects implementing an interface have, is the ablility to deal with the method calls in the interface.
Now here's something I'm not sure of: can I have an array of chewables???
[This message has been edited by eric moon (edited December 16, 2000).]
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic