Documente Academic
Documente Profesional
Documente Cultură
Often, there is a requirement that you would want to create only one instance of the class. To put
it in other words, you do not want any one to create instance of the class if already created. This
is a common requirement in many application designs. So how do we achieve this ?
This can be achieved using the singleton pattern. In this design pattern, we will declare the
constructor as a 'Private' one. Private constructor !! Is that possible?
Yes, it is.
Go to the properties tab of class builder (SE24) and change the instantiation type to private. Then
you can create a private constructor. The advantage of having a private constructor is that now no
one can create an object of your class..
So how do we create the instance of this class ? We create a static method GET_INSTANCE that
will be used to create the instance. A static method because this method can be called directly
with the class reference (without an object). Inside this method, we can create the object
(because the constructor is a private method, we can call it within class).
Now, how to make sure that only one instance is created? Simple.
Store the own reference as an attribute of the class. In the method GET_INSTANCE check if this
attribute is populated. If it populated then return the same instance. If not, create a new one, store
it in the own attribute and return the instance.
An example implementation of GET_INSTANCE is shown here. Lets say the name of the
attribute is GREF_OBJ.
IF gref_obj IS INITIAL.CREATE OBJECT gref_obj TYPE (class name).
ENDIF.
RETURN gref_obj.
Ans2. https://www.sapnuts.com/courses/Object-Oriented-ABAP/OOABAPsingleton/creatg-global-sglen.html
Oopsqwhat is fieldcatalog in oops alv?
1.
Static attributes:
1. The attributes are created only once.
2. That means memory allocation for the attribute is only once.
3. We can call next time the same attribute by using object then the
same memory space is referred.
4. We can change the value in the memory.
Instance attributes:
1. These attributes are created for every time.
2. That means for each and every call statement, the attribute creates a
new memory location at every time.
3. What is a class?
Classes are templates for objects. Conversely, you can say that the type of an
object is the same as its class. A class is an abstract description of an object.
You could say that it is a set of instructions for building an object. The
attributes of objects are defined by the components of the class, which describe the
state and behavior of objects.
4. What is an object?
An object is a combination of messages and data. Objects can receive and send
messages and use messages to interact with each other. The messages contain
information that is to be passed to the recipient object.
5. How many types of classes are there in OOAbap?
Public class
Private class
Final class:Final methods cannot be redefined in subclasses. Final classes cannot
have any more subclasses and constitute the final node of an inheritance tree.
Single-ton class: we can create only one instance of a class.
Abstract class:Abstract methods are declared in abstract classes and cannot be
implemented in the same class but only in a subclass of the inheritance tree.
Abstract classes can consequently not be instantiated. A non-abstract method is a
concrete method. Except for the instance constructor, the concrete instance
methods of a class can also call its abstract methods.
Persistent class:In principle, ABAP programs work with local program data, which resides
in the programs internal session. This data lives only as long as its context that is, as long
as its associated procedure (for local procedure data); its object (for attributes of classes); or
its program (for global program data). This data is known as transient. Data that can be
preserved beyond the runtime of the program is known as persistent. In SAP Systems,
persistent data usually occurs as the content of database tables, but also as the content of
files on application and presentation servers.
Friend class: There are certain special cases where a class would want access to other
classes private attributes and methods in such scenario we can make use of the
friendsconcept in classes.
Endclass.
10.What is a constructor & types of constructors?
Constructor is a special method, which will be called automatically as and when
the object is created for a class, it can have only importing parameters but not
exporting, it is generally used to give some initial state to the object.
A class can contain have two types of constructors static and instance
constructors.
11.Difference between static and instance constructors?
Static constructor will be called only once, i.e. at the time of loading class in to
memory.
Instance constructors are instance specific, these constructors will be called as
and when the new object is created for that class.
12.How to a create object for the class?
Data: ref type ref to <cl_name>.
Create object ref.
13.how to call a method?
Call method ref->method_name<exporting x = 1>.
14.What is static attribute & method?
Static attributes & methods are class specific, memory will be allocated only
once for Static attributes & methods irrespective of no. of objects created.
We can access the components with a reference variable i.e. by using class
name
Call method <cl_name>=>menthod_name.
15.Can we instantiate a class within implementation of other class?
Yes
16.Can we put non declarative statement e.g. START-OF-SELECTION
within a class?
No, we cant use.
17.How to create a global class?
27.
28.Can we implement interface in private section of any class?
No
26.What is alias?
Instead of specifying full name of interface methods, we can assign it a name which
can directly trigger.
28.What is a friend class?
Friend class is a class it can access private components of its friends class.
29.How to create an object for private class?
In general we cannot create object for a private class, but we can access static
method of a private class so call that method using its class name and import that
object.
For example take one static method with an exporting parameter inside private
class and write object creation code in that static method and export that object.
30.What is a Single-ton class?
Single-ton classes can be instantiated only once, i.e. only one object is created
for Single-ton classes.
31.What is a Persistent class?
A special class, the attributes of which are linked to database tables via objectrelational mapping. Since Release 6.10 they can be created using the Mapping
Assistant of the Class Builder.
The objects of persistent classes are managed by Object Services. An object in a
persistent class, the attributes of which are saved as database content after the run
time of an ABAP program.