• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why abstract class cant create instance???

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract class is also a class. It also has default constructor.
Then why we cant able to create instance for Abstract class??

Thansk in advance,
Ramesh
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because, by definition, an abstract class is complete. How could you create an object from a class where a method is not defined? What would happen if you tried to call the method?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith had a finger-check where he meant to type "incomplete" and got "complete".

The whole point of Abstract classes is that you cannot make an instance. Why did the language designers think this was necessary? As Keith said it lets a class designer specify part of a class but leave other necessary parts for later. Many times an abstract class does part of a task for you in a generic way but forces you to override certain methods and provide other parts of the task customized for your particular task.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops I apologize. I typed too fast and didn't proofread. I meant incomplete.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, imagine if u have a rule saying: a person HAVE to be either male or female. Can't be both, can't be neither. With abstract class you can have such a design:

Abstract class Person

class Male extends Person

class Female extends Person

it means that you cant instantiate a sexless person. You have to instantiate a Male or a Female, not a Person. The concept of Abstract class allows you to enforce rules like that (note that the above is just example... dont take the design personally) and still maintain some polymorphism power on it.

Person p1 = new Male()
Person p2 = new Female()

p1.dance() -> does michael jackson dance, or something
p2.dance() -> does demi moore striptease dance, or something
 
Ramesh Shanmugam
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith, James and Zenikko
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic