Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

abstract class

 
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.
 
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.
 
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?
 
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
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic