Some Important interview Questions

>>>Difference between data encapsulation vs abstraction

Encapsulation is nothing but hiding information also called datahiding.
while abstraction denotes the essential characteristics of an object which differentiates from other kinds of object.

 

Abstraction is achieved through encapsulation. Abstraction solves the problem in the design side while encapsulations the implementation

 

Abstraction is virtual class design.

Before actually defining class developer will think about what all properties methods and event will be there in my class.

Whereas encapsulation is data hiding.

At the time of class definitions developer will think about which should display to end user and which should not.

In Short Abstraction is Collection of data and Encapsulation is Exposure (or grouping) of data in appropriate access specifier.

Abstraction means data hiding. That means we use the object without knowing the source code of the class.

Encapsulation is a mechanism by which we design our class such a way that if we change our class in feature we don’t need to change the classes which are depending on our classes.

 

Abstraction is used for hiding the unwanted information and giving relevant information.

Eg: Three set of customers are going to buy a bike First one wants information about the style. Second one wants about the mileage. Third one wants the cost and brand of the bike. So the salesperson explains about the product which customer needs what. So he hiding some information and giving the relevant information.

Encapsulation is combines one or more information into a component.

Eg: Capsule is mixed with one or more medicine and packed into the tube. so its related and acting in two moducles.

————————————————

Encapsulation and abstraction both solve same problem: Complexity; but in different dimensions.

Abstraction: Hides the implementation details of your methods. Provides a base to variations in the application which can grow over a period of time.

Encapsulation: Hides the private data elements of the class and exposes only the required things to the clients.

Both are powerful; but using abstraction require more skills than encapsulation and bigger applications/products can not survive with out abstraction.

It is wrong to call Encapsulation as only data hiding. Though this definition is usually taught in school but now according to latest OOPs principles its considered incomplete.

According to the book Design Patterns Explained

At conceptual level:

Encapsulation means any type of hiding. It can be either data behavior implementation or derived classes or any other thing.

Abstraction means to generalize or conceptualize: to step back from the more concrete to the more conceptual or abstract.

At implementation level:
Encapsulation is achieved by inheritance aggregation or composition.

Abstraction is represented by using abstract classes to represent a generalized version of set of related classes.

>>> Sorting a Array

class SortArray

{

static void Main(string[] args)

{

 

string[] names = new string[] { “Rosy”, “Amy”, “Peter”, “Albert” };

Console.WriteLine(“Original Array:”);

foreach (string str in names)

{

Console.WriteLine(str);

}

Console.WriteLine(“Sorted Array:”);

Array.Sort(names);

Array.Reverse(names);

foreach (string str in names)

{

Console.WriteLine(str);

}

 

 

}

}

>>> Connection String in Web.config

<add key=”connStr1″ value=”Data Source=192.168.0.196;Initial Catalog=dbname;Persist Security Info=True;User ID=sa;Password=pass@word1″/>

 

>>> Database.cs class

public class database

{

char DQ = Convert.ToChar(39);

public database()

{

// char DQ = Convert.ToChar(39);

}

public int InsertUpdate(string qry, string connStr)

{

SqlConnection conn = getConnection(connStr);

//  try

//  {

conn.Open();

SqlCommand cmd = new SqlCommand(qry, conn);

int rowsaff = cmd.ExecuteNonQuery();

conn.Close();

return rowsaff;

//  }

//catch (Exception ex)

//{

//    throw new Exception(ex.Message);//”Error in Insert/Update”);

 

//}

}

 

public void Alert(string msg)

{

System.Web.HttpContext.Current.Response.Write(“<script>alert(” + DQ + msg + DQ + “)</script>”);

}

 

public DataSet getDS(string qry, string connStr)

{

DataSet ds = new DataSet();

SqlConnection conn = getConnection(connStr);

try

{

conn.Open();

if (conn.State == ConnectionState.Open)

{

SqlDataAdapter da = new SqlDataAdapter(qry, conn);

da.Fill(ds);

return ds;

}

return ds;

}

catch (Exception ex)

{

throw new Exception(“Error in Getting DataSet” + ex.Message);

}

 

}

public string getOneField(string qry, string connStr)

{

SqlConnection conn = getConnection(connStr);

SqlCommand cmd = new SqlCommand(qry, conn);

conn.Open();

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

if (dr.HasRows == true)

{

dr.Read();

return Convert.ToString(dr[0]);

}

else

{

return “”;

}

 

 

}

 

//Function to create and return connection based on Connection String

public SqlConnection getConnection(string connStr)

{

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings[connStr]);

return conn;

}

About dksinha
I'm not even Low-maintenance, I'm a no-maintenance guy, little-bit careless, take things light, can adjust and manage things in all the situations smoothly, don't like Bon-Ton girls and ....actually don't know more all about myself. My friends can tell you better about me. I think...Life is very interesting when U make mistakes!...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.