• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract class  RSS feed

 
chun-ju Lin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me why abstract class can't produce instance object?
Thanks.
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are not intended to be directly instantiated, only inherited. When a class extends an abstract class, it agrees to provide all of the necessary implementation for the abstract class. If a class can be thought of as a blueprint, and an interface is a contract, then an abstract class is sorta like an unfinished blueprint, with a contract stating what needs to be done to finish the blueprint. Because a subclass will pass the IS-A test for an abstract class, it allows other code to refer to the abstract class, while the actual implementation may very from subclass to subclass. So, I always know that a subclass of abstract class X has a method called Y. I just don't know what Y does, but I can still reference the method and call it.
 
Ed Bicker
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree but remember, any method that you declare as abstract implies that the class must also be abstract. If you do not declare the class as abstract does the jvm "make" the class abstract if you declare a method abstract and not the class, or does it just throw the error and leave it at that?
 
Tony Jackson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class is not abstract and you declare a method as abstract, it won't compile.

from the compiler:

C:\a\Test.java:1: Test is not abstract and does not override abstract method abstractMethod() in Test
public class Test {
^
1 error

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