• 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

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 191)
Read the following piece of code carefully
public abstract class AbstractClass
{
public AbstractClass()
{
System.out.println("this is an abstract class constructor!");
}
public void aMethod()
{
System.out.println("This is in the method in the abstract class");
}
}
Attempting to compile and run the code will cause
1 Compiler error - abstract classes cannot have constructors.
2 Compiler error - the method AbstractClass does not have a valid return type.
3 Compiler error - the class cannot be declared as abstract as it does not have any unimplemented methods.
4 No compiler error - the class is practically not an abstract class and can be instantiated.
5 No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the extended class will call the constructor of the abstract class (implicitly or explicitly).
6 No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the abstract class will never be called.
the ans is 5th. why? the abstract class should include abstract method,should not it?
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class containing even one abstract method has to be declared abstract. (has to)
But a class not having any abstract method may be declared as abstract. (you may) So effectively you could declare any class abstract, not having any abstract method. (The designer does not want that this class be instantiated.)

Originally posted by wei liu:
Question 191)
Read the following piece of code carefully
public abstract class AbstractClass
{
public AbstractClass()
{
System.out.println("this is an abstract class constructor!");
}
public void aMethod()
{
System.out.println("This is in the method in the abstract class");
}
}
Attempting to compile and run the code will cause
1 Compiler error - abstract classes cannot have constructors.
2 Compiler error - the method AbstractClass does not have a valid return type.
3 Compiler error - the class cannot be declared as abstract as it does not have any unimplemented methods.
4 No compiler error - the class is practically not an abstract class and can be instantiated.
5 No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the extended class will call the constructor of the abstract class (implicitly or explicitly).
6 No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the abstract class will never be called.
the ans is 5th. why? the abstract class should include abstract method,should not it?


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic