This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes will it possible to definefianl abstarct class and interfaces? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "will it possible to definefianl abstarct class and interfaces?" Watch "will it possible to definefianl abstarct class and interfaces?" New topic
Author

will it possible to definefianl abstarct class and interfaces?

vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
can we define interfaces and abstarct classes as final?
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
Interfaces, no. Abstract classes, yes, but why would you ever want to?


-Rich, SCJP 1.4
Amit Biswas
Ranch Hand

Joined: Jun 01, 2006
Posts: 52
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

Joined: Dec 14, 2006
Posts: 105
Hi Richard,

Are You sure of declaring abstract classes as final? :roll:
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

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?


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
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


R.N
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.)


[Jess in Action][AskingGoodQuestions]
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
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 ]
 
 
subject: will it possible to definefianl abstarct class and interfaces?