• 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

Abstract Methods

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the following questions in a mock test
Which of the following statements are true?

a) An abstract method cannot be final
b) An abstract method cannot be static
c) An abstract method cannot be private
d) An abstract method must be specified inside an abstract class only
e) An abstract method cannot throw any exceptions
Select all valid answers.
My answer was a,b,d
But the given answer was a,b,c,d
I tried compiling a program defining an abstract method to be private and i was able to successfully compile that.

Thanks for the help
Chandar
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm, I tried compiling an example of your code on my system which is using JDK 1.2 and I get a compilation error message stating that "Abstract methods can't be private".
If you think about it for a moment how could they be? By definition an abstract method has to be given a method body in a derived class before that class can be instantiated but if that method is private then its not visible to the derived class, see the dilemma?
[This message has been edited by Joe Java (edited March 10, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe
This is the piece of code i tried to execute and it is working. I am compiling with Visual Cafe. Can you please let me know whether you are able to compile the code below with jdk1.2.2
thanks for your help
abstract class test1 {
abstract private void Priv();
}
public class test3 extends test1 {
private void Priv() {System.out.println("Able to define abstract method as private ");}


public static void main(String args[]) {
new test3().Priv();

}
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jdk 1.2.2 and jdk 1.3 beta both give compile errors:
test3.java:2: Abstract methods can't be private: void Priv()
abstract private void Priv();
^
test3.java:6: The method void Priv() declared in class test3 cannot override the method of the same signature declared in class test1. The access modifier is made more restrictive.
private void Priv() {System.out.println("Able to define abstract method as private ");}
^
2 errors
Also, the JLS is specific about this:

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 visible to subclasses; therefore such a method could never be used.


So it looks like this is a bug in Visual Cafe.
[This message has been edited by Jim Yingst (edited March 11, 2000).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic