• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

questions about Dan mock up exam

 
emo wang
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
I have a question about Dan's mockup exam,
abstract class A { // 1
private abstract void m1(); // 2
private abstract class B {} // 3
private class C extends B {} // 4
}
Which line results in a compile-time error?
a. 1
b. 2
c. 3
d. 4
e. None of the above.
the solution is b, but I am so lost don't know why, would any of you explain to me?
thank you so much for help!
emo
 
Anil Hulikal
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Java Language Specifications
***
It is a compile-time error for a private method to be declared abstract.
It would be impossible for a subclass to implement a private abstract method, because private methods are not inherited by subclasses; therefore such a method could never be used.
***
Also, if you try to compile the program in a good IDE (like Netbeans), it clearly says
***
A.java [12:1] illegal combination of modifiers: abstract and private
private abstract void m1(); // 2
^
1 error
Errors compiling A.
***
Regards,
Anil
 
Anil Hulikal
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Java Language Specifications
***
It is a compile-time error for a private method to be declared abstract.
It would be impossible for a subclass to implement a private abstract method, because private methods are not inherited by subclasses; therefore such a method could never be used.
***
Also, if you try to compile the program in a good IDE (like Netbeans), it clearly says
***
A.java [12:1] illegal combination of modifiers: abstract and private
private abstract void m1(); // 2
^
1 error
Errors compiling A.
***
Regards,
Anil
 
Vineela Devi
Ranch Hand
Posts: 191
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Emo,
Ya. the complier complains at line 2 coz u cannot use both private and abstract modifiers together.
Reason is tht abstract methods are meant to be overridden .But private methods can never be overridden there by contracting each other.
Therefore, we should not use both private and abstract together for a method.
Regards
Vineela
 
fethi makhlouf
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there
That's clear : we can never have a both private and abstract for any method! but what's ackward.....is that c:\ is true!
How can we really have an private abstract inner class?
 
Suresh Thota
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fethi,
private abstract is allowed for inner classe's because other inner class in the same enclosing class can extend this abstract class (inspite of being private) and provide implementation.
Hope am clear.
PS: actually I'm looking for a post I can answer, so that my status will be up to ranch hand.
Cheers
-Suresh
 
sai kumar
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suresh,
I appreciate your confidence.
Can you please answer/clarify my question in the garbage collection thread.
Thx
sk
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic