Can any one tell about the difference between the Abstract Class and Interface? Can abstract class replaces Interface? If all things can be done in Abstract class Why we need Interface?

Recommended Answers

All 2 Replies

Can any one tell about the difference between the Abstract Class and Interface?

Abstract classes can contain implementations of some methods, while interfaces cannot. This allows abstract classes to serve as a base implementation of a class hierarchy that shares some (or much) common code and require that subclasses implement only the portions that are specific to themselves.

Can abstract class replaces Interface?

In some cases, yes, in others, no. See next comment below.

If all things can be done in Abstract class Why we need Interface?

Java only allows for single inheritance of classes, but the number of interfaces implemented is unlimited. Use of interfaces allows classes to act as more than one type of object. Without interfaces, all methods that you may want a group of classes to share must be stuffed into a common base class. What if some of these methods only apply to some classes but not others? Your class hierarchy would become a polluted mess, littered with methods that may not even apply to a particular class. Interfaces operate as a contract that guarantees a class implements one or more methods without forcing them to descend from a common base class. Many different kinds of class hierarchies may want to be Printable, or Comparable, or Displayable, without having to extend from some common ancestor. That can only be accomplished with interfaces.

You read more about this here if you wish: http://www.codeguru.com/java/tij/tij0080.shtml

commented: Expert Answer +2

Thanks for your information. Now i have clear idea about Abstract Class and Interface.

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.