Appearance
Lesson 6 — Object References & Inheritance
Inheritance models a generalization/specialization relationship.
text
Product
/ \
Book MovieA Book is a Product, but has additional Book-specific state/behavior.
Learning objectives
- declare reference variables with
REF TO; - understand static reference type vs actual object type;
- create a superclass;
- create subclasses with
INHERITING FROM; - use
PROTECTED SECTIONdeliberately; - call
super->constructor; - reuse inherited public behavior;
- decide whether inheritance fits an “is-a” relationship.
1. Create the superclass
Create:
text
ZCL_PRODUCT_BASE_###Do not mark it FINAL, because it must allow subclasses.
abap
CLASS zcl_product_base_### DEFINITION
PUBLIC
CREATE PUBLIC.
PUBLIC SECTION.
METHODS constructor
IMPORTING price TYPE decfloat34.
METHODS get_price
RETURNING VALUE(price) TYPE decfloat34.
PROTECTED SECTION.
DATA price TYPE decfloat34.
ENDCLASS.2. Why protected?
PROTECTED SECTION is visible inside:
text
the class itself
its subclassesbut not to arbitrary external callers.
Use protected state carefully; private state provides stronger encapsulation. Here it is useful to demonstrate subclass access.
3. Implement superclass constructor
abap
METHOD constructor.
IF price >= 0.
me->price = price.
ELSE.
me->price = 0.
ENDIF.
ENDMETHOD.4. Create Book subclass
Create:
text
ZCL_BOOK_###Definition:
abap
CLASS zcl_book_### DEFINITION
PUBLIC
INHERITING FROM zcl_product_base_###
FINAL
CREATE PUBLIC.Book-specific private state:
text
title
author5. Subclass constructor
abap
METHODS constructor
IMPORTING
price TYPE decfloat34
title TYPE string
author TYPE string.Implementation:
abap
METHOD constructor.
super->constructor( price = price ).
me->title = title.
me->author = author.
ENDMETHOD.super->constructor initializes the superclass portion of the object.
6. Create Movie subclass
Create:
text
ZCL_MOVIE_###It inherits Product price behavior and adds:
text
title
director
category7. REF TO
Reference variable:
abap
DATA product_ref TYPE REF TO zcl_product_base_###.It can point to a Product object or an object of a compatible subclass.
abap
product_ref = NEW zcl_book_###(
price = '15.00'
title = 'ABAP Basics'
author = 'A. Developer'
).8. Static type vs dynamic type
text
Reference variable static type:
REF TO ZCL_PRODUCT_BASE_###
Actual object dynamic type:
ZCL_BOOK_###Through the superclass-typed reference, only components visible through the static type can be called directly.
9. Inherited methods
abap
out->write( product_ref->get_price( ) ).get_price was defined in Product, but the Book object inherits it.
10. “Is-a” test
Inheritance is appropriate when the specialized object can meaningfully be used wherever the superclass is expected.
text
Book is a Product reasonable
Movie is a Product reasonable
Inventory is a Product noDo not use inheritance merely to reuse code between unrelated concepts.
11. Summary
You now know:
text
REF TO
static reference type
dynamic object type
superclass
subclass
INHERITING FROM
PROTECTED SECTION
super->constructor
inherited behaviorLesson 7 uses this hierarchy to introduce polymorphism and method redefinition.