Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

will it possible to definefianl abstarct class and interfaces?

 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we define interfaces and abstarct classes as final?
 
Richard Anderson
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces, no. Abstract classes, yes, but why would you ever want to?
 
Amit Biswas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi Richard,

Are You sure of declaring abstract classes as final? :roll:
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • 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?
 
Rohit Nath
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic