Friday 5 December 2014

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

From my previous post, the difference between public, protected and private methods were shown by calling them outside the application class.

Today, we will try extending the base class to another application class.
The new application class will be called Australian.

This is the code for our base class, Person.

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;

And this is the code for the new class, Australian.

import TEST_APP_PKG:*;
class Australian extends TEST_APP_PKG:Person
   method Australian();
   
   method SayBarbecue();
   method SayAussieHello();
   method SayAussieGoodbye();
   property string Property1;
end-class;

method Australian
   %Super = create TEST_APP_PKG:Person();
end-method;

method SayBarbecue
   /+ Extends/implements TEST_APP_PKG:Person.SayBarbecue +/
   MessageBox(0, "", 0, 0, "Barbie!");
end-method;

method SayAussieHello
   %This.SayHello();
end-method;

method SayAussieGoodbye
   /* cannot call SayGoodbye because this is private in Person class */
   rem %This.SayGoodbye();
end-method;

Notice that I extended the Person class.
This means that the Australian will inherit the properties and methods of Person.

SayBarbecue()

The Australian class can overwrite methods of the Person class if the method has the same name.

SayAussieHello()

The Australian class can call public or protected methods of Person class because it was extended beforehand.

SayAussieGoodbye()

The Australian class cannot call private methods of Person class. Private methods can only be used by the class owner.
Now let's use Australian class in FieldChange.

import TEST_APP_PKG:*;
Local TEST_APP_PKG:Australian &AussieLon = create TEST_APP_PKG:Australian();

&AussieLon.SayAussieHello();
&AussieLon.SayBarbecue();

Notice that even if Person.SayHello method was protected, we were able to "trigger" it outside the application class because the calling method, Australian.SayAussieHello, is public.
Also, Australian.SayBarbecue method has overwritten Person.SayBarbecue, which is why it said "Barbie" instead of "Barbecue".

6 comments:

  1. Is the last point valid "Notice that even if Person.SayHello method was private, we were able to call it outside the application class because Australian.SayAussieHello method is public."

    ReplyDelete
  2. Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming. TubeMate YouTube Downloader For Android Smartphone

    ReplyDelete
  3. You nailed it with simple cute examples. I was looking for a post on the difference between protected & private objects for long. Your post was like water in the desert!! Thanks a bunch!

    ReplyDelete
  4. very clear expalnation. Thanks

    ReplyDelete
  5. the makers can give understanding concerning the necessary alterations, increases or buys that will be vital for every single package considered. földmunkagép szállítás Europa-Road Kft.

    ReplyDelete