Friday, November 11, 2016

Some Questions OOPS

1. Will the following code compile?
 class Base
        {
            public Base(string str)
            {
                Console.WriteLine(str);
            }
        }
        class Child : Base
        {
            public Child()
            {
                Console.WriteLine("I am a child class constructor");
            }
            public static void Main()
            {
                Child CC = new Child();
            }
        }

No, It will not compile because Base does not have parameter less constructor. And child class have one constructor without parameter so it will try to call the same in base class. Although class automatically provide default parameter less constructor if there is no constructor define. But in this case one base constructor with parameter defined.

Need following correction

class Base
        {
            public Base(string str)
            {
                Console.WriteLine(str);
            }
        }
        class Child : Base
        {
            public Child():base("Hello")
            {
                Console.WriteLine("I am a child class constructor");
            }
            public static void Main()
            {
                Child CC = new Child();
            }
        }

2. Can we use access specifier for static constructor
No, Static constructor are not allowed for static constructor.

3.Give 2 scenarios where static constructors can be used?
1. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
2. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

4. What happens if a static constructor throws an exception?
If static constructor will thorw an error, than that class will not load.

5. 
public class Parent
    {      
        public void print()
        {
            Console.WriteLine("I'm a Parent Class.");
        }
    }
    public class Child : Parent
    {        
        public new void print()
        {
            base.print();
            Console.WriteLine("I'm a Child Class.");
        }
    }

    public class MainShow
    {
        public void Show()
        {
            Child child = new Child();
            child.print();//I'm a Parent Class.  I'm a Child Class.
            ((Parent)child).print();//"I'm a Parent Class.                                    
        }
    }

6. Will it compile?

    class Base
    {
        void Print();
    }

    class Child
    {
        public void Print()
        {
            Console.WriteLine("Hello");
        }
    }

    class MainClass
    {
        public void Show()
        {
            Child objChild = new Child();
            objChild.Print();
        }        
    }

Output : It will not compile : 
Error : Base.Print() must declare a body because it is not marked abstract, extern or partial.

7. Structs are not reference types. Can structs have constructors?
Yes, even though Structs are not reference types, structs can have constructors.

8. We cannot create instances of static classes. Can we have constructors for static classes?
Yes, static classes can also have constructors. But is should have static constructor only.

9. Can a child class call the constructor of a base class?
Yes, a child class can call the constructor of a base class by using the base keyword as shown in the example below.

10. An abstract class cannot be a sealed class. I.e. the following declaration is incorrect.

11. Declaration of abstract methods are only allowed in abstract classes.

12. An abstract method cannot be private.

13. The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.

14. An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.

//Incorrect
public abstract virtual int Show();

15. An abstract member cannot be static.

//Incorrect
public abstract  static void Show();

16.

(1) Why is this allowed?
BaseClass b = new ChildClassA(); 
ChildClassA c = (ChildClassA)b


(2) Why is this not allowed?
ChildClassA c = (ChildClassA)new BaseClass(); 
BaseClass b = (BaseClass)c;

To explain, let's use some more real world names for your example classes:
class Animal
{
}

class Dog : Animal
{
}

class Cat : Animal
{
}

So for your example (1):
Animal b = new Dog(); 
Dog c = (Dog)b

This is true because all Dogs are Animals and your Animal b is actually a dog so the cast is successful.

For your example (2):
Dog c = (Dog)new Animal(); 
Animal b = (Animal)c;

This is impossible because you are assigning an Animal object to a Dog, but you know that not all animals are Dogs, some animals are cats.

And for examples (3) & (4):
Dog c = new Dog(); 
Animal b = (Animal)c;

This is the same as your example 1 above. All dogs are animals, so any dog can be classified as an animal and cast (in fact you don't need the cast, there would be an implicit cast and you can write it as Animal b = c;

16.
 public class Baser
    {

        public Baser(int a)
        {
            Console.WriteLine(a);
        }
    }

    public class Deriver : Baser
    {

        public Deriver(int a, int b) : base(Processor(a, b))
        {
        }

        public static int Processor(int a, int b)
        {
            Console.WriteLine(b);
            return a;
        }
    }

    public class MainClass
    {
        public void Show()
        {
            Deriver objDeriver = new Deriver(10, 20);
            //Output 20 10
            //
        }
    }

No comments:

Followers

Link