Google+

Like Us On Facebook


Total Pageviews

192. Defining objects for Abstract Classes







Pre-requisite -


As explained in the above post under 'Pre-requisite' section, we cannot create an objects for Abstract Classes.

But we can just define them as shown below -

Assuming Class 'AbstractClass' is an abstract class.

AbstractClass object_name;    //Defining the object is possible for Abstract Classes

But you cannot assign the Abstract Class object reference to the above defined variable object_name

object_name = new AbstractClass( );  //This will give you a compiler error.


Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, Create a new Java Project 'Project 25' as shown below -


2. Create an Abstract Class 'ClassOne' as shown below -



3. Create another Class 'ClassTwo' with main( ) method to define object for the Abstract Class as shown below -


4. Define an object variable for the Abstract Class as shown below and observe that no errors are displayed -


Hence we can define a variable to store any reference for an abstract class.But now lets find out whether we can assign the reference of  'ClassOne' abstract class to the above defined abstract class type defined variable 'object1' by following the below steps -

5. Assign the abstract class ClassOne's object reference to the above defined abstract class type defined variable 'object1' as shown below -


6. View the error message displayed in the above statement as shown below -


Its very clear that, we cannot assign the abstract class reference to any Class type variable.

Hence  we can define object variables for the Abstract Classes but we cant assign the object reference of the abstract class to it.





Please comment below to feedback or ask questions.

Defining objects for Abstract Class for accessing its abstract methods will be explained in the next post.








No comments:

Blog Index