Oops .NET questions

download Oops .NET questions

of 37

Transcript of Oops .NET questions

  • 8/13/2019 Oops .NET questions

    1/37

    What is Object? Objects are programming constructs that have data, behavior, and identity. Object data iscontained in the fields, properties, and events of the object, and object behaviors are defined bythe methods and interfaces of the object.Objects have identity two objects with the same set of data are not necessarily the same

    object.Objects in C# are defined through classes and structs these form the single blueprint fromwhich all objects of that type operate.Objects have the following properties: Everything you use in C# is an object, including Windows Forms and controls. Objects are instantiated; that is, they are created from templates defined by classes and structs. Objects use Properties to obtain and change the information they contain. Objects often have methods and events that allow them to perform actions. All C# objects inherit from the Object.What is Class? Classes are symbolic representations of objects; classes describe the properties, fields, methods,

    and events that form objects. Classes control user access to object items through encapsulation.a class defines the data and behavior of the data type. Programmers can then create objects thatare instances of this class.Classes have the following properties: a class can inherit implementation from only one base class. A class can implement more than one interface. Class definitions can be split between different source files. Static classes are sealed classes that contain only static methods.What is the difference between class and object?Classes describe the structure of objects, while objects are usable instances of classes. Eachinstance is an exact yet distinct copy of its class. Because an object is an instance of a class, theact of creating an object is called instantiation.Usually, changing the data in one object does not change the data in any other object.How will you create objects? A class defines a type of object, but it is not an object itself. An object is a concrete entity basedon a class, and is sometimes referred to as an instance of a class.Objects can be created by using the new keyword followed by the name of the class that theobject will be based on, like this:Customer object1 = new Customer();When an instance of a class is created, a reference to the object is passed back to the

    programmer.

    What is encapsulation? Encapsulation is the ability to contain and control access to a group of associated items. Classes

    provide one of the most common ways to encapsulate items. Encapsulation allows you to controlhow the data and procedures are used. You can use access modifiers, such as Private orProtected, to prevent outside procedures from executing class methods or reading and modifyingdata in properties and fields. You should declare internal details of a class as Private to preventthem from being used outside of your class; this technique is called data hiding.One basic rule ofencapsulation is that class data should be modified or retrieved only via Property procedures or

    http://ms-help//MS.MSDNQTR.v90.en/dv_csref/html/e295a8a2-b357-4ee7-a12e-385a44146fa8.htmhttp://ms-help//MS.MSDNQTR.v90.en/dv_csref/html/e295a8a2-b357-4ee7-a12e-385a44146fa8.htmhttp://ms-help//MS.MSDNQTR.v90.en/dv_csref/html/e295a8a2-b357-4ee7-a12e-385a44146fa8.htmhttp://ms-help//MS.MSDNQTR.v90.en/dv_csref/html/e295a8a2-b357-4ee7-a12e-385a44146fa8.htm
  • 8/13/2019 Oops .NET questions

    2/37

    methods. Hiding the implementation details of your classes prevents classes from being used inundesired ways, and lets you to later modify such items without risk of compatibility problems.

    What is Inheritance? You can extend the functionality of an existing class by creating a new class that derives from

    the existing class. The derived class inherits the properties of the base class, and you can add oroverride methods and properties as required.In C#, both inheritance and interface implementation are defined by the : operator.The base class should always be leftmost in the class declaration.C# does not support multiple inheritance meaning that classes cannot inherit from more than oneclass. You can,

    however, use interfaces for that purpose

    e.g: public class CoOrds

    {Private int x,y;Public coOrds(){x=0;y =0;}

    Public int X{get {return x;}set { x =value;}}

    Public int Y{ get { return y;}Set { y=value ;}}}

    You derive a new class, called ColorCoOrds, from the CoOrds class, as follows:

    ColorCoOrds : CoOrds{

    }ColorCoOrds then inherits all the fields and methods of the base class, to which you can add newones to provide extra features in the derived class according to our needs.

    The constructor of the derived class implicitly calls the constructor for the base class. In

  • 8/13/2019 Oops .NET questions

    3/37

    inheritance, all base class constructors are called before the derived class's constructors in theorder that the classes appear in the class hierarchy.

    How will you typecast to a baseclass?you cannot use a reference to a base class to access the members and methods of a derived class

    even if the base class reference may contain a valid reference to an object of the derived type.You can reference a derived class with a reference to the derived type implicitly:ColorCoOrds color1 = new ColorCoOrds();CoOrds coords1 = color1;In this code, the base class reference, coords1, contains a copy of the color1 reference.

    What is the use of base keyword? You can access base class members in a subclass even when those base members are overriddenin the superclass using the base keyword.For instance, you can create a derived class which contains a method with the same signature asin the base class. If you prefaced that method with the new keyword, you indicate that this is an

    all-new method belonging to the derived class. You could still provide a method for accessingthe original method in the base class with the base keyword.For instance, say your base CoOrds class had a method called Invert(), which swaps the x and ycoordinates. You could provide a substitute for this method in your derived ColorCoOrds classwith code like this:

    public new void Invert(){int temp = X;X = Y;Y = temp;screenColor = System.Drawing.Color.Gray;}As you can see, this method swaps x and y, and then sets the coordinates' color to gray. Youcould provide access to the base implementation for this method by creating another method inColorCoOrds, such as this one:

    public void BaseInvert(){

    base.Invert();}You then invoke the base method on a ColorCoOrds object by calling the BaseInvert() method.ColorCoOrds color1 = new ColorCoOrds();color1.BaseInvert();Remember that you would get the same effect if you assigned a reference to the base class to aninstance of ColorCoOrds, and then accessed its methods:CoOrds coords1 = color1;coords1.Invert();

    In case of Base class constructor and derived class constructor which one executed first?Base class objects are always constructed before any deriving class. Thus the constructor for the

    base class is executed before the constructor of the derived class. If the base class has more than

  • 8/13/2019 Oops .NET questions

    4/37

    one constructor, the derived class can decide the constructor to be called.

    e.g: public class CoOrds{

    private int x, y;

    public CoOrds(){x = 0;y = 0;}

    public CoOrds(int x, int y){this.x = x;this.y = y;

    }}

    You could then change the ColorCoOrds class to use a particular one of the availableconstructors using the base keyword:

    public class ColorCoOrds : CoOrds{

    public System.Drawing.Color color;

    public ColorCoOrds() : base (){color = System.Drawing.Color.Red;}

    public ColorCoOrds(int x, int y) : base (x, y){color = System.Drawing.Color.Red;}}

    What is Method Overriding?

    A derived class may override the method of a base class by providing a new implementation forthe declared method.methods must be explicitly marked as virtual using the virtual modifier. Property accessors, aswell as methods, can be overridden in much the same way.

    What is virtual method? A method that is to be overridden in a derived class is declared with the virtual modifier. In aderived class, the overridden method is declared using the override modifier.

  • 8/13/2019 Oops .NET questions

    5/37

    The override modifier denotes a method or a property of a derived class that replaces one withthe same name and signature in the base class. The base method, which is to be overridden, must

    be declared as virtual, abstract, or override: it is not possible to override a non-virtual or staticmethod in this way. Both the overridden and the overriding method or property must have thesame access-level modifiers.

    What is polymorphism? ability of a derived class to redefine, or override, methods that it inherits from a base class.Explain About Access modifiers? Access modifiers are keywords used to specify the declared accessibility of a member or a type.This section introduces the four access modifiers: public protected internal privateThe following five accessibility levels can be specified using the access modifiers:

    public: Access is not restricted. protected: Access is limited to the containing class or types derived from the containing class.Internal: Access is limited to the current assembly.

    protected internal: Access is limited to the current assembly or types derived from the containingclass.

    private: Access is limited to the containing type.Difference between const and readonly?The readonly keyword differs from the const keyword. A const field can only be initialized at thedeclaration of the field. A readonly field can be initialized either at the declaration or in aconstructor. Therefore, readonly fields can have different values depending on the constructorused. Also, although a const field is a compile-time constant, the readonly field can be used forrun-time constants, as in this line: public static readonly uint l1 = (uint)DateTime.Now.Ticks;What is Property? Properties are members that provide a flexible mechanism to read, write, or compute the valuesof private fields. Properties can be used as if they are public data members, but they are actuallyspecial methods called accessors. This enables data to be accessed easily and still helps promotethe safety and flexibility of methods. Properties enable a class to expose a public way of getting and setting values, while hidingimplementation or verification code. A get property accessor is used to return the property value, and a set accessor is used to assigna new value. These accessors can have different access levels. For more information The value keyword is used to define the value being assigned by the set indexer. Properties that do not implement a set method are read only. For simple properties that require no custom accessor code, consider the option of using auto-implemented properties.What is Indexer? Indexers are a syntactic convenience that enable you to create a class, struct, or interface thatclient applications can access just as an array. Indexers are most frequently implemented in typeswhose primary purpose is to encapsulate an internal collection or array.What is Abstract class?

  • 8/13/2019 Oops .NET questions

    6/37

    The abstract keyword enables you to create classes and class members solely for the purpose ofinheritance to define features of derived, non-abstract classes.An abstract class cannot be instantiated. The purpose of an abstract class is to provide a commondefinition of a base class that multiple derived classes can share.What is sealed class?

    A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class.Sealed classes are primarily used to prevent derivation. Because they can never be used as a baseclass, some run-time optimizations can make calling sealed class members slightly faster.A class member, method, field, property, or event, on a derived class that is overriding a virtualmember of the base class can declare that member as sealed.

    This negates the virtual aspect of the member for any further derived class. This is accomplished by putting the sealed keyword before the override keyword in the class member declaration. Forexample:

    public class D : C

    { public sealed override void DoWork() { }}

    What is interface? Interfaces describe a group of related functionalities that can belong to any class or struct.Interfaces can consist of methods, properties, events, indexers, or any combination of those fourmember types. An interface cannot contain fields. Interfaces members are automatically public. A class or struct can inherit more than one interface. When a class or struct inherits an interface, it inherits only the method names and signatures,

    because the interface itself contains no implementations. To implement an interface member, the corresponding member on the class must be public,non-static, and have the same name and signature as the interface member. Properties andindexers on a class can define extra accessors for a property or indexer defined on an interface. Interfaces and interface members are abstract; interfaces do not provide a defaultimplementation.

    What is pure virtual function?

    Posted by: Charugoel

    When you define only function prototype in a base class without and do the complete implementationin derived class. This base class is called abstract class and client wont able to instantiate an object usingthis base class.

    A pure virtual function is a function that must be overridden in a derived class and need not be defined.

    http://www.dotnetfunda.com/interview/exam333-what-is-pure-virtual-function.aspxhttp://www.dotnetfunda.com/interview/exam333-what-is-pure-virtual-function.aspxhttp://www.dotnetfunda.com/profile/charugoel.aspxhttp://www.dotnetfunda.com/profile/charugoel.aspxhttp://www.dotnetfunda.com/profile/charugoel.aspxhttp://www.dotnetfunda.com/profile/charugoel.aspxhttp://www.dotnetfunda.com/interview/exam333-what-is-pure-virtual-function.aspxhttp://www.dotnetfunda.com/interview/exam333-what-is-pure-virtual-function.aspx
  • 8/13/2019 Oops .NET questions

    7/37

    A virtual function is declared to be "pure" using the curious "=0"syntax:class Base {public:void f1(); // not virtual

    virtual void f2(); // virtual, not purevirtual void f3() = 0; // pure virtual};

    Can we specify the access modifier for explicitlyimplemented interface method?

    Posted by: Poster

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

    What is Protected access modifier in C#?

    Posted by: Poster

    The protected keyword is a member access modifier. It can only be used in a declaring a function ormethod not in the class ie. a class can't be declared as protected class.

    A protected member is accessible from within the class in which it is declared, and from within any classderived from the class that declare this member. In other words access is limited to within the class

    definition and any class that inherits from the class

    A protected member of a base class is accessible in a derived class only if the access takes place throughthe derived class type.

    For more details see http://msdn.microsoft.com/en-us/library/bcd5672a(VS.71).aspx

    http://www.dotnetfunda.com/interview/exam419-can-we-specify-the-access-modifier-for-explicitly-implemented-interface-met.aspxhttp://www.dotnetfunda.com/interview/exam419-can-we-specify-the-access-modifier-for-explicitly-implemented-interface-met.aspxhttp://www.dotnetfunda.com/interview/exam419-can-we-specify-the-access-modifier-for-explicitly-implemented-interface-met.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam420-what-is-protected-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam420-what-is-protected-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam420-what-is-protected-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam420-what-is-protected-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam419-can-we-specify-the-access-modifier-for-explicitly-implemented-interface-met.aspxhttp://www.dotnetfunda.com/interview/exam419-can-we-specify-the-access-modifier-for-explicitly-implemented-interface-met.aspxhttp://www.dotnetfunda.com/interview/exam419-can-we-specify-the-access-modifier-for-explicitly-implemented-interface-met.aspx
  • 8/13/2019 Oops .NET questions

    8/37

    What is Public access modifier in C#?

    Posted by: Poster

    The public keyword is an access modifier for types and type members ie. we can declare a class or itsmember (functions or methods) as Public. There are no restrictions on accessing public members.

    What is Private access modifier in C#?

    Posted by: Poster

    The private keyword is a member access modifier ie. we can't explicitly declare a class as Private,however if do not specify any access modifier to the class, its scope will be assumed as Private. Privateaccess is the least permissive access level of all access modifiers.

    Private members are accessible only within the body of the class or the struct in which they are

    declared. This is the default access modifier for the class declaration.

    For more details, see http://msdn.microsoft.com/en-us/library/st6sy9xe(VS.71).aspx

    What is Internal access modifier in C#?

    Posted by: Poster

    The internal keyword is an access modifier for types and type members ie. we can declare a class asinternal or its member as internal. Internal members are accessible only within files in the sameassembly (.dll). In other words, access is limited exclusively to classes defined within the current projectassembly.

    http://www.dotnetfunda.com/interview/exam421-what-is-public-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam421-what-is-public-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam422-what-is-private-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam422-what-is-private-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam423-what-is-internal-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam423-what-is-internal-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam423-what-is-internal-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam423-what-is-internal-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam422-what-is-private-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam422-what-is-private-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam421-what-is-public-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam421-what-is-public-access-modifier-in-csharp.aspx
  • 8/13/2019 Oops .NET questions

    9/37

    For more details see http://msdn.microsoft.com/en-us/library/7c5ka91b(VS.71).aspx

    What is Protected Internal access modifier in C#?

    Posted by: Poster

    Protected Internal is a access modifiers for the members (methods or functions) ie. you can't declare aclass as protected internal explicitly. The members access is limited to the current assembly or typesderived from the containing class.

    Protected Internal means the method is accessible by anything that can access the protected methodUNION with anything that can access the internal method.

    For more details read http://haacked.com/archive/2007/10/29/what-does-protected-internal-mean.aspx

    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 accessmodifiers:publicinternal

    privateprotected internal

    An interface has default modifier as public

    A struct has default modifier as Internal and it can declare its members (methods etc) with followingaccess modifiers:

    http://www.dotnetfunda.com/interview/exam424-what-is-protected-internal-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam424-what-is-protected-internal-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam425-default-access-modifiers-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam425-default-access-modifiers-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam425-default-access-modifiers-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam425-default-access-modifiers-in-csharp.aspxhttp://www.dotnetfunda.com/profile/poster.aspxhttp://www.dotnetfunda.com/interview/exam424-what-is-protected-internal-access-modifier-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam424-what-is-protected-internal-access-modifier-in-csharp.aspx
  • 8/13/2019 Oops .NET questions

    10/37

    publicinternalprivate

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

    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.

    public class MultiplyNumbers

    {

    public int Multiply(int a, int b)

    {

    return a * b;

    }

    public int Multiply(int a, int b, int c)

    {

    return a*b*c;

    }

    }

    To call the above method, you can use following code.

    MultiplyNumbers mn = new MultiplyNumbers();

    int number = mn.Multiply(2, 3) // result = 6

    int number1 = mn.Multiply(2, 3, 4) // result = 24

    http://www.dotnetfunda.com/interview/exam453-what-is-method-overloading.aspxhttp://www.dotnetfunda.com/interview/exam453-what-is-method-overloading.aspxhttp://www.dotnetfunda.com/profile/raja.aspxhttp://www.dotnetfunda.com/profile/raja.aspxhttp://www.dotnetfunda.com/profile/raja.aspxhttp://www.dotnetfunda.com/profile/raja.aspxhttp://www.dotnetfunda.com/interview/exam453-what-is-method-overloading.aspxhttp://www.dotnetfunda.com/interview/exam453-what-is-method-overloading.aspx
  • 8/13/2019 Oops .NET questions

    11/37

    You can't have a overload method with same number parameters but different return type. In order tocreate overload method, the return type must be the same and parameter type must be different ordifferent in numbers.

    What is Overriding?

    Posted by: Raja

    Method overriding is a feature that allows to invoke functions (that have the same signatures) and thatbelong to different classes in the same hierarchy of inheritance using the base class reference. In C# it isdone using keywords virtual and overrides .

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

    What is Method overloading?

    Posted by: Babu_akkandi

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

    What is Method Overriding? How to override a function inC#?

    Posted by: Babu_akkandi

    Use the override modifier to modify a method, a property, an indexer, or an event. An override method

    http://www.dotnetfunda.com/interview/exam456-what-is-overriding.aspxhttp://www.dotnetfunda.com/interview/exam456-what-is-overriding.aspxhttp://www.dotnetfunda.com/profile/raja.aspxhttp://www.dotnetfunda.com/profile/raja.aspxhttp://www.dotnetfunda.com/profile/raja.aspxhttp://www.codeproject.com/KB/cs/cs_methodoverride.aspxhttp://www.codeproject.com/KB/cs/cs_methodoverride.aspxhttp://www.codeproject.com/KB/cs/cs_methodoverride.aspxhttp://www.dotnetfunda.com/interview/exam488-what-is-method-overloading.aspxhttp://www.dotnetfunda.com/interview/exam488-what-is-method-overloading.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/interview/exam489-what-is-method-overriding-how-to-override-a-function-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam489-what-is-method-overriding-how-to-override-a-function-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam489-what-is-method-overriding-how-to-override-a-function-in-csharp.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/interview/exam489-what-is-method-overriding-how-to-override-a-function-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam489-what-is-method-overriding-how-to-override-a-function-in-csharp.aspxhttp://www.dotnetfunda.com/interview/exam489-what-is-method-overriding-how-to-override-a-function-in-csharp.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/interview/exam488-what-is-method-overloading.aspxhttp://www.dotnetfunda.com/interview/exam488-what-is-method-overloading.aspxhttp://www.codeproject.com/KB/cs/cs_methodoverride.aspxhttp://www.dotnetfunda.com/profile/raja.aspxhttp://www.dotnetfunda.com/interview/exam456-what-is-overriding.aspxhttp://www.dotnetfunda.com/interview/exam456-what-is-overriding.aspx
  • 8/13/2019 Oops .NET questions

    12/37

    provides a new implementation of a member inherited from a base class. The method overridden by anoverride declaration is known as the overridden base method. The overridden base method must havethe same signature as the override method.

    You cannot override a non-virtual or static method. The overridden base method must be virtual,

    abstract, or override.

    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.

    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 inheritedmember, declare it in the derived class using the same name, and modify it with the new modifier.

    Difference between new and override keyword?

    http://www.dotnetfunda.com/interview/exam490-can-we-call-a-base-class-method-without-creating-instance.aspxhttp://www.dotnetfunda.com/interview/exam490-can-we-call-a-base-class-method-without-creating-instance.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/interview/exam491-in-which-cases-you-use-override-and-new-base.aspxhttp://www.dotnetfunda.com/interview/exam491-in-which-cases-you-use-override-and-new-base.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/interview/exam647-difference-between-new-and-override-keyword.aspxhttp://www.dotnetfunda.com/interview/exam647-difference-between-new-and-override-keyword.aspxhttp://www.dotnetfunda.com/interview/exam647-difference-between-new-and-override-keyword.aspxhttp://www.dotnetfunda.com/interview/exam647-difference-between-new-and-override-keyword.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/interview/exam491-in-which-cases-you-use-override-and-new-base.aspxhttp://www.dotnetfunda.com/interview/exam491-in-which-cases-you-use-override-and-new-base.aspxhttp://www.dotnetfunda.com/profile/babu_akkandi.aspxhttp://www.dotnetfunda.com/interview/exam490-can-we-call-a-base-class-method-without-creating-instance.aspxhttp://www.dotnetfunda.com/interview/exam490-can-we-call-a-base-class-method-without-creating-instance.aspx
  • 8/13/2019 Oops .NET questions

    13/37

    Posted by: Virendradugar

    Let me explain this through code.

    using System;

    using System.Data;

    using System.Text;

    using System.Windows.Forms;

    namespace BaseDerive

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)

    {

    BaseClass b = new BaseClass();

    b.func1();

    DeriveClass d = new DeriveClass();

    d.func1();

    //Calls Base class function 1 as new keyword is used.

    BaseClass bd = new DeriveClass();

    bd.func1();

    http://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspx
  • 8/13/2019 Oops .NET questions

    14/37

    //Calls Derived class function 2 as override keyword is used.

    BaseClass bd2 = new DeriveClass();

    bd2.func2();

    }

    }

    public class BaseClass

    {

    public virtual void func1()

    {

    MessageBox.Show("Base Class function 1.");

    }

    public virtual void func2()

    {

    MessageBox.Show("Base Class function 2.");

    }

    public void func3()

    {

    MessageBox.Show("Base Class function 3.");

    }

    }

    public class DeriveClass : BaseClass

    {

    public new void func1()

  • 8/13/2019 Oops .NET questions

    15/37

    {

    MessageBox.Show("Derieve Class fuction 1 used new keyword");

    }

    public override void func2()

    {

    MessageBox.Show("Derieve Class fuction 2 used override keyword");

    }

    public void func3()

    {

    MessageBox.Show("Derieve Class fuction 3 used override keyword");

    }

    }

    }

    This is a window application so all the code for calling the function through objects is written inForm_Load event.As seen in above code, I have declared 2 classes. One works as a Base class and second is a derieve classderived from base class.

    Now the difference is

    new: hides the base class function.

    Override: overrides the base class function.

    BaseClass objB = new DeriveClass();

    If we create object like above notation and make a call to any function which exists in base class andderive class both, then it will always make a call to function of base class. If we have overidden the

  • 8/13/2019 Oops .NET questions

    16/37

    method in derive class then it wlll call the derive class function.

    For example

    objB.func1(); //Calls the base class function. (In case of new keyword)

    objB.func2(); //Calls the derive class function. (Override)

    objB.func3(); //Calls the base class function.(Same prototype in both theclass.)

    Note:// This will throw a compile time error. (Casting is required.)

    DeriveClass objB = new BaseClass();

    //This will throw run time error. (Unable to cast)

    DeriveClass objB = (DeriveClass) new BaseClass();

    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. Wecan use the private constructor in singleton pattern.

    If you declare a Constructor as private then it doesnt allow to create object for its derived class, i.e youloose inherent facility for that class.

    Example:

    Class A

    {

    // some code

    Private Void A()

    {

    //Private Constructor

    http://www.dotnetfunda.com/interview/exam661-what-is-a-private-constructor-where-will-you-use-it.aspxhttp://www.dotnetfunda.com/interview/exam661-what-is-a-private-constructor-where-will-you-use-it.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/interview/exam661-what-is-a-private-constructor-where-will-you-use-it.aspxhttp://www.dotnetfunda.com/interview/exam661-what-is-a-private-constructor-where-will-you-use-it.aspx
  • 8/13/2019 Oops .NET questions

    17/37

    }

    }

    Class B:A

    {

    //code

    }

    B obj = new B();// will give Compilation Error

    Because Class A constructor declared as private hence its accessibility limit is to that class only, Class Bcan't access. When we create an object for Class B that constructor will call constructor A but class Bhave no rights to access the Class A constructor hence we will get compilation error.

    Can we declare private class in a Namespace?

    Posted by: Virendradugar

    No. If you try to create a private class in a Namespace, Compiler will throw a compile time errorNamespace elements cannot be explicitly declared as private, protected, or protected internal.

    Reason: The message says it all. Classes can only be declared as private, protected or protected internalwhen declared as nested classes, other than that, it doesn't make sense to declare a class with a visibilitythat makes it unusable, even in the same module. Top level classes cannot be private, they are"internal" by default, and you can just make them public to make them visible from outside your DLL.

    What is Polymorphism?

    http://www.dotnetfunda.com/interview/exam662-can-we-declare-private-class-in-a-namespace.aspxhttp://www.dotnetfunda.com/interview/exam662-can-we-declare-private-class-in-a-namespace.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/interview/exam854-what-is-polymorphism.aspxhttp://www.dotnetfunda.com/interview/exam854-what-is-polymorphism.aspxhttp://www.dotnetfunda.com/interview/exam854-what-is-polymorphism.aspxhttp://www.dotnetfunda.com/interview/exam854-what-is-polymorphism.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/interview/exam662-can-we-declare-private-class-in-a-namespace.aspxhttp://www.dotnetfunda.com/interview/exam662-can-we-declare-private-class-in-a-namespace.aspx
  • 8/13/2019 Oops .NET questions

    18/37

    Posted by: Initiotech

    In OPPS, polymorphism(Greek meaning having multiple forms) is the ablity of being able to assign adifferent meaning or usage to something in different contexts - specifically, to allow an entity such as a afunction, or an object to have more than one forms.

    In C# :Parent classes may define and implement virtual methods(Which is done using the virtual keyword),and derived classes can override them(using the override keyword), which means they provide theirown definition and implementation.At run- time, when users code calls the method, t he CLR looks upthe run-time type of the object, and invokes that override of the virtual method. Thus in your sourcecode when a method of the base class is called it executes the overriden method.====================================================Regards Hefin Dsouza.

    What Are Attributes in DotNet?

    Posted by: Initiotech

    An Attribute is a declarative tag which can be used to provide information to the compiler about thebehaviour of the C# elements such as classes and assemblies.

    C# provides convenient technique that will handle tasks such as performing compile time operations ,changing the behaviour of a method at runtime or maybe even handle unmanaged code.C# Provides many Built-in Attributes

    Some Popular ones are

    - Obsolete- DllImport- Conditional- WebMethod

    and Many more.Members please keep on posting more responses providing more In-Built attributes.

    Regards Hefin Dsouza

    http://www.dotnetfunda.com/profile/initiotech.aspxhttp://www.dotnetfunda.com/profile/initiotech.aspxhttp://www.dotnetfunda.com/profile/initiotech.aspxhttp://www.dotnetfunda.com/interview/exam855-what-are-attributes-in-dotnet.aspxhttp://www.dotnetfunda.com/interview/exam855-what-are-attributes-in-dotnet.aspxhttp://www.dotnetfunda.com/profile/initiotech.aspxhttp://www.dotnetfunda.com/profile/initiotech.aspxhttp://www.dotnetfunda.com/profile/initiotech.aspxhttp://www.dotnetfunda.com/profile/initiotech.aspxhttp://www.dotnetfunda.com/interview/exam855-what-are-attributes-in-dotnet.aspxhttp://www.dotnetfunda.com/interview/exam855-what-are-attributes-in-dotnet.aspxhttp://www.dotnetfunda.com/profile/initiotech.aspx
  • 8/13/2019 Oops .NET questions

    19/37

    What can you do to make class available for inheritance butyou need to prevent it's method to come in inheritancechain?

    Posted by: Virendradugar

    Well, Declare a class with public access specifier and mark all it's method to sealed . As anything which isdeclared with sealed keyword cannot be inherited.

    What's the Difference between Interface and Abstract Class

    Posted by: Puneet20884

    Abstract Class:Have constructors.

    Not necessarily for the class inheriting it to Implement all the Methods.Doesn't Support Multiple Inheritance.

    Where everything is Opposite in the Interfaces.

    What are the various types of Constructors

    Posted by: Puneet20884

    Public : Accessible to AllPrivate: Those classes in which only static members are there and you don't want there objects to becreated in any class.Static: Used for initializing only the static members of the class. These will be invoked for the very first

    http://www.dotnetfunda.com/interview/exam1619-what-can-you-do-to-make-class-available-for-inheritance-but-you-need-to-pre.aspxhttp://www.dotnetfunda.com/interview/exam1619-what-can-you-do-to-make-class-available-for-inheritance-but-you-need-to-pre.aspxhttp://www.dotnetfunda.com/interview/exam1619-what-can-you-do-to-make-class-available-for-inheritance-but-you-need-to-pre.aspxhttp://www.dotnetfunda.com/interview/exam1619-what-can-you-do-to-make-class-available-for-inheritance-but-you-need-to-pre.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/interview/exam1715-whats-the-difference-between-interface-and-abstract-class.aspxhttp://www.dotnetfunda.com/interview/exam1715-whats-the-difference-between-interface-and-abstract-class.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1721-what-are-the-various-types-of-constructors.aspxhttp://www.dotnetfunda.com/interview/exam1721-what-are-the-various-types-of-constructors.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1721-what-are-the-various-types-of-constructors.aspxhttp://www.dotnetfunda.com/interview/exam1721-what-are-the-various-types-of-constructors.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1715-whats-the-difference-between-interface-and-abstract-class.aspxhttp://www.dotnetfunda.com/interview/exam1715-whats-the-difference-between-interface-and-abstract-class.aspxhttp://www.dotnetfunda.com/profile/virendradugar.aspxhttp://www.dotnetfunda.com/interview/exam1619-what-can-you-do-to-make-class-available-for-inheritance-but-you-need-to-pre.aspxhttp://www.dotnetfunda.com/interview/exam1619-what-can-you-do-to-make-class-available-for-inheritance-but-you-need-to-pre.aspxhttp://www.dotnetfunda.com/interview/exam1619-what-can-you-do-to-make-class-available-for-inheritance-but-you-need-to-pre.aspxhttp://www.dotnetfunda.com/interview/exam1619-what-can-you-do-to-make-class-available-for-inheritance-but-you-need-to-pre.aspx
  • 8/13/2019 Oops .NET questions

    20/37

    time the class is being loaded on the memory. They cannot accept any arguments. Static Constructorscannot have any access modifiers.Intern: implementations of the abstract class to the assembly defining the class. A class containing aninternal constructor cannot be instantiated outside of the assembly (Namespace).and External

    What are Constructors ?

    Posted by: Puneet20884

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

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

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

    Constructors do not return a valueConstructors can be overloaded

    When to Use Abstract Classes and When Interfaces.

    Posted by: Puneet20884

    If you anticipate creating multiple versions of your component, create an abstract class. Abstract classesprovide a simple and easy way to version your components. By updating the base class, all inheritingclasses are automatically updated with the change. Interfaces, on the other hand, cannot be changedonce created. If a new version of an interface is required, you must create a whole new interface.

    If the functionality you are creating will be useful across a wide range of disparate objects, use aninterface. Abstract classes should be used primarily for objects that are closely related, whereas

    http://www.dotnetfunda.com/interview/exam1723-what-are-constructors.aspxhttp://www.dotnetfunda.com/interview/exam1723-what-are-constructors.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1729-when-to-use-abstract-classes-and-when-interfaces.aspxhttp://www.dotnetfunda.com/interview/exam1729-when-to-use-abstract-classes-and-when-interfaces.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1729-when-to-use-abstract-classes-and-when-interfaces.aspxhttp://www.dotnetfunda.com/interview/exam1729-when-to-use-abstract-classes-and-when-interfaces.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1723-what-are-constructors.aspxhttp://www.dotnetfunda.com/interview/exam1723-what-are-constructors.aspx
  • 8/13/2019 Oops .NET questions

    21/37

    interfaces are best suited for providing common functionality to unrelated classes.

    If you are designing small, concise bits of functionality, use interfaces. If you are designing largefunctional units, use an abstract class.

    If you want to provide common, implemented functionality among all implementations of yourcomponent, use an abstract class. Abstract classes allow you to partially implement your class, whereasinterfaces contain no implementation for any members.

    Diversities between an abstract method & virtual method ?

    Posted by: Puneet20884

    An Abstract method does not provide an implementation and forces overriding to the deriving class(unless the deriving class also an abstract class), where as the virtual method has an implementation andleaves an option to override it in the deriving class. Thus Virtual method has an implementation &provides the derived class with the option of overriding it. Abstract method does not provide animplementation & forces the derived class to override the method.

    What is Early binding and late binding?

    Posted by: Puneet20884

    Calling a non-virtual method, decided at a compile time is known as early binding. Calling a virtualmethod (Pure Polymorphism), decided at a runtime is known as late binding.

    Difference between ASP Session and ASP.NET Session?

    http://www.dotnetfunda.com/interview/exam1899-diversities-between-an-abstract-method-virtual-method.aspxhttp://www.dotnetfunda.com/interview/exam1899-diversities-between-an-abstract-method-virtual-method.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1916-what-is-early-binding-and-late-binding.aspxhttp://www.dotnetfunda.com/interview/exam1916-what-is-early-binding-and-late-binding.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1918-difference-between-asp-session-and-aspnet-session.aspxhttp://www.dotnetfunda.com/interview/exam1918-difference-between-asp-session-and-aspnet-session.aspxhttp://www.dotnetfunda.com/interview/exam1918-difference-between-asp-session-and-aspnet-session.aspxhttp://www.dotnetfunda.com/interview/exam1918-difference-between-asp-session-and-aspnet-session.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1916-what-is-early-binding-and-late-binding.aspxhttp://www.dotnetfunda.com/interview/exam1916-what-is-early-binding-and-late-binding.aspxhttp://www.dotnetfunda.com/profile/puneet20884.aspxhttp://www.dotnetfunda.com/interview/exam1899-diversities-between-an-abstract-method-virtual-method.aspxhttp://www.dotnetfunda.com/interview/exam1899-diversities-between-an-abstract-method-virtual-method.aspx
  • 8/13/2019 Oops .NET questions

    22/37

  • 8/13/2019 Oops .NET questions

    23/37

    Can you declare the override method static while the original method is non-static?

    Posted by: Blessybaby

    No, you can't, the signature of the virtual method must remain the same, only the keyword virtual ischanged to keyword override.

    Can you override private virtual methods?

    Posted by: Blessybaby

    No, you cannot access private methods in inherited classes.

    Can you prevent your class from being inherited and becoming a base class for some other classes?

    Posted by: Blessybaby

    Yes, that's what keyword sealed in the class definition is for. The developer trying to derive from yourclass will get a message: cannot inherit from Sealed class WhateverBaseClassName. It's the sameconcept as final class in Java.

    Can you allow class to be inherited, but prevent the method from being over-ridden?

    Posted by: Blessybaby

    Yes, just leave the class public and make the method sealed.

    http://www.dotnetfunda.com/interview/exam1941-can-you-declare-the-override-method-static-while-the-original-method-is-non.aspxhttp://www.dotnetfunda.com/interview/exam1941-can-you-declare-the-override-method-static-while-the-original-method-is-non.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam1942-can-you-override-private-virtual-methods.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam1943-can-you-prevent-your-class-from-being-inherited-and-becoming-a-base-class-f.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam1944-can-you-allow-class-to-be-inherited-but-prevent-the-method-from-being-over.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam1945-why-cant-you-specify-the-accessibility-modifier-for-methods-inside-the-int.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam1944-can-you-allow-class-to-be-inherited-but-prevent-the-method-from-being-over.aspxhttp://www.dotnetfunda.com/interview/exam1944-can-you-allow-class-to-be-inherited-but-prevent-the-method-from-being-over.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam1943-can-you-prevent-your-class-from-being-inherited-and-becoming-a-base-class-f.aspxhttp://www.dotnetfunda.com/interview/exam1943-can-you-prevent-your-class-from-being-inherited-and-becoming-a-base-class-f.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam1942-can-you-override-private-virtual-methods.aspxhttp://www.dotnetfunda.com/interview/exam1942-can-you-override-private-virtual-methods.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam1941-can-you-declare-the-override-method-static-while-the-original-method-is-non.aspxhttp://www.dotnetfunda.com/interview/exam1941-can-you-declare-the-override-method-static-while-the-original-method-is-non.aspx
  • 8/13/2019 Oops .NET questions

    24/37

    Why can't you specify the accessibility modifier for methods inside the interface?

    Posted by: Blessybaby

    you are not allowed to specify any accessibility, it's public by default.

    Static datamembers should be initialized inside the constructor. True or False.

    Posted by: Abhisek

    False. Static datamembers should not be initialised inside constructor.

    Static methods can not use non static members. True or False.

    Posted by: Abhisek

    True

    A constructor can be private. True or False

    Posted by: Abhisek

    True. A constructor can be private. We can declare a constructor as private.

    What is the work of a constructor?

    Posted by: Abhisek

    http://www.dotnetfunda.com/interview/exam1945-why-cant-you-specify-the-accessibility-modifier-for-methods-inside-the-int.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam2035-static-datamembers-should-be-initialized-inside-the-constructor-true-or-fa.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2036-static-methods-can-not-use-non-static-members-true-or-false.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2037-a-constructor-can-be-private-true-or-false.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2074-what-is-the-work-of-a-constructor.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2074-what-is-the-work-of-a-constructor.aspxhttp://www.dotnetfunda.com/interview/exam2074-what-is-the-work-of-a-constructor.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2037-a-constructor-can-be-private-true-or-false.aspxhttp://www.dotnetfunda.com/interview/exam2037-a-constructor-can-be-private-true-or-false.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2036-static-methods-can-not-use-non-static-members-true-or-false.aspxhttp://www.dotnetfunda.com/interview/exam2036-static-methods-can-not-use-non-static-members-true-or-false.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2035-static-datamembers-should-be-initialized-inside-the-constructor-true-or-fa.aspxhttp://www.dotnetfunda.com/interview/exam2035-static-datamembers-should-be-initialized-inside-the-constructor-true-or-fa.aspxhttp://www.dotnetfunda.com/profile/blessybaby.aspxhttp://www.dotnetfunda.com/interview/exam1945-why-cant-you-specify-the-accessibility-modifier-for-methods-inside-the-int.aspx
  • 8/13/2019 Oops .NET questions

    25/37

    Constructor creates and initialises the objects in an application.

    Name the operators that cannot be overloaded.

    Posted by: Abhisek

    sizeof..*.->::?:

    OOps Interview

    Posted by: Nrvikas

    ok

    What is "this" pointer?

    Posted by: Abhisek

    This pointer is a pointer which points to the current object of a class. this is actually a keyword which isused as a pointer which differentiate the current object with global object.

    public class Base { public virtual void foo(int x) { Console.WriteLine("Base foo(int)"); } } public classDerived: Base { public void foo(int x) { Console.WriteLine("Derived foo(int)"); } } class Program { static

    http://www.dotnetfunda.com/interview/exam2104-name-the-operators-that-cannot-be-overloaded.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2133-oops-interview.aspxhttp://www.dotnetfunda.com/profile/nrvikas.aspxhttp://www.dotnetfunda.com/profile/nrvikas.aspxhttp://www.dotnetfunda.com/profile/nrvikas.aspxhttp://www.dotnetfunda.com/interview/exam2462-what-is-this-pointer.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam3748-public-class-base-public-virtual-void-fooint-x.aspxhttp://www.dotnetfunda.com/interview/exam3748-public-class-base-public-virtual-void-fooint-x.aspxhttp://www.dotnetfunda.com/interview/exam3748-public-class-base-public-virtual-void-fooint-x.aspxhttp://www.dotnetfunda.com/interview/exam3748-public-class-base-public-virtual-void-fooint-x.aspxhttp://www.dotnetfunda.com/interview/exam3748-public-class-base-public-virtual-void-fooint-x.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2462-what-is-this-pointer.aspxhttp://www.dotnetfunda.com/interview/exam2462-what-is-this-pointer.aspxhttp://www.dotnetfunda.com/profile/nrvikas.aspxhttp://www.dotnetfunda.com/interview/exam2133-oops-interview.aspxhttp://www.dotnetfunda.com/interview/exam2133-oops-interview.aspxhttp://www.dotnetfunda.com/profile/abhisek.aspxhttp://www.dotnetfunda.com/interview/exam2104-name-the-operators-that-cannot-be-overloaded.aspxhttp://www.dotnetfunda.com/interview/exam2104-name-the-operators-that-cannot-be-overloaded.aspx
  • 8/13/2019 Oops .NET questions

    26/37

    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.

    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{

    }

    http://www.dotnetfunda.com/interview/exam3748-public-class-base-public-virtual-void-fooint-x.aspxhttp://www.dotnetfunda.com/profile/santosh.impossible.aspxhttp://www.dotnetfunda.com/profile/santosh.impossible.aspxhttp://www.dotnetfunda.com/profile/santosh.impossible.aspxhttp://www.dotnetfunda.com/interview/exam3881-difference-between-sealed-and-static-classes.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/interview/exam3881-difference-between-sealed-and-static-classes.aspxhttp://www.dotnetfunda.com/interview/exam3881-difference-between-sealed-and-static-classes.aspxhttp://www.dotnetfunda.com/profile/santosh.impossible.aspxhttp://www.dotnetfunda.com/interview/exam3748-public-class-base-public-virtual-void-fooint-x.aspx
  • 8/13/2019 Oops .NET questions

    27/37

    2)They can have static members only.

    Differences between a structure and class

    Posted by: Ddd

    Structure:

    1)It is a Value Type2)Its variable directly contains the data on the stack3)Each structure variable has its independent copyof data.4)One structure cannot inherit other5)They do not have destructors6)They do no have explicit parameterless constructors7)we cannot put sealed /abstract modifiers before the structures.8)Easier memory management9)examples:int, short,long,DateTime,Point(predefined)

    Uses:Structures are typically used for handlingsmall amounts of data or where inheritance, overriding is not requiredexample: 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 object4)One class can inherit the other(unless the class is sealed/static)5)Classes have destructors6)They can have explicit parameterless constructors7)Sealed/abstract modifers can be put before classes.8) Comparitively Difficult memory management

    http://www.dotnetfunda.com/interview/exam3884-differences-between-a-structure-and-class.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/interview/exam3884-differences-between-a-structure-and-class.aspxhttp://www.dotnetfunda.com/interview/exam3884-differences-between-a-structure-and-class.aspx
  • 8/13/2019 Oops .NET questions

    28/37

    9)example: SqlConnection,DataView(predefined classes)

    Classes are typically used where inheritance, overriding is requiredor we need to create objects capable of handling large dataexample: DataSet,ArrayList can handle large data.

    What are the different ways a method can be overloaded?

    Posted by: Ddd

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

    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;

    }

    Difference between a Class and an object

    http://www.dotnetfunda.com/interview/exam3888-what-are-the-different-ways-a-method-can-be-overloaded.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/interview/exam3953-difference-between-a-class-and-an-object.aspxhttp://www.dotnetfunda.com/interview/exam3953-difference-between-a-class-and-an-object.aspxhttp://www.dotnetfunda.com/interview/exam3953-difference-between-a-class-and-an-object.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/interview/exam3888-what-are-the-different-ways-a-method-can-be-overloaded.aspxhttp://www.dotnetfunda.com/interview/exam3888-what-are-the-different-ways-a-method-can-be-overloaded.aspx
  • 8/13/2019 Oops .NET questions

    29/37

  • 8/13/2019 Oops .NET questions

    30/37

    1)Class2)Object3)Inheritance4)Polymorphism

    5)Abstraction6)Encapsulation-----------------------------------------------------Advantages:1)Code reusability: define a class and n number of objects implement the classlogic: 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 isaccomplished 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 caneasily 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 classthat multiple derived classes can share.For example,create an abstract class bank with simple interest functionThis function will be implemented in the derived classes ofthe bank class.

  • 8/13/2019 Oops .NET questions

    31/37

    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.

    Can we have Sealed Method in abstarct class ?

    Posted by: Pradsir

    Looking at first site the The Keyword Sealed & Abstract are contradictory to each other..In simple termswe can Say Answer is NO...

    Look the code below

    using System;

    abstract class A{public abstract void Hello();public sealed void Hi();}

    when we will complie the code.. we will get the Compile time Error as below

    'A.Hi()' cannot be sealed because it is not an override..

    But the Crux is We can have Sealed methods in abstract class when the abstract class is Dervided class ..for Eg.

    using System;

    class A{public virtual void Hello(){Console.WriteLine(" Say Hello");}

    http://www.dotnetfunda.com/interview/exam4013-can-we-have-sealed-method-in-abstarct-class.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/interview/exam4013-can-we-have-sealed-method-in-abstarct-class.aspxhttp://www.dotnetfunda.com/interview/exam4013-can-we-have-sealed-method-in-abstarct-class.aspx
  • 8/13/2019 Oops .NET questions

    32/37

    }

    abstract class B : A{public sealed override void Hello()

    {Console.WriteLine(" Say Hi");}

    }

    class C : B{

    }

    class Demo{public static void Main(){C c1 = new C();c1.Hello();// Output is Say Hi}}

    // Thanks

    Can we have an Abstract class without having any abstract method ??

    Posted by: Pradsir

    Yes we can have Abstract class without having any abstract method ..

    See the code belowusing System;

    http://www.dotnetfunda.com/interview/exam4014-can-we-have-an-abstract-class-without-having-any-abstract-method.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/interview/exam4014-can-we-have-an-abstract-class-without-having-any-abstract-method.aspxhttp://www.dotnetfunda.com/interview/exam4014-can-we-have-an-abstract-class-without-having-any-abstract-method.aspx
  • 8/13/2019 Oops .NET questions

    33/37

    abstract class A{public void Hello(){Console.WriteLine(" Say Hi");

    }}

    class B:A{}

    class Demo{public static void Main(){B b1 = new B();b1.Hello();}}// Output is Say HI

    the class A is abstract class.. but it does not have any abstract methods..

    Thanks

    Can we have Multiple Main Methods in one .cs file

    Posted by: Pradsir

    Yes we can Have multiple Main methods in one .cs file.The crux is we can have Multiple classes in one .cs file; and we can define one Main method in eachclass.

    & while doing compliation we can spcify the compiler to choose the Main methods from the specificclass .

    http://www.dotnetfunda.com/interview/exam4027-can-we-have-multiple-main-methods-in-one-cs-file.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/profile/pradsir.aspxhttp://www.dotnetfunda.com/interview/exam4027-can-we-have-multiple-main-methods-in-one-cs-file.aspxhttp://www.dotnetfunda.com/interview/exam4027-can-we-have-multiple-main-methods-in-one-cs-file.aspx
  • 8/13/2019 Oops .NET questions

    34/37

  • 8/13/2019 Oops .NET questions

    35/37

    }

    }

    We have got two class which we can save in single .cs file say Hello.cs

    while doing compliation we can say

    csc Hello.cs /main:Demo --> In order to choose Main from the Demo class

    and

    csc Hello.cs /main:Test --> In order to choose Main from the Test class

    Happy coding..

    Which of these terms defines the hiding of an object's details from the other program?

    Posted by: Ddd

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

    If the Function has same parameter but different return type (int and float), Is it a overloading?

    Posted by: Radhikalingam

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

    How does Composition mechanism works ?

    http://www.dotnetfunda.com/interview/exam4070-which-of-these-terms-defines-the-hiding-of-an-object39s-details-from-the.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/interview/exam4261-if-the-function-has-same-parameter-but-different-return-type-int-and-float.aspxhttp://www.dotnetfunda.com/profile/radhikalingam.aspxhttp://www.dotnetfunda.com/profile/radhikalingam.aspxhttp://www.dotnetfunda.com/profile/radhikalingam.aspxhttp://www.dotnetfunda.com/interview/exam4479-how-does-composition-mechanism-works.aspxhttp://www.dotnetfunda.com/interview/exam4479-how-does-composition-mechanism-works.aspxhttp://www.dotnetfunda.com/interview/exam4479-how-does-composition-mechanism-works.aspxhttp://www.dotnetfunda.com/profile/radhikalingam.aspxhttp://www.dotnetfunda.com/interview/exam4261-if-the-function-has-same-parameter-but-different-return-type-int-and-float.aspxhttp://www.dotnetfunda.com/interview/exam4261-if-the-function-has-same-parameter-but-different-return-type-int-and-float.aspxhttp://www.dotnetfunda.com/profile/ddd.aspxhttp://www.dotnetfunda.com/interview/exam4070-which-of-these-terms-defines-the-hiding-of-an-object39s-details-from-the.aspxhttp://www.dotnetfunda.com/interview/exam4070-which-of-these-terms-defines-the-hiding-of-an-object39s-details-from-the.aspx
  • 8/13/2019 Oops .NET questions

    36/37

    Posted by: Chvrsri

    This mechanism helps to simplify a complex problem into an easier problem.It generally makes differentclasses and objects to communicate with each other and thus making the problem solved. Itcommunicates with the problem by making different classes and objects to send a message to each

    other.

    What is the advantage of parametric polymorphism ?

    Posted by: Chvrsri

    Generally in Parametric polymorphism the code is written without following any specification for the

    type of data present so this particular code can be used any number of times. Hence code re-usability isachieved.

    When We will create architecture and show one application connect with different type of database likeSQL or Oracle. Which option is suitable for create architecture?

    Posted by: Rajusingh

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

    What is difference in between abstrct classes and interfaces ?

    Posted by: Rahulshukla

    An interface offers an alternative to an abstract class for creating contract among classes and theirclient. The main difference in between abstract class and interface are given bellow1. Abstract classes can have concrete methods while interfaces have no methods implemented.

    2.Interface do not come in inheriting chain,while abstract classes come in inheritance .

    http://www.dotnetfunda.com/profile/chvrsri.aspxhttp://www.dotnetfunda.com/profile/chvrsri.aspxhttp://www.dotnetfunda.com/profile/chvrsri.aspxhttp://www.dotnetfunda.com/interview/exam4480-what-is-the-advantage-of-parametric-polymorphism.aspxhttp://www.dotnetfunda.com/profile/chvrsri.aspxhttp://www.dotnetfunda.com/profile/chvrsri.aspxhttp://www.dotnetfunda.com/profile/chvrsri.aspxhttp://www.dotnetfunda.com/interview/exam4497-when-we-will-create-architecture-and-show-one-application-connect-with-diff.aspxhttp://www.dotnetfunda.com/interview/exam4497-when-we-will-create-architecture-and-show-one-application-connect-with-diff.aspxhttp://www.dotnetfunda.com/profile/rajusingh.aspxhttp://www.dotnetfunda.com/profile/rajusingh.aspxhttp://www.dotnetfunda.com/profile/rajusingh.aspxhttp://www.dotnetfunda.com/interview/exam4537-what-is-difference-in-between-abstrct-classes-and-interfaces.aspxhttp://www.dotnetfunda.com/profile/rahulshukla.aspxhttp://www.dotnetfunda.com/profile/rahulshukla.aspxhttp://www.dotnetfunda.com/profile/rahulshukla.aspxhttp://www.dotnetfunda.com/profile/rahulshukla.aspxhttp://www.dotnetfunda.com/interview/exam4537-what-is-difference-in-between-abstrct-classes-and-interfaces.aspxhttp://www.dotnetfunda.com/interview/exam4537-what-is-difference-in-between-abstrct-classes-and-interfaces.aspxhttp://www.dotnetfunda.com/profile/rajusingh.aspxhttp://www.dotnetfunda.com/interview/exam4497-when-we-will-create-architecture-and-show-one-application-connect-with-diff.aspxhttp://www.dotnetfunda.com/interview/exam4497-when-we-will-create-architecture-and-show-one-application-connect-with-diff.aspxhttp://www.dotnetfunda.com/interview/exam4497-when-we-will-create-architecture-and-show-one-application-connect-with-diff.aspxhttp://www.dotnetfunda.com/profile/chvrsri.aspxhttp://www.dotnetfunda.com/interview/exam4480-what-is-the-advantage-of-parametric-polymorphism.aspxhttp://www.dotnetfunda.com/interview/exam4480-what-is-the-advantage-of-parametric-polymorphism.aspxhttp://www.dotnetfunda.com/profile/chvrsri.aspx
  • 8/13/2019 Oops .NET questions

    37/37

    Overloading is Static Polymorphism and Overriding is Dynamic Polymorphism ? True or False ?

    Posted by: Akiii

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

    Which of the following is not a part of OOPs?

    Posted by: Sabarimahesh

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

    What is the default access modifier of a class?

    Posted by: CGN007

    The default access modifier for a class is internal if it's defined within the same namespace. It is private ifit's defined within another class.

    It can declare members (methods etc) with following access modifiers:publicinternalprivateprotected internal

    http://www.dotnetfunda.com/interview/exam4588-overloading-is-static-polymorphism-and-overriding-is-dynamic-polymorphism.aspxhttp://www.dotnetfunda.com/profile/akiii.aspxhttp://www.dotnetfunda.com/profile/akiii.aspxhttp://www.dotnetfunda.com/profile/akiii.aspxhttp://www.dotnetfunda.com/interview/exam4964-which-of-the-following-is-not-a-part-of-oops.aspxhttp://www.dotnetfunda.com/profile/sabarimahesh.aspxhttp://www.dotnetfunda.com/profile/sabarimahesh.aspxhttp://www.dotnetfunda.com/profile/sabarimahesh.aspxhttp://www.dotnetfunda.com/interview/exam5309-what-is-the-default-access-modifier-of-a-class.aspxhttp://www.dotnetfunda.com/profile/cgn007.aspxhttp://www.dotnetfunda.com/profile/cgn007.aspxhttp://www.dotnetfunda.com/profile/cgn007.aspxhttp://www.dotnetfunda.com/profile/cgn007.aspxhttp://www.dotnetfunda.com/interview/exam5309-what-is-the-default-access-modifier-of-a-class.aspxhttp://www.dotnetfunda.com/interview/exam5309-what-is-the-default-access-modifier-of-a-class.aspxhttp://www.dotnetfunda.com/profile/sabarimahesh.aspxhttp://www.dotnetfunda.com/interview/exam4964-which-of-the-following-is-not-a-part-of-oops.aspxhttp://www.dotnetfunda.com/interview/exam4964-which-of-the-following-is-not-a-part-of-oops.aspxhttp://www.dotnetfunda.com/profile/akiii.aspxhttp://www.dotnetfunda.com/interview/exam4588-overloading-is-static-polymorphism-and-overriding-is-dynamic-polymorphism.aspxhttp://www.dotnetfunda.com/interview/exam4588-overloading-is-static-polymorphism-and-overriding-is-dynamic-polymorphism.aspx