Inheritance:-
- Inheritance is a process of acquiring the properties of other class.
- Inheritance provides reusability of the components.
- Only public and protected components can be inherited i.e private components cannot be inherited.
- The class which gives the properties is called as super class or Base Class or parent class.
- The class which takes the properties is called as Sub-Class or Derived-Class or Child-Class.
- Single Inheritance:
- Multiple Inheritance:
- Multilevel Inheritance:
Multiple Inheritance: A class derived from more than one super class.
(Note: ABAP doesn't support multilevel inheritance directly it can be implemented indirectly through the concept of interface.)
Multilevel Inheritance: A Class drived from another drived class.
Final Class:
final Class cannot be Inherited .
final is a keyword used as part of class definition to declare the local classes.
Example Program:
REPORT ZRDD_OOPS21.
class cycle definition.
public section.
methods : setcycle,
display.
protected section.
data : wheels type i,
brakes type i,
colour(20) type c.
endclass.
class cycle implementation.
method setcycle.
wheels = 2.
brakes = 2.
colour = 'green'.
endmethod.
method display.
write :/ wheels,brakes,colour.
endmethod.
endclass.
class scooter definition inheriting from
cycle.
public section.
methods setscooter.
endclass.
class scooter implementation.
method setscooter.
wheels = 2.
brakes = 4.
colour = 'red'.
endmethod.
endclass.
class car definition
inheriting from scooter.
public section.
methods setcar.
endclass.
class car implementation.
method setcar.
wheels = 4.
brakes = 5.
colour = 'cyan'.
endmethod.
endclass.
start-of-selection.
data ob1 type ref to cycle.
create object ob1.
write :/ 'CYCLE...'.
call method : ob1->setcycle,
ob1->display.
data ob2 type ref to scooter.
create object ob2.
write :/ 'SCOOTER...'.
call method : ob2->setscooter,
ob2->display.
data ob3 type ref to car.
create object ob3.
write :/ 'CAR...'.
call method : ob3->setcar,
ob3->display.
------------------------------------------------
Example 2:
REPORT ZRDD_OOPS26.
class abc definition.
public section.
methods m1 final.
endclass.
class abc implementation.
method m1.
write :/ 'inside m1 - super class'.
endmethod.
endclass.
class pqr definition inheriting from abc.
* public section.
* methods m1 redefinition.
endclass.
start-of-selection.
data ob type ref to pqr.
create object ob.
call method ob->m1.
public section.
methods : setcycle,
display.
protected section.
data : wheels type i,
brakes type i,
colour(20) type c.
endclass.
class cycle implementation.
method setcycle.
wheels = 2.
brakes = 2.
colour = 'green'.
endmethod.
method display.
write :/ wheels,brakes,colour.
endmethod.
endclass.
class scooter definition inheriting from
cycle.
public section.
methods setscooter.
endclass.
class scooter implementation.
method setscooter.
wheels = 2.
brakes = 4.
colour = 'red'.
endmethod.
endclass.
class car definition
inheriting from scooter.
public section.
methods setcar.
endclass.
class car implementation.
method setcar.
wheels = 4.
brakes = 5.
colour = 'cyan'.
endmethod.
endclass.
start-of-selection.
data ob1 type ref to cycle.
create object ob1.
write :/ 'CYCLE...'.
call method : ob1->setcycle,
ob1->display.
data ob2 type ref to scooter.
create object ob2.
write :/ 'SCOOTER...'.
call method : ob2->setscooter,
ob2->display.
data ob3 type ref to car.
create object ob3.
write :/ 'CAR...'.
call method : ob3->setcar,
ob3->display.
------------------------------------------------
Example 2:
REPORT ZRDD_OOPS26.
class abc definition.
public section.
methods m1 final.
endclass.
class abc implementation.
method m1.
write :/ 'inside m1 - super class'.
endmethod.
endclass.
class pqr definition inheriting from abc.
* public section.
* methods m1 redefinition.
endclass.
start-of-selection.
data ob type ref to pqr.
create object ob.
call method ob->m1.
No comments:
Post a Comment