• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why we use abstract class?

 
manas ranjan mandal
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to know that what is the need of using abstract class in java programm?we can use interface every where without extends abstract class?
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DO you understand the concept of abstract class?
 
ch hemasundar
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manas,
Abstarct classes are meant for 'abstracting'.
means if some classes are having common behaviour, instead of writing evry time the same thing in each class, write that in one class and ask the other classes to use it[by making the classes as subclasses to the abstract class].
this is nothing but inheritance.
To summarise:
Use abstract classes when you want default behaviour for some classes
Use interfaces when you want different behaviour different classes.

hope this helps you

regards
hemasundar
 
manas ranjan mandal
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for clearing my doubt
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a rough rule-of-thumb:
  • Functionality: likely to be different in all parts: Interface.
  • Data, and functionality, parts will be mostly the same, parts different: abstract class.
  • Data, and functionality: actually working, if extended only with slight changes: ordinary (concrete) class
  • Data and functionality, no changes planned: ordinary (concrete) class with final modifier.
  • Data, and maybe functionality: read-only: enum members.
  • Remember this is very rough and ready and not at all strictly defined, but there is a spectrum from interfaces where everything is intended to be changed to enums where everything is fixed a bit like a read-only file.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic