Example of Protected Method in x++
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 thePerson
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
andb
. - 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
andb
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)); } }
-
public static void main(Args _args)
: This is the entry point of the program, commonly found in X++ classes. Themain
method is where the program begins execution. -
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. -
Person person = new Person()
: This line creates an instance of a class namedPerson
and assigns it to the variableperson
. This assumes that you have a class namedPerson
defined elsewhere in your code. -
person.methodProtected()
: It calls themethodProtected
method of theperson
object. As indicated by the name, this method is protected, which means it's accessible only within the classPerson
and its subclasses. -
person.addMethodProtected(12, 10)
: This line calls a method namedaddMethodProtected
on theperson
object. Again, this method is not defined in the provided code, but it likely has a protected access modifier, considering the naming convention used. -
int returnedValue;
: Here, a variable namedreturnedValue
of type integer is declared. -
returnedValue = person.addMethodProtected1(10, 11);
: This line calls a method namedaddMethodProtected1
on theperson
object with arguments 10 and 11. It appears to return an integer value, which is then assigned to thereturnedValue
variable. -
info(strFmt(" --> %1 ", returnedValue));
: This line uses theinfo
function to display a message to the user. It formats the message as " --> " followed by the value ofreturnedValue
.
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.