Professional Documents
Culture Documents
Answer: OOPs is an Object Oriented Programming language,which is the extension of Procedure OrientedProgramming language.OOPS
reduce the code of the program because of the extensive feature of Polymorphism. OOPS have many properties such as Data-
Hiding,Inheritence,Data Absraction,Data Encapsulation and many moreEverything in the world is an object. The type of the object may
vary. In OOPS, we get the power to create objects of our own, as & when required.
Answer: When more than one function is created with the same name, but different is of there arguments . In other words, function names
can be overloaded. A function may also have the same name as an attribute. In the case that there is an ambiguity between a function on
a complex type and an attribute of the complex type, the attribute will always be used.
Answer:A group of objects that share a common definition and that therefore share common properties, operations, and behavior. A user-
defined type that is defined with the class-key 'class,' 'struct,' or 'union.' Objects of a class type consist of zero or more members and base
classobjects.Classes can be defined hierarchically, allowing one class to be an expansion of another, and classes can restrict access to their
members.
Base class is a root class or superior class from which we can extend classes.it is the topmost in the classes so other classes can be derived
from this class but it is not derived from any class.Depending on procedure base class may or may not give its objects to derived classes of
it.
What is Virtual?
The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. For
example, this method can be overridden by any class that inherits it:
When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most
derived class is called, which might be the original member, if no derived class has overridden the member.
By default, methods are non-virtual. You cannot override a non-virtual method.
You cannot use the virtual modifier with the static, abstract, private or override modifiers.
Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
It is an error to use the virtual modifier on a static property.
A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
Virtual functions implement the concept of polymorphism are the same as in C#, except that you use theoverride keyword with the
virtual function implementaion in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual
method will use the override keyword.
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Shape.Draw") ;
}
}
{
public override void Draw()
{
Console.WriteLine("Rectangle.Draw");
}
}
What is Override?
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or
event.
In this example, the class Square must provide an overridden implementation of Area because Area is inherited from the abstract
ShapesClass:
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have
the same access level modifier.
You cannot use the modifiers new, static, virtual, or abstract to modify an override method.
An overriding property declaration must specify the exact same access modifier, type, and name as the inherited property, and the
overridden property must be virtual, abstract, or override.
Answer:Value Types - Value types are primitive types. Like Int32 maps to System.Int32, double maps to System.double.All value types are
stored on stack and all the value types are derived from System.ValueType. All structures and enumerated types that are derived from
System.ValueType are created on stack, hence known as ValueType.In value type we create a copy of object and uses there value its not
the original one.
Answer: An abstract class and Interface both have method only but not have body of method.The difference between Abstract class and An
Interface is that if u call Ablstract class then u have to call all method of that particular Abstract class but if u call an Interface then it is not
necessary that u call all method of that particular interface.Method OverLoading:-Return type, Parameter type, parameter and body of
method number may be different.Method Overriding:- Return type, Parameter type, Parameter number all must be same . Only body of
method can change.
Polymorphism
There are two powerful aspects to inheritance. One is code reuse. When you create a ListBox class, you’re able to reuse some of the logic in the
base (Window) class.
What is arguably more powerful, however, is the second aspect of inheritance: polymorphism. Poly means many and morphmeans form. Thus,
polymorphism refers to being able to use many forms of a type without regard to the details.
When the phone company sends your phone a ring signal, it does not know what type of phone is on the other end of the line. You might have an
old-fashioned Western Electric phone that energizes a motor to ring a bell, or you might have an electronic phone that plays digital music.
As far as the phone company is concerned, it knows only about the “base type”phone and expects that any “derived” instance of this type knows
how to ring. When the phone company tells your phone to ring, it, effectively, calls your phone’s ring method, and old fashioned phones ring, digital
phones trill, and cutting-edge phones announce your name. The phone company doesn’t know or care what your individual phone does; it treats
your telephone polymorphically.
Creating Polymorphic Types
Because a ListBox is a Windowand aButtonis a Window, you expect to be able to use either of these types in situations that call for a Window. For
example, a form might want to keep a collection of all the derived instances of Window it manages (buttons, lists, and so on), so that when the form
is opened, it can tell each of its Windows to draw itself. For this operation, the form does not want to know which elements areListBoxes and which
areButtons; it just wants to tick through its collection and tell each one to “draw.” In short, the form wants to treat all its Window objects
polymorphically.
You implement polymorphism in two steps:
1. Create a base class with virtual methods.
2. Create derived classes that override the behavior of the base class’s virtual methods.
To create a method in a base class that supports polymorphism, mark the method asvirtual. For example, to indicate that the
methodDrawWindow()of class Window in Example 11-1 is polymorphic, add the keywordvirtualto its declaration, as follows:
public virtual void DrawWindow()
Each derived class is free to inherit and use the base class’sDrawWindow()method as is or to implement its own version ofDrawWindow(). If a derived
class does override theDrawWindow()method, that overridden version will be invoked for each instance of the derived class. You override the base
class virtual method by using the keywordoverride in the derived class method definition, and then add the modified code for that overridden
method.
OOps Interview
ok
What Is Threading?
Every process has at least one thread of control and every process can perform at least one task at a time. A process that has more than one
thread of control defines a multi-threaded process. A multi-threaded process allows multiple tasks to run asynchronously from within the
environment of the process.
Static methods can not use non static members. True or False.
True
Why can't you specify the accessibility modifier for methods inside the interface?
you are not allowed to specify any accessibility, it's public by default.
Can you allow class to be inherited, but prevent the method from being over-ridden?
Yes, just leave the class public and make the method sealed.
Can you prevent your class from being inherited and becoming a base class for some other classes?
Yes, that's what keyword sealed in the class definition is for. The developer trying to derive from your class will get a message: cannot
inherit from Sealed class WhateverBaseClassName. It's the same concept as final class in Java.
Can you declare the override method static while the original method is non-static?
No, you can't, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword
override.
If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be
used primarily for objects that are closely related, whereas 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 large functional units, use an abstract class.
If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract
classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
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.
What can you do to make class available for inheritance but you need to prevent it's method to come in inheritance chain?
Well, Declare a class with public access specifier and mark all it's method to sealed . As anything which is declared with sealed keyword
cannot be inherited.
- Obsolete
- DllImport
- Conditional
- WebMethod
A class has default modifiers as Internal . It can declare members (methods etc) with following access modifiers:
public
internal
private
protected internal
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 Polymorphism?
In OPP’S, polymorphism(Greek meaning “having multiple forms”) is the ablity of being able to assign a different meaning or usage to
something in different contexts - specifically, to allow an entity such as a a function, 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 their own definition and implementation.At run-time, when user’s code calls
the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code
when a method of the base class is called it executes the overriden method.
Reason: The message says it all. Classes can only be declared as private, protected or protected internal when declared as nested classes,
other than that, it doesn't make sense to declare a class with a visibility that 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.
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.
Example:
Class A
{
// some code
Private Void A()
{
//Private Constructor
}
}
Class B:A
{
//code
}
Because Class A constructor declared as private hence its accessibility limit is to that class only, Class B can't access. When we create an
object for Class B that constructor will call constructor A but class B have no rights to access the Class A constructor hence we will get
compilation error.
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();
//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()
{
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 in Form_Load event.
As seen in above code, I have declared 2 classes. One works as a Base class and second is a derieve class derived from base class.
If we create object like above notation and make a call to any function which exists in base class and derive class both, then it will always
make a call to function of base class. If we have overidden the 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 the class.)
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();
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
What is Overriding?
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 .
You can't have a overload method with same number parameters but different return type. In order to create overload method, the return
type must be the same and parameter type must be different or different in numbers.
What is Protected Internal access modifier in C#?
Protected Internal is a access modifiers for the members (methods or functions) ie. you can't declare a class as protected internal explicitly.
The members access is limited to the current assembly or types derived from the containing class.
Protected Internal means the method is accessible by anything that can access the protected method UNION with anything that can access
the internal method.
Abstract classes cannot be used to instantiate objects; because abstract classes are incomplete, it may contain only definition of the
properties or methods and derived classes that inherit this implements it's properties or methods.
Static, Value Types & interface doesn't support abstract modifiers. Static members cannot be abstract. Classes with abstract member must
also be abstract.
Can we specify the access modifier for explicitly implemented interface method?
No, we can't specify the access modifier for the explicitly implemented interface method. By default its scope will be internal.
What is Protected access modifier in C#?
The protected keyword is a member access modifier. It can only be used in a declaring a function or method 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 class derived 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 through the derived class type.
The internal keyword is an access modifier for types and type members ie. we can declare a class as internal or its member as internal.
Internal members are accessible only within files in the same assembly (.dll). In other words, access is limited exclusively to classes
defined within the current project assembly.
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. Private access 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.
The public keyword is an access modifier for types and type members ie. we can declare a class or its member (functions or methods) as
Public. There are no restrictions on accessing public members.
A pure virtual function is a function that must be overridden in a derived class and need not be defined. 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 pure
virtual void f3() = 0; // pure virtual
};
What is an Interface?
An interface is a contract & defines the requisite behavior of generalization of types.
An interface mandates a set of behavior, but not the implementation. Interface must be inherited. We can't create an instance of an
interface.
An interface is an array of related function that must be implemented in derived type. Members of an interface are implicitly public &
abstract.
It provides a convenient way to reuse existing fully tested code in different context thereby saving lot of coding.
Inheritance of classes in C# is always implementation Inheritance.
What is Class?
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?
Object is anything that is identifiable as a single material item.
What is Polymorphisms?
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.