Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

will it possible to definefianl abstarct class and interfaces?

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we define interfaces and abstarct classes as final?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces, no. Abstract classes, yes, but why would you ever want to?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anything that is defined as abstract cannot be defined as final. Interfaces are always abstract regardless of whether one types the word abstract. for example: when you write
public interface A{....} it is actually
public abstract interface A{...} // in fact you can type in this syntax too

So Abstract Classes and Interfaces CAN NEVER BE FINAL
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

Are You sure of declaring abstract classes as final? :roll:
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abtract - used to say, "I have no clue, how to implement it? so, you implement it"

final - used to say "I give you something, but you should use it as it is. you should not change it."


Why do you want to make abstract(say a class) and also you want to say "don't extend it". does this make any sense? no, right?
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final class cannot be extended.
A final method cannot be overridden.
A final variable cannot be modified.


If you have a final class then you cannot extend.
If you make an abstract class as final then you cannot extend it to provide the missing functionality. Therefor you cannot have an abstract class as final as it goes agains extensibility and basic purpose of having an abstract class!

Thanks!
Rohit Nath
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes you'll want to create a class that can neither be extended nor instantiated. java.lang.Math, java.lang.System, and java.lang.Collections are all like this. If you could define the class as "abstract final", that would do the trick, so perhaps that's what he's interested in. The right way to do this in Java is just to give the class a single empty constructor, and make it private. If a class's only constructor is private, then it can't be extended (well, except by its own inner classes) nor instantiated (except by code inside its own class body.)
 
Rohit Nath
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the methods and properties defined in Interface are by default public and abstract

If the methods themselves are abstract then the interface also is abstract.



Try compiling above code and see.
If you compile this it does not gives any compiler errors.



compiling abstract final class above give compile time error.

MyAbstractClass.java:12: illegal combination of modifiers: abstract and final

Thanks!
Rohit Nath
[ December 15, 2006: Message edited by: Rohit Nath ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic