189. Objects cant be created for Abstract Classes

Pre-requisite -

In Java, we cant access the method and variables of abstract classes by creating their objects.

Lets find out practically by trying to create an object for abstract class.

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a Java Project 'Project 23' as shown below -

2. Right click on the Java Project 'Project 23' and select 'New' -> 'Class' as shown below -

3. Select 'abstract' check box option in the displayed 'New Java Class' dialog as shown below -

4. Now enter class name as 'ClassOne' and click on 'Finish' button as shown below -

5. Observe that an abstract class is displayed as shown below -

6. Now create a method say one( ) inside the ClassOne Class as shown below -

7. Write a print statement inside the one( ) method as shown below -

8. Now create another Class 'ClassTwo' with main( ) method as shown below -

9. Try to create an object for 'ClassOne' abstract Class in the main( ) method as shown below -

10. View the error that is displayed while creating an object for an abstract class as shown below -

Hence we cannot create an instance (i.e. object) for an abstract Class. In order to access the members of any abstract class, we need to create a sub class for that abstract class, create object for the subclass and then access the members of the abstract class using the object of its subclass. 

Lets implement accessing the members of an abstract class using the object of its subclass in the next post.

Please comment below to feedback or ask questions.

Accessing the members of the Abstract Class using its Sub Class object will be explained in the next post.

No comments: