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

Question is ambigitive

 
Lanka Prasad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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.
 
rajeshwar Rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • 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.
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Arno Reper
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • 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"
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic