Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do we have concrete methods in abstract class ?

 
Dhaval Shah
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please tell me the logic behind having concrete methods in abstract class as we cant instantiate them ?

E.g. - (Abstract Class)InputStream - has concrete method - read (byte [] b)
and the subclass FileInputStream also has method -- read (byte [] b)

Now we cant create an object of InputStream as it is an abstract class then why do we have concrete method (read (byte [] b))in InputStream ? What is the use of having concrete methods in abstract class

Thanks
Dhaval
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
having concrete methods in abstract class

It is a design decision by the programmer to enforce a particular vision of how the class is to be used. In InputStream it ensures that extending classes do not need to provide an implementation of read( byte[] ) in order to work.

You can in fact have a class declared abstract but containing all concrete methods.

Bill
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q: Why have abstract classes at all, since they can't be instantiated?
A: They can be subclassed!

Q: Why have concrete methods in abstract class, since they can't be instantiated?
A: They can be subclassed! The subclasses can inherit these methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic