Head scratching object oriented stuff

ron naber
.NET voyage
Published in
3 min readOct 28, 2014

--

some concepts which makes us nervous

Last minute brush on classic object oriented questions

Abstract class :

cannot be instantiated and acts as a base class for other classes.

common functionality that multiple derived classes can share and overrid

An abstract class contain abstract members as well as non-abstract members.

An abstract class can have concrete methods which is not possible in Interface.

An abstract class cannot be a sealed class because the sealed modifier
prevents a class from being inherited and the abstract modifier requires
a class to be inherited.

A non-abstract class which is derived from an abstract class must include
actual implementations of all the abstract members of parent abstract class.

An Abstract class can has access modifiers like private, protected,
internal with class members.But abstract members cannot have private
access modifier

An abstract class can have constructors and destructor.

Abstract class does not support multiple inheritance.

little more about Abstract class :

Abstract class cannot be instantiated and job of constructor is to get
called when child classes ‘s objects are created. So keeping the
constructor of abstract class as public gives idea to developer that
this class is instantiable , hence it should be protected or internal.
Again keeping constructor as private also doesnot make sense

Lets see difference between const and readonly

Yes thats not object oriented concept , but in implementation of this
two keywords object oriented concept plays important role.

Now const is compile time read.That means you dont need object to create
constant. So const value is initialised at compile time.

Const value is declared in compile time.

Read only is like const but IT IS INITIALISED AT RUN TIME.So at compile time
value of readonly variable is not known.

const are static while readonly is not. One way to put this is
that we dont have to create object of const to acess const but not the same
for readonly.

What is singleton pattern ? and difference with static class

Singleton pattern makes sure that you have only single instance of
that class available while static class can never be instantiated at all.

—single pattern has public class with private constructor
which means that no other class than that class can create object of that
class.
—then it has private instance class object declared in it as property.
—similarly you have public instance class object declared in it as property
— in the get method of public instance class object , we return the private instance after instantiating it.

using System;

public class Singleton
{ private static Singleton instance;

private Singleton() {}

public static Singleton Instance { get { if (instance == null) { instance = new Singleton(); } return instance; } }
}

also here you can declare a static method inside which we
can put constructor

and this static method can be called from any where.

What if class implementing two interface have same methods

we can do explicit implimentation then

public interface IFoo1
{ void DoStuff();
}

public interface IFoo2
{ void DoStuff();
}
You can implement both like this:

public class Foo : IFoo1, IFoo2
{ public void IFoo1.DoStuff() { }

public void IFoo2.DoStuff() { }
}

Have a look at this perfect running code , this will tell us few more interesting things

class program
{
interface IA
{
void getName(string s);
}
interface IB
{
void getName(string s);
}
interface IC
{
void getName(string s);
}
public abstract class AC
{
public abstract void getName(string s);
}
public class CA : AC , IA, IB, IC
{
public override void getName(string s)
{
Console.WriteLine(s);
}
}
static void Main()
{
CA ca = new CA();
Console.ReadLine();
}
}

so what is happening here is that 3 interface and one abstract method has same method getname . I used to think in such scenario we should implement method thrice (like implicit implementation in the above topic we discussed but voila , we dont need to and this is perfectly fine.

However lets keep in mind this thing

—to make interface happy just implement it

—to make abstract happy YOU HAVE TO OVERRIDE IT. At the end of the day dont forget that it is a class too.

--

--

ron naber
.NET voyage

writing sets you free , lets you explore and enjoy life more, so read n write