Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract class without abstract method  RSS feed

 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ,
Why java compiler allowes following code to compile.What we gain with such a class design.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question and sounds like homework.

To answer the "will it compile?" question, why don't you just try it?
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swapnil didn't ask 'will it compile.' The question was 'why' will it compile.
 
Campbell Ritchie
Marshal
Posts: 55741
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstract class is one you don't intend to instantiate. It doesn't have to contain abstract methods.

A concrete class is one you do intend to instantiate. It cannot contain abstract methods.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not? What does the abstract keyword mean? Your observations match the definition given in the Sun Glossary. Editing later: Oops, maybe not! The definition of "abstract class" is not quite complete, I think. A class that has abstract methods or the abstract keyword seems to be what the compiler is telling you.

Why would we ever make an abstract class with no abstract methods? Turns out there are some hazards in extending a concrete class. So even though we make a perfectly complete and functional class, because we intend it to be extended and not used as-is, we make the class abstract. The abstract class serves as a nice set of default behavior that others can override without extending a concrete class. Hmmm, as I type that I don't find it very convincing as a great design.

Ok, what are the hazards? Google for "Liskov Substitution Principle" and see if you can find a way that extending a concrete class could get you in trouble. Let us know what you find.
[ May 10, 2007: Message edited by: Stan James ]
 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all..thanks James for your explanation..
cheers..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!