Abstract Class
Introduction:
- We will cover the following basic concepts:
- Basics of Abstract Class.
- Example Class to understand practical implications.
- Example program to implement the methods of the class.
- Why Abstract Class?
Lets Start
1. First let us compare Normal Class and Abstract Class.
- Below comparison will give us a basic understanding of an abstract class.
2. Let us create an Abstract Class example to understand the practical implications.
- Create an Abstract Class as shown below.
- LINE 3: To make a class abstract class we need to add the keyword ABSTRACT in the definition section.
- LINE 6:Let us define an abstract method using the same keyword as above.
- METHODS:ABSTRACT_METHOD ABSTRACT.
- LINE7: Define a non-abstract method.
- METHODS: NORMAL_METHOD.
- LINE8:Lets try to define a static abstract method.
- CLASS-METHODS: STATIC_ABSTRACT_METHOD ABSTRACT.
- Interestingly, we cannot define a static method as an abstract method.
- This is because static methods cannot be redefined in their sub-class.
- And abstract methods can only be implemented in their parent's sub-class.
- LINE9:Interestingly we can however define a non-abstract static method as shown below.
- CLASS-METHODS: STATIC_NORMAL_METHOD.
- This is because abstract classes can even have non-abstract methods.
- And the defined non-abstract methods can be implemented in the same class.
- We will learn about this in our current example.
- Now let us create the implementation part of the abstract class.
- We have to implement both non-abstract methods as shown below.
- And we cannot implement Abstract methods in the parent class.
- Now let us implement the created abstract methods in the Sub Class.
- LINE6: We will redefine the abstract method.
- METHODS: ABSTRACT_METHOD REDEFINITION.
- LINE7: As we know we cannot redefine a static method, even though it is the method from the Parent Class.
- LINE 15:In the implementation section, we will implement our abstract method.
- Let us create an executable program.
- LINE7:DATA(LR_ABSTRACT) = NEW ZCLASS_ABSTRACT( ).
- Here we will try to instantiate the abstract class.
- We will get a syntax error stating that we cannot create an instance for an abstract class.
- LINE8:DATA(LR_ABSTRACT_SUB) = NEW ZCLASS_ABSTRACT_SUB( ).
- Now let us create an instance for the sub-class.
- LINE10: ZCLASS_ABSTRACT=>STATIC_NORMAL_METHOD( ).
- We will call the non-abstract static method of the abstract class.
- And interestingly we will be able to call it without any issues, just like any other PUBLIC static method.
- LINE13:LR_ABSTRACT_SUB->NORMAL_METHOD( ).
- This is the method from the abstract class, we will directly call it using the subclass of the abstract class without even REDEFINING it.
- LINE16:LR_ABSTRACT_SUB->ABSTRACT_METHOD( ).
- We will REDEFINE the abstract method in the subclass of the abstract class.
- And then we will call it as shown below.
- Output is shown below.
4. Alright then, why do we need an Abstract Class?
- Abstract class can act as an interface.
- As we know, the interface components can be reused in any class just by defining them in the implementing class.
- Similarly, we can re-use the components of the abstract class in its subclasses.
- The extra advantage of using an abstract class as an interface is that the abstract class provides the advantages of both INHERITANCE as well as an INTERFACE.
- That means, we can access both the Public and Protected components of the abstract class as well as can use its abstract components just like interface methods with the definition already available.
- And another advantage is unlike INTERFACE abstract classes give access to any implementation of non-abstract methods and non-abstract components of public and protected visibilities.
- So multiple classes related to the same project can utilize these unique features.
Comments
Post a Comment