Inheritance is a general property of OOPs. Class defined by Inheriting From statement denotes that this class is a sub class of a Super class. A class can have only a single super class where as it can have multiple sub classes. Sub class gets all of the Public and Protected components with visibility property of the super class. Sub class can add components as well in its declaration part. Here the super class will not access any of sub class's components.
In the following program the Child class can have all the access of Public and Protected data and methods of its Parent class. Thus if a Parent class can have only the addition method then Child class will have that functionality for Inheritance property of OOPs. Here Child class can add multiply method as unique. Hence the Child has now two functionality Addition and Multiplication.
The important point is that the Parent class will not have any access of this Multiplication. Below is a program of Inheritance.
Now again we are going to the sub class of parent class and that is child class. From there we shall have access of public & protected components of super_cls & parent class. Both are hierarchical super classes of the child sub class.
The output is as follows:
In the following program the Child class can have all the access of Public and Protected data and methods of its Parent class. Thus if a Parent class can have only the addition method then Child class will have that functionality for Inheritance property of OOPs. Here Child class can add multiply method as unique. Hence the Child has now two functionality Addition and Multiplication.
The important point is that the Parent class will not have any access of this Multiplication. Below is a program of Inheritance.
REPORT zsr_test NO STANDARD PAGE
HEADING.
*----------------------------------------------------------------------*
* CLASS super_cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS super_cls DEFINITION.
PUBLIC SECTION.
DATA v_super_pub TYPE char40 VALUE 'Super public data'.
METHODS m_super.
PROTECTED SECTION.
DATA v_super_pro TYPE char40 VALUE 'Super protected data'.
PRIVATE SECTION.
DATA v_super_pri TYPE char40 VALUE 'Super private data'.
ENDCLASS. "super_cls DEFINITION
*----------------------------------------------------------------------*
* CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION INHERITING FROM super_cls.
PUBLIC SECTION.
DATA v_parent_pub TYPE char40 VALUE 'Parent public data'.
METHODS m_parent.
PROTECTED SECTION.
DATA v_parent_pro TYPE char40 VALUE 'Parent protected data'.
PRIVATE SECTION.
DATA v_parent_pri TYPE char40 VALUE 'Parent private data'.
ENDCLASS. "parent DEFINITION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
PUBLIC SECTION.
DATA v_child_pub TYPE char40 VALUE 'Child public data'.
METHODS m_child.
PROTECTED SECTION.
DATA v_child_pro TYPE char40 VALUE 'Child protected data'.
PRIVATE SECTION.
DATA v_child_pri TYPE char40 VALUE 'Child private data'.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
* CLASS super_cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS super_cls IMPLEMENTATION.
METHOD m_super.
WRITE: / 'Super Class' COLOR 3,
/ v_super_pub,
/ v_super_pro,
/ v_super_pri.
SKIP.
ENDMETHOD. "m_super
ENDCLASS. "super_cls IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
METHOD m_parent.
v_super_pub = 'Super public data from Parent'.
v_super_pro = 'Super protected data from Parent'.
WRITE: / 'Parent Class' COLOR 3,
/ v_super_pub,
/ v_super_pro,
/ '=====================',
/ v_parent_pub,
/ v_parent_pro,
/ v_parent_pri.
SKIP.
ENDMETHOD. "m_parent
ENDCLASS. "parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
METHOD m_child.
v_super_pub = 'Super public data from Child'.
v_super_pro = 'Super protected data from Child'.
v_parent_pub = 'Parent public data from Child'.
v_parent_pro = 'Parent protected data from Child'.
WRITE: / 'Child Class' COLOR 3,
/ v_super_pub,
/ v_super_pro,
/ '======================',
/ v_parent_pub,
/ v_parent_pro,
/ '======================',
/ v_child_pub,
/ v_child_pro,
/ v_child_pri.
SKIP.
ENDMETHOD. "m_child
ENDCLASS. "child IMPLEMENTATION
START-OF-SELECTION.
DATA: obj_super TYPE REF TO super_cls,
obj_parent TYPE REF TO parent,
obj_child TYPE REF TO child.
CREATE OBJECT: obj_super, obj_parent, obj_child.
CALL METHOD: obj_super->m_super,
obj_parent->m_parent,
obj_child->m_child.
*----------------------------------------------------------------------*
* CLASS super_cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS super_cls DEFINITION.
PUBLIC SECTION.
DATA v_super_pub TYPE char40 VALUE 'Super public data'.
METHODS m_super.
PROTECTED SECTION.
DATA v_super_pro TYPE char40 VALUE 'Super protected data'.
PRIVATE SECTION.
DATA v_super_pri TYPE char40 VALUE 'Super private data'.
ENDCLASS. "super_cls DEFINITION
*----------------------------------------------------------------------*
* CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION INHERITING FROM super_cls.
PUBLIC SECTION.
DATA v_parent_pub TYPE char40 VALUE 'Parent public data'.
METHODS m_parent.
PROTECTED SECTION.
DATA v_parent_pro TYPE char40 VALUE 'Parent protected data'.
PRIVATE SECTION.
DATA v_parent_pri TYPE char40 VALUE 'Parent private data'.
ENDCLASS. "parent DEFINITION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
PUBLIC SECTION.
DATA v_child_pub TYPE char40 VALUE 'Child public data'.
METHODS m_child.
PROTECTED SECTION.
DATA v_child_pro TYPE char40 VALUE 'Child protected data'.
PRIVATE SECTION.
DATA v_child_pri TYPE char40 VALUE 'Child private data'.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
* CLASS super_cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS super_cls IMPLEMENTATION.
METHOD m_super.
WRITE: / 'Super Class' COLOR 3,
/ v_super_pub,
/ v_super_pro,
/ v_super_pri.
SKIP.
ENDMETHOD. "m_super
ENDCLASS. "super_cls IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
METHOD m_parent.
v_super_pub = 'Super public data from Parent'.
v_super_pro = 'Super protected data from Parent'.
WRITE: / 'Parent Class' COLOR 3,
/ v_super_pub,
/ v_super_pro,
/ '=====================',
/ v_parent_pub,
/ v_parent_pro,
/ v_parent_pri.
SKIP.
ENDMETHOD. "m_parent
ENDCLASS. "parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
METHOD m_child.
v_super_pub = 'Super public data from Child'.
v_super_pro = 'Super protected data from Child'.
v_parent_pub = 'Parent public data from Child'.
v_parent_pro = 'Parent protected data from Child'.
WRITE: / 'Child Class' COLOR 3,
/ v_super_pub,
/ v_super_pro,
/ '======================',
/ v_parent_pub,
/ v_parent_pro,
/ '======================',
/ v_child_pub,
/ v_child_pro,
/ v_child_pri.
SKIP.
ENDMETHOD. "m_child
ENDCLASS. "child IMPLEMENTATION
START-OF-SELECTION.
DATA: obj_super TYPE REF TO super_cls,
obj_parent TYPE REF TO parent,
obj_child TYPE REF TO child.
CREATE OBJECT: obj_super, obj_parent, obj_child.
CALL METHOD: obj_super->m_super,
obj_parent->m_parent,
obj_child->m_child.
Now we go to debug mode and analysis the behavior of inheritance. The super class doesn't have access to its sub classes. Hence we can't see the data from sub classes of the SUPER_CLS class.
Now we are going to parent class which is sub class of super_cls class. From here the public & protected components of super class are available. No private components will be available from the outside of that class. We can modify public & protected components of super class from the sub class.
Now again we are going to the sub class of parent class and that is child class. From there we shall have access of public & protected components of super_cls & parent class. Both are hierarchical super classes of the child sub class.
3 comments:
Great explanation in simple format, everybody can understand.
SAP Success Factors Real Time Hands on Training in Chennai...
Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..SAP Success Factors Training in Chennai
If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286
Both Classroom/Online Training is Available!!!!!!
Post a Comment