Wednesday, November 18, 2009
Constructor in Abstract Class
using System;
using System.Collections.Generic;
using System.Text;
namespace TestProject
{
public class Mains
{
public static void Main()
{
//This will give error:
//Cannot create an instance of the abstract class or interface 'TestProject.Abstract'
//Abstract abs = new Abstract(4);
//abs.Print();
//This will work fine
Abstract abs = new First(4,5);
abs.Print();
}
}
abstract class Abstract
{
protected int i;
//Abstract()
//{
//}
public Abstract(int i)
{
this.i = i;
}
public virtual void Print()
{
Console.WriteLine(i);
Console.Read();
}
}
class First : Abstract
{
int k;
public First(int i, int j)
: base(i)
{
this.k = j;
}
public override void Print()
{
Console.WriteLine("i= " + base.i + " k= " + k);
Console.Read();
}
}
}
Will it Compile?
class First
{
virtual void Me()
{
Console.WriteLine("It's Me First");
}
}
No. virtual or abstract methods connot be private.
2)
class First{
protected virtual void Me(){
Console.WriteLine("It's Me First");
}
}
public class Second : First{
override void Me(){
Console.WriteLine("It's Me First");
}
}
No any override method is itself virtual which can be overrided in another child class. And virtual and abstract methods cannot be private.
3)
class First{}
public class Second : First{}
No, any base class must have at least the accessibility level of the child class.
4)
class First
{
public First()
{
}
protected virtual void Mine()
{
Console.WriteLine("It's Me First");
}
}
class Second : First
{
public override void Mine()
{
Console.WriteLine("It's Me First");
}
}
No, virtual and override methods should have same access modifier.
It will give error:
'Second.Mine()': cannot change access modifiers when overriding 'protected' inherited member 'First.Mine()'
Questions
Interface is a like a contract, which must be fulfilled. You can provide more functionality than required this means a class which implements an interface can have more methods than the interface but it must have all the methods declared in interface (the contract) with the same signature. You cannot define member field for interfaces.
What if I inherit from an abstract class and an interface and both have same method with same signature, this method is an abstract method in the abstract class?
You just override the method in the abstract class and contract will be automatically be fulfilled. this means you need only one method with the same signature which overrides the abstract method. This method now implicitly implements the method required for interface to fulfill the contract. If this method doesnot work as desired for the interface then you just need to explicetely implement the method and then the child class shall have two methods with the same name but one will be totally dedicated to the interface. This method now won't take any access modifier and will be called only if the instance of the class is created and referenced by the interface.
What if I have 5 interfaces, all have one common method and which work in the
same way for three and entirely different for the two, and there is one class which implements all these 5 interfaces.
In the class create one method which will be common to (implicitely implementing) 3 interfaces. Explicitely implement the method for rest of two interfaces.
What is the need of Abstract Class?
The class which defines all the common functionality and gives the signature for the functionality which is uncertain for what to be done and how to be done, is called an abstract class.
Transportation medium is an abstract class which will have method TernLeft() and TernRight() defined but Move() can be abstract as this can be different for Car, Rail, Ship and Plane.
What is the difference between out and ref?
out keyword is totally dedicated to outputting a value from a method. ref can also do the same but not totally dedicated for this. For ref the parameter must be initialized before passing which is not necessary for out. For out the parameter must be assigned some value in the method which is not the necessity for the ref. Also ref is usually for passing a value type as reference type. When we pass a value type by ref, the reference of variable in stack is kept in heap and that reference is passed and hence no new value is stored on heap. All the work is done by same old value at stack by that reference.
Which methods can be overrided?
Methods with keywork virtual or override (which is eventually virtual) can be overrided.
How to hide a method (which is not recommended)?
Using new keyword in derived class we can hide a method.
Why the base class should have atleast the accessibility level of child class i.e., why a parent class should have equal to or greater accessibility of the child class?
A parent class can keep the reference of a child class as:
Parent p = new Child();
Here with p all the methods available in Parent will be accessible. If those methods are overrided in the child class then p.Method() will provide the functionality of Child class. This means in this way of declaration any more method provided in the child class won't be accessible.If parent would be less accessible than the child class, we would unintentionally be trying to access the functionality of parent class in the access domain of child where the parent would not be accessible which prohibits OOP mechanism. That's why parent class should be equally or greater accessible than the child class. The same is true for Interfaces also.
Can we have a method with the same name with the class?
No, only class name is reserved for constructors. You cannot define a method with same name with class.
Why we cannot instantiate an abstract class?
Abstract class has its orgin in pure virtual function of C++ which used to have only declaration and not its implementation. in C# these methods must have its body or be it declared with abstract or extern keyword, if you declare the method with abstract keyword, the compiler will force you to declare the class as abstract. But if you want to be a class abstract in another way i.e., it has no abstract method but you want it to be abstract just add the abstract keyword to the class definition and it becomes abstract.Logically, an abstract class is a class which doesnt have an implementation for one or more methods. It means that the CLR doesn't have any direction of what to do in case if someone calls the method which is abstract. So if you were able to create an object for the abstract class and call any abstract method of it the CLR will not be able to decide what to do and hence it my be crashed. So to avoid this situation we are restricted not to instantiate a abstract class. If you need a object for that abstract class create a concrete subclass and create an object for it and use it.
Why Structs have constructors as opposed to it allocates memory on Stack?
Structs cannot have a default constructor supplied by user. That is default and when called it initializes all the data members ny zeroing them. If you define a constructor you must initialize all the data members in that constructor. Struct constructors do not allocate memory on heap but it assignes value to the data members i.e., allocates memory on the stack. As it allocates memory on stack it cannot be referenced by a child hence it doesnot support inheritance.
What's going under the hood of foreach?
The concept of foreach has come from Enumerator. All the basic methods of Enumerator is listed in IEnumerator interface. It defines two methods, MoveNext() and Reset(). It also defines a property Current which returns the object at current index from the collection.
But how foreach and Enumerator get combined? This is the responsibility of IEnumerable interface. it has a method called getEnumerator() which returns an IEnumerator. To iterate a collection using foreach you must implement IEnumerable.getEnumerator() which in turn returns an Enumerator object which enumerates the class which implements IEnumerable.
