• 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

Question is ambigitive

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone tell me whether the following statement is true or not.

A final class may not have any abstract method.

I think this is false since it means "A final class may have abstract method".

But a practice tester I found it as true.
I am very dissapointed about this meaning.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final class may not have any abstract method.

This statement is not false because as we know if a super class contains an abstract method it need to be implemented by the derived class.

But a final class cannot be inherited by any other derived class. Actually we declare a final class so that it is not inherited by any another class.

I hope you are getting it.

Yes it is of no use to declare abstract methods in final class but we can declare abstract methods in final class.

Please correct me if i am wrong.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rajeshwar,

I think your last affirmation is not correct.


Yes it is of no use to declare abstract methods in final class but we can declare abstract methods in final class.



Once you declared a method as abstract, the compiler obligates you to declare the entire class as abstract.

So a final class containing an abstract method would be forced to be declared as final abstract class MyClass which for sure is illegal.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems to be more a problem of semantics than of Java concepts. "A final class may not have any abstract method(s)," does not mean, "A final class may have abstract method(s)". Those two sentences have opposite meanings. The first is true, and the second is false.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't have abstract and final at the same time because you must override the abstract method and a final class can't be overriden...it's like writing final abstract class...non sense :-)

final class A{
abstract void myA();
}
The compiler isn't very happy...
"A is not abstract and does not override abstract method myA() in A
final class A{
^
1 error"
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edisandro Bessa:
...Once you declared a method as abstract, the compiler obligates you to declare the entire class as abstract.

So a final class containing an abstract method would be forced to be declared as final abstract class MyClass which for sure is illegal.


This is exactly right. If you declare an abstract method without declaring the class as abstract, you get the error, "ClassName is not abstract and does not override abstract method methodName..."

But then if you declare the final class as abstract, you get the error, "illegal combination of modifiers: abstract and final..."
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Greg suggested, this seems to be a semantics issue.

Perhaps you're reading this as, "A final class might not have any abstract methods." This is still a true statement. But you could be interpreting this to imply that a final class typically does have abstract methods, although it's not required to (i.e., it "might not"). This would be a misinterpretation from reading too much into the statement.

Better wording might have been: "A final class cannot have any abstract methods." This is (clearly?) true.
 
rajeshwar Rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks every one for correcting me. Dont mind i did not think it in depth as i was a little bit curious to give answer quickly
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic