Home / Questions / Example of Protected Method in Inheritance in x++
Explanatory Question

Example of Protected Method in Inheritance in x++

👁 102 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

Example of Protected Method in Inheritance in x++

Person:


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;
   }
  
}

Developer:


class Developer extends Person
{
 
   protected void methodProtected()
   {       
       info("Inside protected method. Inside Developer Class.");
       super();
   }
 
   protected int addMethodProtected1(int _a, int _b)
   {
       this.methodProtected();
       int c = _a + _b;
       return c;
   }
 
   public static void main(Args _args)
   {
       setPrefix("Output");
       //Person person = new Person();
       Developer developer = new Developer();
 
       developer.methodProtected();
       developer.addMethodProtected(12, 10);
       int returnedValue;
       returnedValue = developer.addMethodProtected1(10, 11);
       info(strFmt(" --> %1 ", returnedValue));
   }
}

Output:


Inside protected method. Inside Developer Class.
Inside protected method. Inside Person Class.
Inside protected parameterized method1. 
Inside Person Class.--> 12 + 10 = 22
Inside protected method. Inside Developer Class.
Inside protected method. Inside Person Class.
--> 21