ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Find the output of C#.Net programs | Inheritance | Set 1: 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
    {
        public ABC()
        {
            Console.WriteLine("ABC-Constructor called");
        }
        public void FUN()
        {
            Console.WriteLine("FUN() called");
        }
    }

    class XYZ : ABC
    { 
        public XYZ()
        {
            Console.WriteLine("XYZ-Construtor called");
        }
    }

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

            X.FUN();
        }
    }
}

Output:

ABC-Constructor called
XYZ-Construtor called
FUN() 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 no-argument constructor and a method FUN(), and class XYZ contains a no-argument constructor.

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 it calls the constructor of class ABC before calling the constructor of class XYZ because of inheritance. Then we called the FUN() method of ABC class.

Question 2:

using System;

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

    class XYZ : ABC
    { 
        public XYZ()
        {
            Console.WriteLine("XYZ-Construtor called");
        }
    }

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

            X.FUN();
        }
    }
}

Output:

main.cs(32,15): error CS1540: Cannot access protected member `Demo.ABC.FUN()' via a qualifier of type `Demo.XYZ'. The qualifier must be of type `Demo.Program' or derived from it
main.cs(11,24): (Location of the symbol related to previous error)
main.cs(32,15): error CS0122: `Demo.ABC.FUN()' is inaccessible due to its protection level
main.cs(11,24): (Location of the symbol related to previous error)

Explanation:

The above program will generate a syntax error because a protected member of a class can only be accessed in the child or derived class, here FUN() method is called in Main() method of Program, which is not possible due to protection level.

Question 3:

using System;

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

    class XYZ : ABC
    { 
        public XYZ()
        {
            Console.WriteLine("XYZ-Construtor called");
        }
    }

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

            X.FUN();
        }
    }
}

Output:

ABC-Constructor called
XYZ-Construtor called
FUN() 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 no-argument constructor with a protected access modifier and a method FUN(), and class XYZ contains a no-argument constructor.

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 it calls the constructor of class ABC before calling the constructor of class XYZ because of inheritance. Here, constructor of ABC class is protected member; here it was called from child class XYZ. Then we called the FUN() method of ABC class.

Question 4:

using System;

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

    class XYZ : ABC
    { 
        protected XYZ()
        {
            Console.WriteLine("XYZ-Construtor called");
        }
    }

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

            X.FUN();
        }
    }
}

Output:

main.cs(30,21): error CS1540: Cannot access protected member `Demo.XYZ.XYZ()' via a qualifier of type `Demo.XYZ'. The qualifier must be of type `Demo.Program' or derived from it
main.cs(19,19): (Location of the symbol related to previous error)
main.cs(30,21): error CS0122: `Demo.XYZ.XYZ()' is inaccessible due to its protection level
main.cs(19,19): (Location of the symbol related to previous error)

Explanation:

The above program will generate a syntax error because here we accessed the constructor of XYZ class from Program class, but here we declared constructor of XYZ as a protected member. But protected members can only be accessed in the same class on the derived class.

Question 5:

using System;

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

    class XYZ : ABC
    { 
        public ~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:

main.cs(7,17): error CS0106: The modifier `public' is not valid for this item
main.cs(19,17): error CS0106: The modifier `public' is not valid for this item

Explanation:

The above program will generate two syntax errors because we cannot use an access modifier with destructors.

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

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.