Example of Protected Method in x++

Short Answer
Views 57

Answer:

protected void methodProtected()

  • This is a protected method with no parameters.
  • The protected access modifier means that this method can only be accessed from within the class itself or any derived class (a subclass) of the Person class.
  • The method is declared as void, which means it doesn't return a value.
  • When called, it executes the code within its body.
  • In this case, it calls the info method (which is not defined in your provided code) with the message "Inside protected method. Inside Person Class."

protected void methodProtected(){
           info("Inside protected method. Inside Person Class.");
   }

protected void methodProtected(int a, int b)

  • This is also a protected method, but it has two integer parameters, a and b.
  • Just like the first method, it can only be accessed within the Person class or any of its subclasses.
  • It is declared as void, indicating that it doesn't return a value.
  • When called, it executes the code within its body, but in this case, it has access to the values of a and b that are passed as arguments.
  • Again, it calls the info method (which is not defined in your provided code) with the same message: "Inside protected method. Inside Person Class."

protected void methodProtected(int a, int b){
           info("Inside protected method. Inside Person Class.");
   }


Example Code


public class Person
{
   str firstname;
   str lastname;
   protected void methodProtected(){
           info("Inside protected method. Inside Person Class.");
   }
 
   protected void addMethodProtected(int _a, int _b)
   {
       int c = _a + _b;
       info(strFmt("Inside protected parameterized method1. Inside Person Class."));
       info(strFmt(" --> %1 + %2 = %3", _a, _b, c));
   }
 
   protected int addMethodProtected1(int _a, int _b)
   {
       int c = _a + _b;
       return c;
   }
 
   public static void main(Args _args){
           setPrefix("Output");
           Person person = new Person();
           person.methodProtected();
           person.addMethodProtected(12, 10);
 
           int returnedValue;
           returnedValue = person.addMethodProtected1(10, 11);
           info(strFmt(" --> %1 ", returnedValue));
   }
}

  1. public static void main(Args _args): This is the entry point of the program, commonly found in X++ classes. The main method is where the program begins execution.

  2. setPrefix("Output"): This function call seems to set a prefix for output. It's not defined in the provided code, but it could be a custom function responsible for configuring some aspect of the program.

  3. Person person = new Person(): This line creates an instance of a class named Person and assigns it to the variable person. This assumes that you have a class named Person defined elsewhere in your code.

  4. person.methodProtected(): It calls the methodProtected method of the person object. As indicated by the name, this method is protected, which means it's accessible only within the class Person and its subclasses.

  5. person.addMethodProtected(12, 10): This line calls a method named addMethodProtected on the person object. Again, this method is not defined in the provided code, but it likely has a protected access modifier, considering the naming convention used.

  6. int returnedValue;: Here, a variable named returnedValue of type integer is declared.

  7. returnedValue = person.addMethodProtected1(10, 11);: This line calls a method named addMethodProtected1 on the person object with arguments 10 and 11. It appears to return an integer value, which is then assigned to the returnedValue variable.

  8. info(strFmt(" --> %1 ", returnedValue));: This line uses the info function to display a message to the user. It formats the message as " --> " followed by the value of returnedValue.


Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of X++ Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.