I understand:
- Since Abstract class are nothing on its own i.e. vehicle that's why we want to create an object of an concrete implementation like Car,Bike etc.
Constructor of an Abstract class gets called during object chaining.
We can never directly create an object of Abstract class even if it contains a Constructor and all methods are implemented
why ? I am looking to understand from Compiler perspective, why Java forces these ?
Thanks
Because an abstract class is an incomplete class (incomplete in the sense it contains abstract methods without body and output) we cannot create an instance or object; the same way you say for an interface.
You CAN instantiate an abstract class. You only need to provide a concrete subclass.
You can't instantiate an interface or an abstract class because it would defy the object oriented model. Read more
Well actually you can - but only if you implement any methods that have been declared abstract or left out.
Added
The
IA
class instantiates an object of theI
abstract class and implements thenext
method that was missing from theI
class. You are actually creating an object of an anonymous that implements the implied abstract method.An abstract classs can NOT be instantiated by using new operator. Becuase an abstract may have abstract methods i.e. methods without any body (or implementation). Because an object can NOT have an abstract methods and JVM can NOT allocate memory of the abstract methods
very simple reason jvm playing to restrict us to instantiate abstract class and interface.
Assume compiler allow us to instantiate both ok.
So suppose my abstract class contains 5 abstract method means only method prototype no method body.
So as we know every method call creating a separate stack in jvm Java stack area That memory allocation happened based on method structure and after execute that stack is destroying right.
So if my method is not contains any body means how can jvm predict memory to allocate that method
Second if no body means no method execution so never it will destroy from your Java stack area there may be a chance u can get memoryout error.
So to consider these 2 case compiler restrict us to instantiate both interface and abstract class