ADVERTISEMENT
ADVERTISEMENT

C#.Net find output programs (Inheritance) | set 2

Find the output of C#.Net programs | Inheritance | Set 2: Enhance the knowledge of C#.Net Inheritance concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 08, 2021

Question 1:

using System;

namespace Demo
{
    class ABC
    {
        ~ABC()
        {
            Console.WriteLine("ABC-Destructor called");
        }
        public void FUN()
        {
            Console.WriteLine("FUN() called");
        }
    }

    class XYZ : ABC
    { 
        ~XYZ()
        {
            Console.WriteLine("XYZ-Destructor called");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            XYZ X = new XYZ();

            X.FUN();
        }
    }
}

Output:

FUN() called
XYZ-Destructor called
ABC-Destructor called
Press any key to continue . . .

Explanation:

In the above program, we created two classes ABC and XYZ. Here, we inherited class ABC into class XYZ.

Class ABC contains a destructor and a method FUN(), and class XYZ also contains a destructor.

Now look to the Main() method, the Main() method of Program class is the entry point of the program. Here, we created the object of class XYZ then here we called FUN() method. After the destructor of the XYZ, class gets called and then the destructor of ABC class called.

Question 2:

using System;

namespace Demo
{
    class ABC
    {
        ~ABC()
        {
            Console.WriteLine("ABC-Destructor called");
        }
        public void FUN()
        {
            Console.WriteLine("FUN() called");
        }
    }

    class PQR : ABC
    { 
        ~PQR()
        {
            Console.WriteLine("PQR-Destructor called");
        }
    }
    class XYZ : PQR
    { 
        ~XYZ()
        {
            Console.WriteLine("XYZ-Destrutor called");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            XYZ X = new XYZ();

            X.FUN();
        }
    }
}

Output:

FUN() called
XYZ-Destrutor called
PQR-Destructor called
ABC-Destructor called
Press any key to continue . . .

Explanation:

In the above program, we created two classes ABC, PQR, and XYZ. Here, we inherited class ABC into class PQR class then we inherited PQR class into XYZ class.

Class ABC contains a destructor and a method FUN(), and class PQR and XYZ also contains a destructor.

Now look to the Main() method, The Main() method of Program class is the entry point of the program. Here, we created the object of class XYZ then here we called FUN() method. After destructors of XYZ, PQR, and ABC classes called respectively.

Question 3:

using System;

namespace Demo
{
    class ABC
    {
        protected int A;
        protected int B;

        public ABC()
        {
            A = 10;
            B = 20;
        }

        public void Print()
        {
            Console.WriteLine(A + " " + B);

        }
    }

    class PQR : ABC
    { 
        ~PQR()
        {
            Console.WriteLine("PQR-Destructor called");
            A = 50;
            A = 100;
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            PQR X = new PQR();

            X.Print();        
        }
    }
}

Output:

10 20
PQR-Destructor called
Press any key to continue . . .

Explanation:

In the above program, we created three classes ABC, PQR, and Program. The ABC class contains two protected members A and B that are initialized in the constructor with 10 and 20 respectively. Here, we inherited class ABC into class PQR. The PQR class contains a destructor that modifies the value of A and B members of class ABC.

Now look to the Main() method of Program class. Here, we created object X of PQR class then data members A and B initialized with 10 and 20. Then we called the Print() method that will print the value of A and B on the console screen. After that when the scope of class PQR gets finished then the destructor of class PQR gets called.

Question 4:

using System;

namespace Demo
{
    class ABC
    {
        protected int A;
        protected int B;

        public ABC()
        {
            A = 10;
            B = 20;
        }

        public void Print()
        {
            Console.WriteLine("ABC: "+A + " " + B);
        }
    }

    class PQR : ABC
    { 
        ~PQR()
        {
            Console.WriteLine("PQR-Destructor called");
        }
        public void Print()
        {
            Console.WriteLine("PQR: "+A + " " + B);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            PQR X = new PQR();

            X.Print();        
        }
    }
}

Output:

PQR: 10 20
PQR-Destructor called
Press any key to continue . . .

Explanation:

In the above program, we created three classes ABC, PQR, and Program. The ABC class contains two protected members A and B that are initialized in the constructor with 10 and 20 respectively. Here, we inherited class ABC into class PQR. The PQR class contains a destructor. Here, Print() method is implemented in both classes ABC and PQR that will print values of A and B on the console screen.

Now look to the Main() method of Program class. Here, we created object X of PQR class then data members A and B initialized with 10 and 20. Then we called the Print() method that will print the value of A and B on the console screen. Here, Print() method of PQR class gets called because ABC and PQR both contain the method with name Print(). After that when the scope of class PQR gets finished then the destructor of class PQR gets called.

Question 5:

using System;

namespace Demo
{
    class ABC
    {
        protected int A;
        protected int B;

        public ABC()
        {
            A = 10;
            B = 20;
        }

        public void Print()
        {
            Console.WriteLine("ABC: "+A + " " + B);
        }
    }

    class PQR : ABC
    { 
        ~PQR()
        {
            Console.WriteLine("PQR-Destructor called");
        }
        public void Print()
        {
            super.Print();
            Console.WriteLine("PQR: "+A + " " + B);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            PQR X = new PQR();

            X.Print();        
        }
    }
}

Output:

main.cs(28,21): warning CS0108: `Demo.PQR.Print()' hides inherited member `Demo.ABC.Print()'. Use the new keyword if hiding was intended
main.cs(16,21): (Location of the symbol related to previous warning)
main.cs(30,13): error CS0103: The name `super' does not exist in the current context

Explanation:

The above program will generate a syntax error because we used super to call the Print() method of the superclass in the Print() method of PQR class. In C# super does not exist. To call the base class method we need to use the base keyword in the C# program.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.