• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I got stuck in this interview question

 
Thill Sivam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I just attended java interview .. one quesion is 'what is the use of an abstract class without any abstract method ???

I just replied, there is no use.... Is it right?
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only possible use is that the intention is to stop that class from being instantiated.

If I consider the code for an organization



and the organization always want to have the employee classified into one of the above type.

 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooops... Yes, a class can be declared abstract even without an abstract method.
This is done when default methods are provided, but it is expected that some/all
methods will be overridden before the class is instantiated with new.
Jim ... ...
 
Thill Sivam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jim,

can you explain a little bit..? thanks.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a design decision by the programmer as part of a larger architecture.

I have done it simply to remind myself of the purpose of the class when looking at the code years later.

Bill
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jim,


expected that some/all methods will be overridden before the class is instantiated with new.


sorry, need to correct your sentence a little bit ... it sounds correct and sounds wrong ...

"all abstract methods need to be implemented on the first concrete class that inherit the abstract class"
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Thill

normally in practical , in between of interface and abstract class ...
if you don need any implemted method (i.e. all methods is abstract), you will choose interface
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lee Kian Giap wrote:@Jim,
Jim wrote:
expected that some/all methods will be overridden before the class is instantiated with new.

sorry, need to correct your sentence a little bit ... it sounds correct and sounds wrong ...

"all abstract methods need to be implemented on the first concrete class that inherit the abstract class"

Except that the question specifically stated there were no abstract methods, so the sentence is already correct (mostly--none of the methods would *have* to be overridden, since it's only the class itself that is abstract).
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javax.swing package has several abstract classes with fully implemented methods.
The class AbstractButton, for example, has a very long list. The strategy is that in your
concrete class you will override only the few methods that you intend to use. Jim ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic