• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract class may not have abstract method

 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some 10 year back I remember reading that Abstract class must have at least one abstract method but recently I found following :
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

I am wondering what was the need to change this basics of java.

Any java guru can enlighten me about the need of this change.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not a change; it has never been necessary for an abstract class to have any abstract methods.
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be right.

On second thought, class must be declared abstract if it has one or more abstract method. (so it may not be "MUST" but still you can declare a class abstract though it does not contain any abstract method)
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He's definitely right. Java has always been this way. Or at least since the first language specification; I confess I'm not so familiar with what came before.

An example of a use for this is KeyAdapter - or any of the abstract ***Adapter classes that implement EventListener. All three methods of KeyAdapter are concrete, not abstract. And they all do nothing at all. The idea here is that you can use this to quickly write a KeyListener that only reacts to the events you want to react to - because you only override the methods that you want to do something with. The remaining methods are already overridden to do nothing. There was no need for any particular method in KeyAdapter to remain abstract; they all have a reasonable default behavior of "do nothing". So there are no abstract methods here. And yet, the class is useless unless you override something. So the class is declared abstract, even though no one method is abstract.
 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic