2008 Feb 07 6:31 AM
Hello Gurus,
1. why the interface methods are always public ?
2. why the protected section is not in interface?
3. why the inherited protected methods act as private in sub class , in this case child-sub class also access the methods of the super class methods whether it wl access the protected methods of super class but methods wl act private in sub class.
the inheritance tree wl look like this
Super class--> sub class --> child-sub class.
Regards,
Ravi.
2008 Feb 07 6:49 AM
Interfaces are particularly used for inheritance purposes only. They cannot be regarded as standalone classes.
1. An interface does not have any visibility sections. They do not need any, as they have methods which have to be implemented in the subclasses. So, all the methods are visible to any body who needs to call them. However in the implemented subclasses, you can inherit the methods of interface publicly or privately or in protected mode. Eg.
INTERFACE lif_document.
DATA: author TYPE REF TO lcl_author.
declaration
METHODS: print,
display.
ENDINTERFACE.
CLASS lcl_text_document DEFINITION.
PUBLIC SECTION.
INTERFACES lif_document.
METHODS: display.
ENDCLASS.
In the class lcl_text, the interface lif_document is inherited publicly.
2. As said before, it does not make sense to have any visibility sections in the interfaces themselves, however, in the subclasses, you can have private, protected, or public sections when you define methods of the interfaces.
3. The child will always access the methods (if not over-ridden itself) of the immediate parent. If the method is not over-ridden in the immediate parent, then it will access the method implementation of the grandparent.
So, in the case you have mentioned, child-subclass will access subclass methods, but if not implemented in subclass, then it will look into the implementation of the Superclass.
2008 Feb 07 6:49 AM
Interfaces are particularly used for inheritance purposes only. They cannot be regarded as standalone classes.
1. An interface does not have any visibility sections. They do not need any, as they have methods which have to be implemented in the subclasses. So, all the methods are visible to any body who needs to call them. However in the implemented subclasses, you can inherit the methods of interface publicly or privately or in protected mode. Eg.
INTERFACE lif_document.
DATA: author TYPE REF TO lcl_author.
declaration
METHODS: print,
display.
ENDINTERFACE.
CLASS lcl_text_document DEFINITION.
PUBLIC SECTION.
INTERFACES lif_document.
METHODS: display.
ENDCLASS.
In the class lcl_text, the interface lif_document is inherited publicly.
2. As said before, it does not make sense to have any visibility sections in the interfaces themselves, however, in the subclasses, you can have private, protected, or public sections when you define methods of the interfaces.
3. The child will always access the methods (if not over-ridden itself) of the immediate parent. If the method is not over-ridden in the immediate parent, then it will access the method implementation of the grandparent.
So, in the case you have mentioned, child-subclass will access subclass methods, but if not implemented in subclass, then it will look into the implementation of the Superclass.
2008 Feb 07 7:20 AM
please follow the question numbers
1. According to ur answer "you can inherit the methods of interface publicly or privately or in protected mode"
a public section methods is crushed to private method in implementing class.
iam creating the object to the class and assign to the Ref object of interface.
we cant access the method , wt is the need ?
3. Super class is having a protected method , this method is redefined in sub class , whether child-sub class can assess that method ?
(protected methods wl act as private in sub class so child-sub class cant access because it is private )
if above is wrong please justify.
2008 Feb 07 8:27 AM
1. Sorry, the interfaces can be inherited only Publicly... sorry for the previous glitch.
2. While redefining any method of superclass (protected or public), you are not allowed to change the visibility of the method in the subclass (i.e. if a method is protected in superclass, it will be protected in subclass, and same applies for public as well).
Related to your example, the child-subclass can access that method. Check this code for a report program.
CLASS super1 DEFINITION.
PROTECTED SECTION.
METHODS: area.
ENDCLASS. "super DEFINITION
CLASS super1 IMPLEMENTATION.
METHOD area.
WRITE:/ 'super1'.
ENDMETHOD. "area
ENDCLASS. "super IMPLEMENTATION
CLASS sub DEFINITION INHERITING FROM super1.
PROTECTED SECTION.
METHODS: area REDEFINITION.
ENDCLASS. "sub DEFINITION
CLASS sub IMPLEMENTATION.
METHOD area.
WRITE: / 'sub'.
ENDMETHOD. "area
ENDCLASS. "sub IMPLEMENTATION
CLASS childsub DEFINITION INHERITING FROM sub.
PUBLIC SECTION.
METHODS area_childsub.
ENDCLASS. "childsub DEFINITION
CLASS childsub IMPLEMENTATION.
METHOD area_childsub.
area( ).
ENDMETHOD. "area_childsub
ENDCLASS. "childsub IMPLEMENTATION
START-OF-SELECTION.
DATA: cs TYPE REF TO childsub.
CREATE OBJECT cs.
cs->area_childsub( ).
-
The implementation of the childsub can call the method 'area' which was redefined in protected section of sub. The output will be "sub".
Edited by: Varsha Chachadi on Feb 7, 2008 9:35 AM
2008 Feb 07 10:05 AM
once again shall i redefine the area method in child-sub class , whether that is possible ?
2008 Feb 07 10:33 AM
Yes, you can do that as well. By using the following code in the childsubclass. What you are effectively achieving here is called multilevel inheritance. Note that you have to define the method area in the PROTECTED SECTION only because in super and sub, you have defined it there.
CLASS childsub DEFINITION INHERITING FROM sub.
PUBLIC SECTION.
METHODS area_childsub.
PROTECTED SECTION.
METHODS area REDEFINITION.
ENDCLASS. "childsub DEFINITION
CLASS childsub IMPLEMENTATION.
METHOD area_childsub.
area( ).
ENDMETHOD. "area_childsub
METHOD area.
WRITE: / 'childsub'.
ENDMETHOD. "area
ENDCLASS. "childsub IMPLEMENTATION
START-OF-SELECTION.
DATA: cs TYPE REF TO childsub.
CREATE OBJECT cs.
cs->area_childsub( ).
-
Now, the output will be 'childsub'.
2008 Feb 07 10:40 AM