OOPS - Object-Oriented Programming System
OOPS - Object-Oriented Programming System
What’s a Class?
A class is a template definition of the methods and variables for a particular kind of object. In
other words, class is the blue print from which an individual objects is created.
What’s an Object?
It’s a basic unit of a system. An object is an entity that has attributes, behavior, and identity.
Objects are members of a class. Attributes and behavior of an object are defined by the class
definition.
1. Private:
In OOP Attributes, Methods, Properties, Constructors declared with “private” access modifier
get access or visible only to members of current class.
If we declared the variable as private in current class, we cannot access that variable in
another class due to the protection level of “private” access modifier.
2. Protected:
In OOP Attributes, Methods, Properties, Constructors declared with “protected” access
modifier get access or visible only to members of current class as well as to members of
derived class.
This means we can access Attributes, Methods, Properties, Constructors declared with
“protected” access modifier in derived class.
3. Internal:
In OOP Attributes, Methods, Properties, Constructors declared with “internal” access
modifier get access or visible only to members of current project or in current namespace.
This means we can access Attributes, Methods, Properties, and Constructors declared with
“internal” access or visible only to members of current project or in current namespace.
4. Protected Internal:
In OOP Attributes, Methods, Properties, Constructors declared with “protected internal”
access modifier get access or visible only to members of current project or in current
namespace and also to members of derived class.
5. Public:
In OOP Attributes, Methods, Properties, Constructors declared with “public” access modifier
get access or visible to all members of classes and projects.
This means “public” access modifier is access or visible to all members of classes and
projects.
What is Abstraction?
1. Abstraction is the representation of only the essential features of an object and hiding
unessential features of an object.
2. Through Abstraction all relevant data can be hide in order to reduce complexity and
increase efficiency.
3. It shows required data and hides unwanted data.
What is Encapsulation?
1. Encapsulation is a process of hiding all internal details of an object from the outside
world.
2. Encapsulation gives us maintainability, flexibility and extensibility to our code.
Types of Polymorphism:
1. Compile time Polymorphism
2. Run time Polymorphism
What is an Interface?
1. Interface is a contract that defines the signature of the functionality.
2. Single Class can implement multiple interfaces. If a class implements an interface then it has
to provide implementation to all its methods.
What is Inheritance?
Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is
defined in other classes The Class whose methods and variables are defined is called super class
or base class. The Class that inherits methods and variables are defined is called sub class or
derived class Sometimes base class known as Generalized Class and derived class known as
Specialized Class keyword to declare inheritance is “:” (colon) in Visual C#.
We have two classes “Base Class” and “Derived Class”. We have created constructor and
Destructor of both Base and Derived classes. Which Constructor and Destructor will get
called first?
1. If we create Derived class object then, Base class constructor -> Derived Class
Constructor -> Derived Class Destructor -> Base Class Destructor will get called
respectively in above order.
2. If we create Base class object then, Base Class Constructor -> Base Class Destructor will
get called respectively
What is a delegate?
Delegate is a class that can hold a reference to a method or a function. Delegate class has a
signature and it can only reference those methods whose signature is compliant with the class.
Delegates are type-safe functions pointers or callbacks.
Delegate object can then be passed to code which can call the referenced method, without having
to know at compile time which method will be invoked.
Types of Delegates:
1. Single Cast Delegate: It represents one function.
2. Multi Cast Delegate: It represents more than one function.
What is shadowing?
When two elements in a program have same name, one of them can hide and shadow the other
one. So in such cases the element which shadowed the main element is referenced.
In C# by using the keyword “new” a programmer can hide inherited method / variable.
Below is a sample code, there are two classes “ShadowParent” and “ShadowChild”.
1. In “ShadowParent” there is a variable “iInt” which is an integer.
2. In “ShadowChild” overrides “ShadowParent” and shadows the “iInt” variable to a
string.
What’s difference between Shadowing and Overriding?
Following are the differences between shadowing and overriding:-
1. Overriding redefines only the implementation while shadowing redefines the whole
element.
2. In overriding derived classes can refer the parent class element by using “ME” keyword,
but in shadowing you can access it by “MYBASE”.
Compiler makes sure that no instance of static class is created. In previous version of C#, the
constructor has to be marked private to avoid this from happening.
What is the use of “Overrides” and “Overridable” keywords?
Overridable is used in parent class to indicate that a method can be overridden. Overrides are
used in the child class to indicate that you are overriding a method.
1 Array comes in the System namespace Array List comes in the System.Collections
namespace
2 The capacity of array is fixed Array List size can increase and decrease
dynamically
3 An Array is a collection of similar items Array List can hold item of different types
4 An Array can have multiple dimensions Array List always has exactly one dimension
What is HashTable?
1. HashTable object contains items in key/value pairs.
2. The keys are used as indexes.
3. We can search value by using their corresponding key.
4. Items are added to the HashTable with the “Add ()” method.
5. The data type of HashTable is object and the default size of a HashTable is 16.
What is ENUM?
ENUM is used to define constants.
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.
Static Data Members should be initialized inside the constructor. True or False.
False. Static Data Members should not be initialized inside the constructor.
Why can't you specify the accessibility modifier for methods inside the interface?
We are not allowed to specify any accessibility. Since 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.
A Class has default modifiers as Internal. It can declare members (methods etc) with following
access modifiers:
1. Public
2. Internal
3. Private
4. Protected internal
A Structure has default modifier as Internal and it can declare its members (methods etc) with
following access modifiers:
1. Public
2. Internal
3. Private
A methods, fields, and properties has default access modifier as "Private" if no modifier is
specified.
In below sample code if we create a object of Class2 which constructor will fire first?
Public Class Class1
Sub New ()
End Sub
End Class
Public Class Class2 Inherits Class1
Sub New ()
End Sub
End Class
In above case, the Constructor of base Class (i.e. Class1) will get fire first and then Derived class
Constructor Class2 will get fire.
If you declare a Constructor as private then it doesn’t allow creating object for its derived class,
i.e. you lose inherent facility for that class.
Example:
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.
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 will call the derive class function.
For example:
Note:
This will throw a compile time error. (Casting is required.)
Method overloading allows us to write different version of the same method in a class or derived
class. Compiler automatically selects the most appropriate method based on the parameter
supplied.
You can't have an 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.
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:
What is a sealed modifier?
1. Sealed types cannot be inherited & are concrete.
2. Sealed modifiers can also be applied to instance methods, properties, events & indexes. It
can't be applied to static members.
3. Sealed members are allowed in sealed and non-sealed classes.
What is Inheritance?
1. It provides a convenient way to reuse existing fully code in different context thereby saving
lot of coding.
2. Inheritance of classes in C# is always Implementation Inheritance.
What is a property?
Property - A property is a thing that describes the features of an object. A property is a piece of
data contained within a class that has an exposed interface for reading/writing. The main
difference between a field and a property is in the inclusion of an interface. Properties provide
the opportunity to protect a field in a class by reading and writing to it through the property.
We make use of Get and Set keywords while working with properties. We prefix the variables
used within this code block with an underscore. Value is a keyword, which holds the value which
is being retrieved or set to property.
Operator Overloading: It is one of the features of Object Oriented Programming which gives
an extra ability to an operator to act on a User-defined operand (Objects).
Different Operators:
1. All C# binary operators can be over loaded. i.e. +, - , *, / , %,&,|, <<,>>.
2. All C# unary operators can be over loaded. i.e. +,_,!,++,-- .
3. All relational operators can be over loaded, but only as pairs. i.e. = =, !=, <>, <=, >=
Static Polymorphism:
In Static Polymorphism the method called is decided at compile-time only. Method Overloading
is an example of Static Polymorphism.
Method overloading is a concept where we use the same method name many times in the same
class, but with different parameters.
Depending on the parameters we pass, it is decided at compile-time only, which method is too
called.
The same method name with the same parameters is an error and it is a case of duplication
of methods which C# does not permits. In Static Polymorphism decision is taken at compile
time. Static Polymorphism is also as Method Overloading / Compile Time Polymorphism / Early
Binding.
Dynamic Polymorphism:
In Dynamic Polymorphism a call to an overridden function is resolved during runtime. Dynamic
Polymorphism is achieved using inheritance and overriding. Dynamic Polymorphism is also
known as Method Overriding / Late Binding / Runtime Polymorphism.
What is the use of abstract classes? How will we decide in a scenario, whether we have to
use abstract class or interface?
We use Abstract class and interface to enforce some rules to the classes which
extends/implements. For example we can define a class say Bird and we can declare methods say
Fly() Walk() . This means whatever the class that is derived from Bird has to override or give
definition for Fly() and Walk() and therefore we are making sure that all the derived classes
follows or has the common functionalities. In other way the classes derived from super class
should have common properties. In addition to this the derive class can have its own methods
like Swim().
In case of Abstract class we can define COMMON functionalities in super class and those can be
used in the derived class where as in Interface we can’t do that. (this I would say as advantage of
abstract class)
In case of Interface the derived class can implement any number of interface but restricted to
extend only one abstract class (this i would say as advantage of Interface)
Constant variable value is been assigned at the Read only variable value has been assigned at
compile time only. the runtime only.
Constant variable value cannot be changed Read only variables values can be changed
after its initialization. inside the constructors.
Constant variables value can be assigned to the Read only variables value can’t be assigned to
read only variable. the constant variables.
http://www.dotnetfunda.com/interview/exclusive/x2944-what-are-the-advantages-of-
generics-.aspx