Interface:-
Example 1-
Example 2:
- An interface is pure abstract class i.e by default all the methods.
- All the component of the interface by default public.
- we cannot instantiated the interface because they are not implemented.
- A Class has to implement the interface . This class we called the Implementation class.
- The class must have to implement all the methods of the interface otherwise the class should be declared as abstract.
- whenever the class has to implement the interface the class must redeclare the interface by using interfaces keyword.
- Interface must be implemented on public section.
- A class can implemented any no of the interface i.e a Class can make use of components of multiple interface which is nothing but multiple inheritance.
- whenever the interface components are referred outside the interface , the component must be prefixed with the name of the interfaces.
Example 1-
REPORT Z7PM_OOPS32.
interface rectangle.
constants : length type i value 10,
breadth type i value 5.
methods : area,
perimeter.
endinterface.
interface square.
constants side type i value 5.
methods : area,
perimeter.
endinterface.
class abc definition.
public section.
interfaces : rectangle,
square.
data r type i.
endclass.
class abc implementation.
method rectangle~area.
r = rectangle~length * rectangle~breadth.
write :/ 'Area of rectangle is ',r.
endmethod.
method rectangle~perimeter.
r = 2 * ( rectangle~length + rectangle~breadth ).
write :/ 'perimeter of rectangle is ',r.
endmethod.
method square~area.
r = square~side * square~side.
write :/ 'Area of square is ',r.
endmethod.
method square~perimeter.
r = 4 * square~side.
write :/ 'perimeter of square is ',r.
endmethod.
endclass.
start-of-selection.
data r type ref to rectangle.
data s type ref to square.
data ob type ref to abc.
create object ob.
call method : ob->rectangle~area,
ob->rectangle~perimeter,
ob->square~area,
ob->square~perimeter.
write :/ 'RECTANGLE --> ABC'.
r = ob.
call method : r->area,
r->perimeter.
write :/ 'SQUARE --> ABC'.
s = ob.
call method : s->area,
s->perimeter.
interface rectangle.
constants : length type i value 10,
breadth type i value 5.
methods : area,
perimeter.
endinterface.
interface square.
constants side type i value 5.
methods : area,
perimeter.
endinterface.
class abc definition.
public section.
interfaces : rectangle,
square.
data r type i.
endclass.
class abc implementation.
method rectangle~area.
r = rectangle~length * rectangle~breadth.
write :/ 'Area of rectangle is ',r.
endmethod.
method rectangle~perimeter.
r = 2 * ( rectangle~length + rectangle~breadth ).
write :/ 'perimeter of rectangle is ',r.
endmethod.
method square~area.
r = square~side * square~side.
write :/ 'Area of square is ',r.
endmethod.
method square~perimeter.
r = 4 * square~side.
write :/ 'perimeter of square is ',r.
endmethod.
endclass.
start-of-selection.
data r type ref to rectangle.
data s type ref to square.
data ob type ref to abc.
create object ob.
call method : ob->rectangle~area,
ob->rectangle~perimeter,
ob->square~area,
ob->square~perimeter.
write :/ 'RECTANGLE --> ABC'.
r = ob.
call method : r->area,
r->perimeter.
write :/ 'SQUARE --> ABC'.
s = ob.
call method : s->area,
s->perimeter.
Example 2:
REPORT ZRDD_OOPS33.
interface abc.
constants : x type i value 10,
y type i value 5.
methods m1.
endinterface.
class pqr definition.
public section.
interfaces abc.
aliases : a1 for abc~m1.
protected section.
aliases : a2 for abc~x,
a3 for abc~y.
endclass.
class pqr implementation.
method a1.
data r type i.
r = a2 + a3.
write :/ 'sum is ',r.
endmethod.
endclass.
start-of-selection.
data ob type ref to pqr.
create object ob.
call method : ob->a1.
interface abc.
constants : x type i value 10,
y type i value 5.
methods m1.
endinterface.
class pqr definition.
public section.
interfaces abc.
aliases : a1 for abc~m1.
protected section.
aliases : a2 for abc~x,
a3 for abc~y.
endclass.
class pqr implementation.
method a1.
data r type i.
r = a2 + a3.
write :/ 'sum is ',r.
endmethod.
endclass.
start-of-selection.
data ob type ref to pqr.
create object ob.
call method : ob->a1.
No comments:
Post a Comment