Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Plzz explain

Former Member
0 Kudos

Hi gurus,

Can u explain about friend class, inheritance and persistant class.

4 REPLIES 4

Former Member
0 Kudos

Hi,

using friend class u can access the private data or methods.

inheritance is same as oops concept u can acess the protected data.

Persistent class

A special class, the attributes of which are linked to database tables via object-relational mapping. Since Release 6.10 they can be created using the Mapping

http://help.sap.com/saphelp_nw04/helpdata/en/f5/a36828bc6911d4b2e80050dadfb92b/frameset.htm

http://help.sap.com/saphelp_47x200/helpdata/en/ab/9d0a3ad259cd58e10000000a11402f/frameset.htm

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

Reward if usefull

Former Member
0 Kudos

Hi Shyam,

Friend Concepts

In rare cases, classes have to work together so closely that they need access to their protected and private components. To avoid making these components available to all users, there is the concept of friendship between classes.

To do this you use the FRIENDS additions to the CLASS statement, in which all classes and interfaces that are to be provided friendship are listed

In principle, providing friendship is one-sided: A class providing friendship is not automatically a friend of its friends. If a class providing friendship wants to access the non-public components of a friend, this friend has to explicitly provide friendship to it.

Classes that inherit from friends and interfaces that contain a friend as a component interface also become friends. However, providing friendship, unlike the attribute of being a friend, is not inherited. A friend of a superclass is therefore not automatically a friend of its subclasses.

yes we can declare in this 2 ways

CLASS A DEFINTION CREATE PRIVATE FRIEND CLASS B.

CLASS A DEFINTION FRIEND CLASS B.

Check this link for all details

http://help.sap.com/saphelp_nw2004s/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

Regards,

Satish

Former Member
0 Kudos

Hi

Friend Concepts


In rare cases, classes have to work together so closely that they need access to their protected and private components. To avoid making these components available to all users, there is the concept of friendship between classes.

To do this you use the FRIENDS additions to the CLASS statement, in which all classes and interfaces that are to be provided friendship are listed

In principle, providing friendship is one-sided: A class providing friendship is not automatically a friend of its friends. If a class providing friendship wants to access the non-public components of a friend, this friend has to explicitly provide friendship to it.
Classes that inherit from friends and interfaces that contain a friend as a component interface also become friends. However, providing friendship, unlike the attribute of being a friend, is not inherited. A friend of a superclass is therefore not automatically a friend of its subclasses.


yes we can declare in this 2 ways
CLASS A DEFINTION CREATE PRIVATE FRIEND CLASS B.
CLASS A DEFINTION FRIEND CLASS B.

"Inheritance




Inheritance allows you to derive a new class from an existing class. You do this using the INHERITING FROM addition in the


"Redefining Methods


All subclasses contain the components of all classes between themselves and the root node in an inheritance tree. The visibility of a component cannot be changed. However, you can use the REDEFINITION addition in the METHODS statement to redefine an inherited public or protected instance method in a subclass and make its function more specialized. When you redefine a method, you cannot change its interface. The method retains the same name and interface, but has a new implementation.

The method declaration and implementation in the superclass is not affected when you redefine the method in a subclass. The implementation of the redefinition in the subclass obscures the original implementation in the superclass.

Any reference that points to an object of the subclass uses the redefined method, even if the reference was defined with reference to the superclass. This particularly applies to the self-reference ME->. If, for example, a superclass method M1 contains a call CALL METHOD M2, and M2 is redefined in a subclass, calling M1 from an instance of the superclass will cause the original method M2 to be called, and calling M1 from an instance of the subclass will cause the redefined method M2 to be called.

Within a redefine method, you can use the pseudoreference SUPER-> to access the obscured method. This enables you to use the existing function of the method in the superclass without having to recode it in the subclass.

"Abstract and Final Methods and Classes

The ABSTRACT and FINALadditions to the METHODS and CLASS statements allow you to define abstract and final methods or classes.

An abstract method is defined in an abstract class and cannot be implemented in that class. Instead, it is implemented in a subclass of the class. Abstract classes cannot be instantiated.

A final method cannot be redefined in a subclass. Final classes cannot have subclasses. They conclude an inheritance tree.

References to Subclasses and Polymorphism

Reference variables defined with reference to a superclass or an interface defined with reference to it can also contain references to any of its subclasses. Since subclasses contain all of the components of all of their superclasses, and given that the interfaces of methods cannot be changed, a reference variable defined with reference to a superclass or an interface implemented by a superclass can contain references to instances of any of its subclasses. In particular, you can define the target variable with reference to the generic class OBJECT.

When you create an object using the CREATE OBJECT statement and a reference variable typed with reference to a subclass, you can use the TYPE addition to create an instance of a subclass, to which the reference in the reference variable will then point.

A static user can use a reference variable to address the components visible to it in the superclass to which the reference variable refers. However, it cannot address any specialization implemented in the subclass. If you use a dynamic method call, you can address all components of the class.

If you redefine an instance method in one or more subclasses, you can use a single reference variable to call different implementations of the method, depending on the position in the inheritance tree at which the referenced object occurs. This concept that different classes can have the same interface and therefore be addressed using reference variables with a single type is called polymorphism.

Namespace for Components

Subclasses contain all of the components of all of their superclasses within the inheritance tree. Of these components, only the public and protected ones are visible. All public and protected components within an inheritance tree belong to the same namespace, and consequently must have unique names. The names of private components, on the other hand, must only be unique within their class.

When you redefine methods, the new implementation of the method obscures the method of the superclass with the same name. However, the new definition replaces the previous method implementation, so the name is still unique. You can use the pseudoreference SUPER-> to access a method definition in a superclass that has been obscured by a redefinition in a subclass.

"Inheritance and Static Attributes

Like all components, static attributes only exist once in each inheritance tree. A subclass can access the public and protected static attributes of all of its superclasses. Conversely, a superclass shares its public and protected static attributes with all of its subclasses. In terms of inheritance, static attributes are not assigned to a single class, but to a part of the inheritance tree. You can change them from outside the class using the class component selector with any class name, or within any class in which they are shared. They are visible in all classes in the inheritance tree.

When you address a static attribute that belongs to part of an inheritance tree, you always address the class in which the attribute is declared, irrespective of the class you specify in the class selector. This is particularly important when you call the static constructors of classes in inheritance. Static constructors are executed the first time you address a class. If you address a static attribute declared in a superclass using the class name of a subclass, only the static constructor of the superclass is executed.

"Inheritance and Constructors

There are special rules governing constructors in inheritance.

"Instance Constructors

Every class has an instance constructor called CONSTRUCTOR. This is an exception to the rule that states that component names within an inheritance tree must be unique. However, the instance constructors of the various classes in an inheritance tree are fully independent of one another. You cannot redefine the instance constructor of a superclass in a subclass, neither can you call one specifically using the statement CALL METHOD CONSTRUCTOR. Consequently, no naming conflicts can occur.

The instance constructor of a class is called by the system when you instantiate the class using CREATE OBJECT. Since a subclass contains all of the visible attributes of its superclasses, which can also be set by instance constructors, the instance constructor of a subclass has to ensure that the instance constructors of all of its superclasses are also called. To do this, the instance constructor of each subclass must contain a CALL METHOD SUPER->CONSTRUCTOR statement. The only exception to this rule are direct subclasses of the root node OBJECT.

In superclasses without an explicitly-defined instance constructor, the implicit instance constructor is called. This automatically ensures that the instance constructor of the immediate superclass is called.

When you call an instance constructor, you must supply values for all of its non-optional interface parameters. There are various ways of doing this:

· Using CREATE OBJECT

If the class that you are instantiating has an instance constructor with an interface, you must pass values to it using EXPORTING.

If the class that you are instantiating has an instance constructor without an interface, you do not pass any parameters.

If the class you are instantiating does not have an explicit instance constructor, you must look in the inheritance tree for the next-highest superclass with an explicit instance constructor. If this has an interface, you must supply values using EXPORTING. Otherwise, you do not have to pass any values.

· Using CALL METHOD SUPER->CONSTRUCTOR

If the direct superclass has an instance constructor with an interface, you must pass values to it using EXPORTING.

If the direct superclass has an instance constructor without an interface, you do not pass any parameters.

If the direct superclass does not have an explicit instance constructor, you must look in the inheritance tree for the next-highest superclass with an explicit instance constructor. If this has an interface, you must supply values using EXPORTING. Otherwise, you do not have to pass any values.

In both CREATE OBJECT and CALL METHOD SUPER->CONSTRUCTOR, you must look at the next-available explicit instance constructor and, if it has an interface, pass values to it. The same applies to exception handling for instance constructors. When you work with inheritance, you need an precise knowledge of the entire inheritance tree. When you instantiate a class at the bottom of the inheritance tree, you may need to pass parameters to the constructor of a class that is much nearer the root node.

The instance constructor of a subclass is divided into two parts by the CALL METHOD SUPER->CONSTRUCTOR statement. In the statements before the call, the constructor behaves like a static method, that is, it cannot access the instance attributes of its class. You cannot address instance attributes until after the call. Use the statements before the call to determine the actual parameters for the interface of the instance constructor of the superclass. You can only use static attributes or local data to do this.

When you instantiate a subclass, the instance constructors are called hierarchically. The first nesting level in which you can address instance attributes is the highest-level superclass. When you return to the constructors of the lower-level classes, you can also successively address their instance attributes.

In a constructor method, the methods of the subclasses of the class are not visible. If an instance constructor calls an instance method of the same class using the implicit self-reference ME->, the method is called as it is implemented in the class of the instance constructor, and not in any redefined form that may occur in the subclass you want to instantiate. This is an exception to the rule that states that when you call instance methods, the system always calls the method as it is implemented in the class to whose instance the reference is pointing.

"Static Constructors

Every class has a static constructor called CLASS_CONSTRUCTOR. As far as the namespace within an inheritance tree, the same applies to static constructors as to instance constructors.

The first time you address a subclass in a program, its static constructor is executed. However, before it can be executed, the static constructors of all of its superclasses must already have been executed. A static constructor may only be called once per program. Therefore, when you first address a subclass, the system looks for the next-highest superclass whose static constructor has not yet been executed. It executes the static constructor of that class, followed by those of all classes between that class and the subclass you addressed.

statement. The new class agent.

  • CATCH CX_OS_OBJECT_NOT_FOUND .

*ENDTRY.

*TRY.

CALL METHOD L_FLIGHT_AGENT->GET_PERSISTENT

EXPORTING

I_CARRID = 'LH'

I_CONNID = '0400'

I_FLDATE = '20060701'

RECEIVING

RESULT = l_flight

.

  • CATCH CX_OS_OBJECT_NOT_FOUND .

*ENDTRY.

l_seatsfree = L_FLIGHT->GET_SEATSMAX( ) - L_FLIGHT->GET_SEATSOCC( ).

if l_seatsfree > 0.

l_seatsfree = L_FLIGHT->GET_SEATSOCC( ) + 1.

l_flight->set_seatsocc( l_seatsfree ).

endif.

There are lots of other methods and techniques that you can use in persistent classes.

A demonstration of this can be found in program DEMO_CREATE_PERSISTENT.

DEMO_QUERY_PERSISTENT

kindly look into below link for screen shot to create persistence class

http://erpgenie.com/abaptips/content/view/417/38/

To tell you more about this persistancy classes are used replace the sql. Actually this are developed to combine the advantages of object oriented design to the relational database model. The main advantage is security is enforced here because of the visibility restrictions private, protected, public of the classes , so you need not take care about the authorisations required for the task. Apat from this all updates are done in update tasks.

As already explained how to do this. Let me explain actually what happens.

Whenever you actiavate a persistancy class it creates class which acts as agent or actor to the persistancy class created. This class actually is responsible for the creation and maintenace of the instances of your persistancy class. If you take a closer look at the code given by satish we actually create an instace of the actor class. This class is actually a sub class of a base class whichhas all the methods.

So whenever you create a persistant class it creates 3 classes

1. CL_