Differences Interface, Abstract and sealed classes

Differences Interface, Abstract and sealed classes

Interface

——————

interface keyword

interface is used to DECLARE members like

methods and properties.

An interface is public always.

Members of interface cannot use access modifer.

An interface is used to implement its members

to a class or structure. More than one interfaces

can be implemented to a class or structure.

All members of an interface MUST be defined

inside the implementing class or structure.

‘Object instance’ cannot be created from an interface.

———————————————————–

abstract class

————————-

abstract  keyword

An abstract class is used for ‘inheritance’ only.

An abstract class can be used as a ‘base class’.

‘Object instantiation’ is NOT allowed with

an abstract class. So no ‘constructor’ is available

with an abstract class. And ‘new’ operator cannot

be used with an abstract class.

Members like methods and properties can be DEFINED

in an abstract class. All the members will be inherited

to the ‘derived class’ automatically.

 

‘Abstract Members’ can also be DECLARED in

an abstract class[like interfaces], using abstract keyword.

Access modifiers CANNOT be used with abstract members.

 

All the ‘abstract members’ of the abstract class

MUST be DEFINED in the derived class.

public abstract  class AbsClass : Object {

string x = “”;

public string Method1() {

// do code

return “”;

}

public string Property1 {

get{return x;}

set{x=value;}

}

// abstract member(s)

// Only Declaration

abstract  int MyMethod();

abstract string MyProperty{get;}

}

AbsClass – abstract class can be used for

inheritance ONLY.

public class  NewClass : AbsClass {

// Ordinary Members of AbsClass are inherited

// and available for use.

// All abstract members MUST be defined here

public int MyMethod() {

// do code

}

public string MyProperty {

get {

return “”;

}

}

} // end of class

Object instance of ‘NewClass’  can be created.

NewClass nc = new NewClass();

nc.Property1

nc.MyProperty

nc.Method1()

nc.MyMethod()

——–

Sealed Class

——————–

sealed keyword

A sealed class cannot ‘Inherite’. Inheritance is

not allowed to avoid ‘Un-wanted’ derivation

of the class.

‘Object Instantiation’ is possible with a sealed class.

So constructors are available. ‘new’ operator can be

used for ‘object’ creation.

—–

System.Data.SqlClient Namespace

– Used for SQL Server 2000, 2005, 2008 ONLY.

SqlConnection, SqlCommand, SqlDataAdapter,

SqlDataReader are all ‘sealed classes’.

– Un-wanted derivation is NOT allowed.

System.Data.Common namespace

– DbConnection

– DbCommand

– DbDataAdapter

– DbDataReader classes.

[All are inheritable- Not sealed.]

‘Sealed class’  -vs- Structure

————————————-

Both CANNOT inherit.

sealed class is reference type.

A sealed class can be created by inheriting another class.

Access modifer should be used with a sealed class.

structure is value type.

structure cannot inherit.

structure need no ‘access modifier’. It is public.

 

* Differences Interface, Abstract and sealed classes

About

Hi, This is R K Gupta, an IT professional and Entrepreneur. I have written many articles yet for my websites gsesoftsolutions.com, Hope you like it. Kindly subscribe our feed for any updates. Add me in Facebook and Google+

Posted in Microsoft .Net Help, Software Development