Inheritance is one of a concepts of Object Oriented programming where we have a parent class and a child class. Here the child class has the access of all of its parent class attributes and methods declared in Public and Protected sections only.
We have created a program where we define a Parent class. Under public section we declare an attribute and a method. Similarly we have created attribute & method in Protected section also. Now in the implementation we set some function for both of the methods. Then we have created the child class Inheriting from parent class. We declare attribute & method in public section of the child class. Then we implement the class with having the functionality of the child class method. In the child class method we call the methods of parent class. After that in the start-of-selection we create an object for the child class. Here we are not creating object for parent class. Then we call the method of child class and we get all the data declared & implemented by parent class. Hence the child class has the access of all declared in public and protected section of Parent class.
REPORT zsr_test NO STANDARD PAGE
HEADING.
*----------------------------------------------------------------------*
* CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION.
PUBLIC SECTION.
"Accessible to child class
DATA v_parent_pub TYPE char40.
METHODS m_parent_pub.
PROTECTED SECTION.
"Accessible to child class
DATA v_parent_pro TYPE char40.
METHODS m_parent_pro.
PRIVATE SECTION.
"Not Accessible to child class
DATA v_parent_pri TYPE char40.
METHODS m_parent_pri.
ENDCLASS. "cl_parent DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
METHOD m_parent_pub.
v_parent_pub = 'Parent Class Public data'.
ENDMETHOD. "m_parent_pub
METHOD m_parent_pro.
v_parent_pro = 'Parent Class Protected data'.
ENDMETHOD. "m_parent_pro
METHOD m_parent_pri.
v_parent_pri = 'Parent Class Private data'.
ENDMETHOD. "m_parent_pri
ENDCLASS. "cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
PUBLIC SECTION.
DATA v_child_pub TYPE char40.
METHODS m_child_pub.
ENDCLASS. "cl_child DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
METHOD m_child_pub.
v_child_pub = 'Child Class Public data'.
"Child can access public & protected methods
CALL METHOD: m_parent_pub,
m_parent_pro.
"Child can access public & protected data
WRITE: / v_parent_pub,
/ v_parent_pro,
/ v_child_pub.
ENDMETHOD. "m_child_pub
ENDCLASS. "cl_child IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cl_child.
CREATE OBJECT obj.
CALL METHOD obj->m_child_pub.
*----------------------------------------------------------------------*
* CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION.
PUBLIC SECTION.
"Accessible to child class
DATA v_parent_pub TYPE char40.
METHODS m_parent_pub.
PROTECTED SECTION.
"Accessible to child class
DATA v_parent_pro TYPE char40.
METHODS m_parent_pro.
PRIVATE SECTION.
"Not Accessible to child class
DATA v_parent_pri TYPE char40.
METHODS m_parent_pri.
ENDCLASS. "cl_parent DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
METHOD m_parent_pub.
v_parent_pub = 'Parent Class Public data'.
ENDMETHOD. "m_parent_pub
METHOD m_parent_pro.
v_parent_pro = 'Parent Class Protected data'.
ENDMETHOD. "m_parent_pro
METHOD m_parent_pri.
v_parent_pri = 'Parent Class Private data'.
ENDMETHOD. "m_parent_pri
ENDCLASS. "cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
PUBLIC SECTION.
DATA v_child_pub TYPE char40.
METHODS m_child_pub.
ENDCLASS. "cl_child DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
METHOD m_child_pub.
v_child_pub = 'Child Class Public data'.
"Child can access public & protected methods
CALL METHOD: m_parent_pub,
m_parent_pro.
"Child can access public & protected data
WRITE: / v_parent_pub,
/ v_parent_pro,
/ v_child_pub.
ENDMETHOD. "m_child_pub
ENDCLASS. "cl_child IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cl_child.
CREATE OBJECT obj.
CALL METHOD obj->m_child_pub.
Now we have some step by analysis in debugging mode. At first we set the break point at calling method and controller will enter into the method. Here we can see that the child class have access to all public & protected data of parent class. It doesn't have any access to it's private data.
So it populates all of those accordingly.
Below is the output:
7 comments:
i create this inheritance program, but i am getting SYNTAX error-
the type "PARENT" is unknown
You must have the PARENT class definition and implementation. I have successfully compiled this program and have this output. That's why I have posted it.
I include parent definition and implementation also if you have any demo or vidoe or slide for creating simple inheritance please post it or give link
Thank you for such guidance about astrology,Best Blog on
vasiyam in chennai
KIndly Explain about narrow Casting ..
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..
http://thecreatingexperts.com/sap-successfactors-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!!!!!!
Hi I read your post very carefully and I think you are right that a well written post should be at least a 100 words and should capture the essence of your blog, book or article.
SAP HR Training in Chennai
SAP SD Training in Chennai
SAP Basis Training in Chennai
Post a Comment