-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.

2
  • Also strongly recommend the answer at this question: stackoverflow.com/questions/56867/interface-vs-base-class
    – Kirk Woll
    Oct 5, 2010 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, 2014 at 17:00

3 Answers 3

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.

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.

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)

2
  • hi, when we need to use overloading and overriding, if u know good example please tell me.
    – Velmurugan
    Oct 5, 2010 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 stackoverflow.com/questions/154577/…, there is an example there. Oct 5, 2010 at 18:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.