• Post Reply Bookmark Topic Watch Topic
  • New Topic

About interfaces and Abstract class  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56610
172
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!