Friday, March 22, 2013

Child Class gets Access Public & Protected data-methods from Parent Class


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.
 
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:

12 comments:

eswar jhothi said...

i create this inheritance program, but i am getting SYNTAX error-
the type "PARENT" is unknown

SANDIP ROY said...

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.

eswar jhothi said...

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

simashree said...

Thank you for such guidance about astrology,Best Blog on
vasiyam in chennai

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

Kumar Basu said...

KIndly Explain about narrow Casting ..

sanayamohit said...

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!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-in-chennai/

suganya said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

Camelliacanan said...

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

Resma Biswal said...


Thank you for giving this best information. It’s a very nice topic
SAP Success Factor Online Access

gilledashivani said...

Great information. the information provided is of great use as i got to learn new things. If you are looking for real time training in SAP ABAP, kindly contact sieve software
Sieve software offers real time training by industry experts with live projects and placement support.
SAP ABAP TRAINING IN HYDERABAD