• 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:

About interfaces and Abstract class

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tell me exactly in which scenario we use abstract class and in which scenario we use interfaces in real time project.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract classes have only one advantage over interfaces, as I see it: If you want your subclasses to have identical implementations of one or more methods, you make those methods non-abstract in the abstract class, while you make specific implementations of the abstract methods in the subclasses.

Rule of thumb: If you are in doubt, use an interface. You can change it later, if necessary.
[ June 22, 2006: Message edited by: �dne Brunborg ]
 
Marshal
Posts: 80617
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reachvijay Kumar: Welcome.

This sort of question comes up frequently. Try here.

Adne has told you part of it, but has forgotten that you don't have multiple inheritance.
Imagine the scenario class Animal, class Cat, class Goldfish, class Budgerigar. You have never seen an animal, but you have seen a cat a budgerigar and a goldfish (though maybe not in the same place ). so make the Animal class abstract.
Again. Try class Vehicle, class Car, class Motorcycle, class Truck. Make Vehicle abstract.

In all those cases one can say "a cat is an animal," or, "a bus is a vehicle."

As Adne says, you can have implemented methods in those abstract classes, where you expect the behaviour to be the same in most cases.
You can use abstract methods in your abstract classes as well, where you expect them to be different in most cases, so the Animal.eat() method would be abstract.

An abstract method requires the concrete subclasses to implement it appropriately.

An interface describes a behaviour or set of behaviours. You may have to use an interface because methods require it, eg the Arrays.sort() method requires the objects sorted to implement the Comparable interface.
An interface has any number of abstract methods, eg java.langComparable has 1, java.awt.event.MouseListener has 5, java.util.List has 27, and java.io.Serializable has none.
An interface represents several abstract methods. It is a little like having a very abstract class. All the methods in your interfaces have to be implemented in every concrete class implementing them (but an implementation in a superclass will count).
Most programmers use interfaces with relatively few methods; there are lots of classes which implement java.util.List, but we take them unchanged from the API.

The real advantage of interfaces is that you can use several of them without messing up your inheritance.

There are several other threads about multiple inheritance, which might or might not be of interest. Here. Here.
And a rather old but short and pithy thread on the advancde forum.

CR
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Programmatically then can be used for virtually the same things. However, conceptually, one represents a partial implementation of an object and one represents the conceptual object itself.

Also, abstract classes are good for the Template Method Pattern.
reply
    Bookmark Topic Watch Topic
  • New Topic