-1

Possible Duplicate:
Abstract class and Interface class?

When i need to use abstract class and interface in Java?

My doubt is

  1. Which situation i need to use abstract class and which situation i need to use interface.
  2. interface satisfy the abstract class properties. so then why we need especially abstract class?
  3. i know, that abstract class contains abstract methods and non abstract methods, but we can use abstract class as a ordinary class, then the result will be same in the both classes. the ordinary class also inherited same as abstract class. So why we need abstract class.

If anybody know the good example please reply me.

Thanks.

Community
  • 1
  • 1
Velmurugan
  • 63
  • 1
  • 3
  • 9
  • Also strongly recommend the answer at this question: http://stackoverflow.com/questions/56867/interface-vs-base-class – Kirk Woll Oct 05 '10 at 17:47
  • Consider using abstract classes if any of these statements apply to your situation: - You want to share code among several closely related classes. - You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private). - You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong. – sarath Feb 11 '14 at 17:00

3 Answers3

2

Another important aspect of abstract class is that unlike interface, adding new methods to it won't break binary compatibility. So, from the API evolution point of view, especially when you can expect additions to the public API, abstract classes are more preferable.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
1

I would say that there are two types of inheritance. One I would say as Implementation Inheritance and other as Contract Inheritance.

Abstract classes are used for having Implementation Inheritance. You can extend/change the behavior of your super/parent class.

Interfaces would go for Contract Inheritance. Where you are more interested in having the class implement some kind of a contract (service methods with arguments - more of a contract) and the behavior is different for different implementation, nothing generic that you can bundle up in an abstract class and extend the behavior.

Faisal Feroz
  • 12,458
  • 4
  • 40
  • 51
0

You need to use abstract classes if you want to apply the template pattern http://en.wikipedia.org/wiki/Template_method_pattern, usually in framework code. As a rule of thumb, if you're not intending to implement the template pattern, you're better off with interfaces, which permit loose coupling, the way spring framework does. Loose coupling leads to a design open to evolutions and a better testability, with techniques like mock objects (http://easymock.org)

Victor Ionescu
  • 1,967
  • 2
  • 21
  • 24
  • hi, when we need to use overloading and overriding, if u know good example please tell me. – Velmurugan Oct 05 '10 at 17:56
  • overloading has nothing to do with abstract classes and interfaces. Overloading is having a method with the same name as an existing method, but with different kind or number of parameters. Overriding is redefining a method of a superclass in a child class. See the answer of http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading, there is an example there. – Victor Ionescu Oct 05 '10 at 18:03