• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract class  RSS feed

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it legal to have an abstract class with none of its variables or methods is actually defined as abstract ?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try this yourself quite easily, couldn't you? Here's your minimal test case:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben oliver wrote:Is it legal to have an abstract class with none of its variables or methods is actually defined as abstract ?

Yes; except that there's no such thing as an abstract variable.

Winston
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that makes sense. i can imagine there might be a time when you want all the methods concrete but dont want the class to be instatiated
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you can have such abstract class, but note that abstract keyword is not used for variables.
 
Korhan Rankin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ben,

as replies mentioned you can, but I would ask myself if you are not making something you will regret later. You are setting up a class hierarchy dependency without actually using the "abstract" background.

If it wasn't just a question so that you actually are going to do something this... I guess you may want several classes beeing grouped by that abstract class to distinct somewhere in die code using instanceof or something... if that is the reason, I suggest you not doing it..
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:that makes sense. i can imagine there might be a time when you want all the methods concrete but dont want the class to be instatiated


I have had a failure of imagination, please could you give an example.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit I can imagine of that.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy Gibbons wrote:
Randall Twede wrote:that makes sense. i can imagine there might be a time when you want all the methods concrete but dont want the class to be instatiated


I have had a failure of imagination, please could you give an example.


Don't the Swing XxxAdapter classes follow that model? A bunch of concrete empty methods, and you just override the ones for events you care about? The class wouldn't have to be abstract, but making it so makes it clear that it's of no use on its own and is intended to be extended.
 
Chetan Sarnad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Wendy Gibbons wrote:
Randall Twede wrote:that makes sense. i can imagine there might be a time when you want all the methods concrete but dont want the class to be instatiated


I have had a failure of imagination, please could you give an example.


Don't the Swing XxxAdapter classes follow that model? A bunch of concrete empty methods, and you just override the ones for events you care about? The class wouldn't have to be abstract, but making it so makes it clear that it's of no use on its own and is intended to be extended.


If none of the methods have body then wouldn't it be better to go for an interface rather than an abstract class?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chetan Sarnad wrote:If none of the methods have body then wouldn't it be better to go for an interface rather than an abstract class?

The listener adapters are specifically designed so that you don't have to implement all the methods if you're only interested in one of them - it provides a default implementation of "do nothing".

Another example is HttpServlet. You use it by extending it and implementing at least one of doGet, doPost etc. But you only have to implement one if that's all you're interested in. If the methods were abstract you'd have to implement all of them.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chetan Sarnad wrote:
Jeff Verdegan wrote:
Wendy Gibbons wrote:
Randall Twede wrote:that makes sense. i can imagine there might be a time when you want all the methods concrete but dont want the class to be instatiated


I have had a failure of imagination, please could you give an example.


Don't the Swing XxxAdapter classes follow that model? A bunch of concrete empty methods, and you just override the ones for events you care about? The class wouldn't have to be abstract, but making it so makes it clear that it's of no use on its own and is intended to be extended.


If none of the methods have body then wouldn't it be better to go for an interface rather than an abstract class?


No, because it's


not



An interface would be the equivalent of the second one, but it cannot do the first one. The reason we want the first one is that there are a bunch of "callback" methods that Swing calls to tell us something has happened. Often times, we only care about a couple of those. However, Swing doesn't know which ones we care about, so it calls all of them, meaning we need implementations for all of them. The abstract parent class gives us dummy implementations for all of them, so we only need to override the ones we care about. Otherwise, we'd have to provide all those dummy implementations ourselves.

It's a case of deliberately choosing convenience over OO purity for a specific usage pattern.
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DOH of course.
 
raj pandey
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben oliver wrote:Is it legal to have an abstract class with none of its variables or methods is actually defined as abstract ?
@ben
yes !!!you can declare a class as abstract with using concrete methods inside the class.
the main advantage to make a class as abstract without incorporating abstract method is that no body can make object of this class from outside.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raj pandey wrote:
ben oliver wrote:Is it legal to have an abstract class with none of its variables or methods is actually defined as abstract ?
@ben
yes !!!you can declare a class as abstract without using abstract methods inside the class.


In the future, please read the other responses before answering.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!