8

I have some questions around abstract classes and their use. I know the basics about them; for example, that they can't be instantiated, they can have concrete and abstract methods, ... But I guess what I wanna know is, what purpose do they serve in life (in software engineering)?

What is the purpose of abstract classes in Java? Why and when should one use an Abstract class? If you can use a normal class and then inherit it, why would you inherit an abstract class? How would using abstract classes make our life easier? would it provide better maintainability? more flexibility? ...

btw, I've already looked at some similar questions and answers, including Understanding the purpose of Abstract Classes in Java, but they don't answer my question. I'm more looking for answers that shed light on the philosophy behind the introduction of abstract classes in the first place in Java.

Thanks

Community
  • 1
  • 1
Spiky
  • 278
  • 1
  • 3
  • 9

1 Answers1

8

An abstract class can be used as a type of template for other classes, and most commonly used for inheritance.

The best example is from the book head first java:

abstract class Animal(){}

then you can extend child class such as: dog, cat, fish.

OPK
  • 3,870
  • 5
  • 30
  • 56
  • 4
    Providing a "template to other classes" is a purpose I hadn't thought of. Thanks – Spiky Apr 07 '15 at 18:03
  • The way you define `inheritance and templates` only based on `abstract` classes is not right –  Apr 08 '15 at 14:33
  • @RajaAnbazhagan I never defined `inheritance`, I said `abstraction` concept is connected with `inheritance`. I never defined `templates`, i said `abstract` class can be used as a `template` for other sub classes. – OPK Apr 08 '15 at 14:37
  • When you saytemplate, It can be achieved by interfaces itself. An abstract class allows you define a basic functionality leaving undefined parts. –  Apr 08 '15 at 15:19
  • @RajaAnbazhagan Look, I was simply using plain English to answer the question that OP asked. He was asking why we need `abstract` class, not asking why use `Interface`. Please do not over generate questions that are not related to the question. If you do not like the word `template`, you can totally use another word to replace it.I didn't imply `interface` here. – OPK Apr 08 '15 at 15:44