Forums Register Login

will it possible to definefianl abstarct class and interfaces?

+Pie Number of slices to send: Send
can we define interfaces and abstarct classes as final?
+Pie Number of slices to send: Send
Interfaces, no. Abstract classes, yes, but why would you ever want to?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hi Richard,

Are You sure of declaring abstract classes as final? :roll:
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.)
+Pie Number of slices to send: Send
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 ]
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 867 times.
Similar Threads
abstract class and interfaces
How does this go pls explain
Wrong combinations
Abstract class and Interface
Difference in Abstract classes and Interfaces
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:28:09.