• 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 Methods

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every one,

In one of the mock exams i came over this question...

Abstract method cannot be final. True or False ?

The answer given is True.

In some book i read that abstract methods can have final methods, in which case the subclasses of the abstract class need not give implementation to those methods which are declared as final in the base class.

Can any one clarify this...

Thanks in advance
 
Vishnu Munnangi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just executed the program and i got compile time error that illegal combination of modifiers static and abstract.

I should have asked this question after compiling the program.

Sorry for the inconvenience
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vishnu priya parimi:
Abstract method cannot be final. True or False ?



It also helps to consult the
JLS.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True. Abstract class need to be implemented by subclass, so can not be final. But it can have non-abstract methods in it which can be declared as final if you don't want subclass override those methods or make them abstract.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An abstract CLASS can have final methods.
An abstract METHOD can not be final.

Abstract means: MUST be implemented by subclasses
Final means: MAY NOT be implemented by subclasses.

Thus abstract final would mean: MUST be implemented but MAY NOT be implemented by subclasses, which is impossible.

Therefore the combination is not allowed.

As an abstract class can have non-abstract methods, some of those methods may be final.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code did compile and run. I declared the final varible as private. It appears that you can place a final varible in an abstract class if it is declared as private.



([C0DE][/C0DE] tags added.)
[ October 07, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
suri, please read our Javaranch Naming Policy and change your displayed name to comply with it. Please use two names in the format <first name> <family name>.

Thankyou
-Barry
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because private are not inherited.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas, you can place any final variable of any access specification (public, private, protected, default) in a abstract class:

 
reply
    Bookmark Topic Watch Topic
  • New Topic