Virendradugar: What Is A Private Constructor? Where Will You Use It?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

What is a private constructor? Where will you use it?

Posted by: Virendradugar


When you declare a Constructor with Private access modifier then it is called Private
Constructor. We can use the private constructor in singleton pattern.

If you declare a Constructor as private then it doesn’t allow to create object for its derived class,
i.e you loose inherent facility for that class.
Hope this helps!!!!!

In which cases you use override and new base?

Posted by: Babu_akkandi


Use the new modifier to explicitly hide a member inherited from a base class. To hide an
inherited member, declare it in the derived class using the same name, and modify it with the
new modifier.

Can we call a base class method without creating instance?

Posted by: Babu_akkandi


Yep. But ..

* Its possible If its a static method.

* Its possible by inheriting from that class also.

* Its possible from derived classes using base keyword.

What is Method overloading?

Posted by: Babu_akkandi


Method overloading occurs when a class contains two methods with the same name, but different
signatures.

What is Overriding?
Posted by: Raja
Method overriding is a feature that allows to invoke functions (that have the same signatures)
and that belong to different classes in the same hierarchy of inheritance using the base class
reference. In C# it is done using keywords virtual and overrides .

For more information visit http://www.codeproject.com/KB/cs/cs_methodoverride.aspx

What is method overloading?

Posted by: Raja


Method overloading allows us to write different version of the same method in a class or derived
class. Compiler automatically select the most appropriate method based on the parameter
supplied.
OOPS

Object Oriented Programming Stuctures:

It is a programming methodology in which the programs are organized as collections of


objects.Each object represents an instance of some class.The classes can be interrelated to each
other through inheritance

OOPS revolves around these concepts:

1)Class
2)Object
3)Inheritance
4)Polymorphism
5)Abstraction
6)Encapsulation
-----------------------------------------------------
Advantages:
1)Code reusability: define a class and n number of objects implement the class
logic: example: Button class and n number of Button objects

2)Inheritance : Eliminates redundant code and extend the use of existing classes.

example: 1)we can create our own TextBox by inheriting from the TextBox class.
2)We can inherit one Windows Frorm into another.

3)Encapsulation: The programmer can hide the data and functions in a class from other
classes.It is accomplished through modifiers like private, protected,
protected internal.
4)Easy to Maintain and Upgrade:
If we want to make changes in a class, we can make them and save the changes in the .dll This
.dll can easily be updated in the client by using Update Reference.

5)Polymorphism provides us with methods extensibility.


we can have different methods with same name, but with different kinds of behavior

ex:
Console.WriteLine("welcome");
Console.WriteLine(100);

6)Abstraction allows us to define a common definition of a base class


that multiple derived classes can share.
For example,
create an abstract class bank with simple interest function
This function will be implemented in the derived classes of
the bank class.
ex: 1)icici class will have its own simple interest.
2)ABC class will have its own simple interest.

icici and ABC both are child classes of bank class.

Difference between a Class and an object

Posted by: Ddd


Class:

1)It is a datatype that contains the programming logic.

2)Class is visible in the source code and resides in hard disk.

3)Class is like a template or blueprint of the object. It implements reusability,

encapsulation, inheritance

example:Button is a class with properties like Text,BackColor,


events like click, methods like Focus

Object:

1)it is a chunk of memory that implements the class logic.


2)Object is in the RAM and not visible in source code.

3)It is the real world implementation of the class.


Each object has its own copy of data.
example: Button1, Button2 are the objects of Button class.

What are the different ways a method can be overloaded?

Posted by: Ddd


Different parameter data types, different number of parameters, different order of
parameters.

example: int area(int a, int b)


{
return a*b; --different number of parameters
}
int area(int b)
{
return a*a;
}

--parameter return types

int calc(int a)
{
return a;
}

double calc(double b)
{
return b*5;
}

Differences between a structure and class

Posted by: Ddd


Structure:

1)It is a Value Type


2)Its variable directly contains the data on the stack
3)Each structure variable has its independent copy
of data.
4)One structure cannot inherit other
5)They do not have destructors
6)They do no have explicit parameterless constructors
7)we cannot put sealed /abstract modifiers before the structures.
8)Easier memory management
9)examples:
int, short,long,DateTime,
Point
(predefined)

Uses:
Structures are typically used for handling
small amounts of data or where inheritance, overriding is not required
example: int a=100;

Class

1)It is a reference Type


2)Its variable has references to the data(data is stored in the object created in the heap) .
3)Two Class variables can refer to the same object
4)One class can inherit the other(unless the class is sealed/static)
5)Classes have destructors
6)They can have explicit parameterless constructors
7)Sealed/abstract modifers can be put before classes.
8) Comparitively Difficult memory management
9)example: SqlConnection,DataView(predefined classes)

Classes are typically used where inheritance, overriding is required


or we need to create objects capable of handling large data
example: DataSet,ArrayList can handle large data.

Difference between sealed and static classes

Posted by: Ddd


sealed classes:

1)we can create their instances, but cannot inherit them

ex:

sealed class demo


{
}

class abc:demo
{
--Wrong
}
2)They can contain static as well as nonstatic members.

static classes:

1)we can neither create their instances, nor inherit them

ex:
static class Program
{

2)They can have static members only.

public class Base { public virtual void foo(int x) { Console.WriteLine("Base foo(int)"); } }


public class Derived: Base { public void foo(int x) { Console.WriteLine("Derived foo(int)");
} } class Program { static void Main(string[] args) { Derived d = new Derived(); int i = 10;
d.foo(i); } }

Posted by: Santosh.impossible


NOTE: This is objective type question, Please click question title for correct answer.

Default Access modifiers in C#?

Posted by: Poster


An enum has default modifier as public

A class has default modifiers as Internal . It can declare members (methods etc) with following
access modifiers:
public
internal
private
protected internal

An interface has default modifier as public


A struct has default modifier as Internal and it can declare its members (methods etc) with
following access modifiers:
public
internal
private

A methods, fields, and properties has default access modifier as "Private" if no modifier is
specified.

What is "this"

What are Constructors ?

Posted by: Puneet20884


Constructors are used for initializing the members of a class whenever an object is created with
the default values for initialization.

If no constructor defined then the CLR will provide an implicit constructor which is called as
Default Constructor.

A class can have any number of constructors provided they vary with the number of arguments
that are passed, which is they should have different signatures.

Constructors do not return a value


Constructors can be overloaded

Can we specify the access modifier for explicitly implemented interface method?

Posted by: Poster


No, we can't specify the access modifier for the explicitly implemented interface method. By
default its scope will be internal.

What is Class?

Posted by: Poster


A Class is the generic definition of what an object is a template.

The keyword class in C# indicates that we are going to define a new class (type of object)

What is Object?

Posted by: Poster


Object is anything that is identifiable as a single material item.

Can Struct be inherited?

Posted by: Raja


No, Struct can't be inherited as this is implicitly sealed.

What is Polymorphisms?

Posted by: Raja


Polymorphism means one interface and many forms. Polymorphism is a characteristics of being
able to assign a different meaning or usage to something in different contexts specifically to
allow an entity such as a variable, a function or an object to have more than one form.

There are two types of Polymorphism.


Compile time: function or operator overloading
Runtime: Inheritence & virtual functions

You might also like