In order to access the members (i.e. instance variables and methods) of abstract classes, we have to follow the below steps -
1. Create a Sub Class for the Abstract Class
2. Create an object for Sub Class
3. Access the members of Abstract Class using the object of its Sub Class.
Lets implement this on Eclipse IDE -
1. Launch Eclipse IDE, open the existing Java Project 'Project 23' as shown below - (Project 23 is created in the previous post)
2. Comment the object creation statement of Abstract Class 'ClassOne' as shown below and observe that the error got resolved -
3. Create a sub class for 'ClassOne' Class and name the Sub Class as 'SubOne' as shown below -
4. Let the Class 'SubOne' inherit the Class 'ClassOne' by writing the below code - (i.e. make the 'SubOne' class the subclass of 'ClassOne' Class) -
5. Create an instance variable 'var' in the 'SubOne' Class as shown below -
6. Create an object of 'SubOne' subclass in the main( ) method of 'ClassTwo' Class as shown below -
7. Now access the one( ) method in the 'ClassOne' abstract class using the object of its sub class 'SubOne' as shown below -
8. Access the variable of 'SubOne' subclass using its object as shown below -
9. Now run the 'ClassTwo' class in which the main( ) method exists and observe that the output is displayed in the console as shown below -
- Though objects cant be created for accessing the members of Abstract Classes, we can create object for the sub class of the abstract class and access the members of Abstract Class using the object of its subclass.
- Also the subclass object can be used to access the members of itself.
Please comment below to feedback or ask questions.