Tuesday 2 December 2014

Application Package (Public vs Protected vs Private) Methods Part 1

Today, we will check the difference between public, protected and private for Application Packages.

First, let's check this sample Application Package.

class Person
   method Person();
   method SayBarbecue();
   property string Property1;
protected
   method SayHello();
   property string Property2;
private
   method SayGoodbye();
   instance string &Property3;
end-class;

method Person
end-method;

method SayBarbecue
   MessageBox(0, "", 0, 0, "Barbecue!");
end-method;

method SayHello
   MessageBox(0, "", 0, 0, "Hello!");
end-method;

method SayGoodbye
   MessageBox(0, "", 0, 0, "Goodbye!");
end-method;

We have 3 methods.

  • public method: SayBarbecue()
  • protected method: SayHello()
  • private method: SayGoodbye()

What will happen if we use these methods outside the application class?
The following code has been placed on a FieldChange event.

import TEST_APP_PKG:*;

Local TEST_APP_PKG:Person &Lon = create TEST_APP_PKG:Person();

&Lon.SayBarbecue();

/* Cannot be called because these methods are protected or private */
rem &Lon.SayHello();
rem &Lon.SayGoodbye();

SayBarbecue() method can be freely used.
But SayHello() and SayGoodbye() methods will error at compile time.
This is because protected and private methods can only be used within the application class.

No comments:

Post a Comment