Class

From Abap2Java

Jump to: navigation, search

Image:Dotright.JPG ABAP

Classes are the most important component of object orientation. They represent a type of objects that have the same attributes and methods. A class is usually defined in the ABAP Workbench. A class can be seen as a construction plan for the individual type.

An instance of a class can be created with the keyword CREATE OBJECT (by which the constructor of the class will be called).

*1) Definition of the class
CLASS CL_MY_CLASS DEFINITION.
  PUBLIC SECTION.
     "public attribute:
     DATA mv_1 TYPE I.

     "constructor (optional)
     METHODS constructor IMPORTING VALUE(iv_p) TYPE I.

   PROTECTED SECTION.
     "protected attribute:
     DATA mr_2 TYPE REF TO cl_other_class.

   PRIVATE SECTION.
     "private method:
     METHODS do_this IMPORTING VALUE(iv_i_times) TYPE I. 
ENDCLASS. 
*2) Implementation of the class
CLASS CL_MY_CLASS IMPLEMENTATION.

   METHOD constructor.
      me->mv_1 = iv_p.
   ENDMETHOD.

   METHOD do_this.
      DATA lv_string TYPE STRING.
      lv_string = me->mv_1.
      DO iv_i_times TIMES. 
        WRITE lv_string. 
      ENDDO.
   ENDMETHOD.
ENDCLASS.
"3) creating new objects of the type CL_MY_CLASS: 
DATA lr_1st_object TYPE REF TO cl_my_class.
DATA lr_2nd_object TYPE REF TO cl_my_class.
CREATE OBJECT lr_1st_object EXPORTING iv_p = 1.
CREATE OBJECT lr_2nd_object EXPORTING iv_p = 55.
"4) calling a method of an object of the class
lr_1st_object->do_this( 3 ).
lr_2nd_object->do_this( 5 ).

See also:
METHOD, OO, Hello World

Image:Dotleft.JPG Java

Classes are the most important component of object orientation. They represent a type of objects that have the same attributes and methods. A class can be seen as a construction plan for the individual type.

The keyword class declares an individual type. A class is saved in a text file, which has the same name as the class plus the ending '.java'. A '*.java' file only contains one class. A class can have inner classes though.

An instance of a class can be created with the keyword new (by which the constructor of the class will be called).

//This class must be saved in the filesystem as: com/abap2java/MyClass.java
package com.abap2java
 
class MyClass {

   //private attribute:
   private int m1;

   //protected attribute:
   protected OtherClass otherOjbect. 

   //Constructor has same name as class:
   public MyClass(int p) { 
      this.m1 = p;
   }

   //public method:
   public void doThis(int i_times) {
      for(int i = 0; i[[<]]i_times; i[[++]]){ 
        System.out.print ( this.m1 ); 
      }
   }  
}
//Creating a new object of the type MyClass 
MyClass object1 = new MyClass(1);
MyClass object2 = new MyClass(55);
//Calling a method of an object of the class
object1.doThis(3);
object2.doThis(5);

Objects are handled as a reference. An allocation with "=" will only copy the reference of the object. A method call on object1 or object2 are identical in result. To retrieve an identical copy of the object use the method clone instead.

Several instances can be constructed from a class, whereas every instance will contain its individual data contained in the member fields. However the modifier static allows the declaration of fields and methods which only exist once within a class and are same for all instances.


See also
Overload, Overwrite, Extending, Implementing, modifier, interface, abstract class,inner class, method, field, package

Personal tools