This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

abstract  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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
  • 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?


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!