Object Oriented Programming Concept
Object Oriented Programming Concept
OOP is a technique to develop logical modules, such as classes that contain properties, fields and events.
OOP provides many concepts such as inheritance, data binding, polymorphism etc.
Object means a real word entity such as pen,paper, chair, table etc.
Object-Oriented Programming is a methodology or paradigm to design a program using classes and
objects.
It simplifies the software development and maintenance by providing some concepts:
● Object
● Class
● Inheritance
● Polymorphism
● Abstraction
● Encapsulation
Simula is considered as the first object-oriented programming language.
2. What is a class?
A class can be defined as the primary building block of OOP. A class contains data and behavior of an
entity.
A class in C# can contain:
1. data member
2. properties
3. constructor
4. methods
Notes:
1. Class names should start with uppercase letters and be a noun e.g. It should not be a String, Color,
Button, System, Thread etc.
2. The name of the constructor is always same as the class name
3. A class can have any number of data members, properties, constructors and methods
4. Data member defined using a class is called an object reference.
5. A class can have a data member which is an object reference of the same class Like the manager of the
employee is also an employee.
3. What is an Object?
An entity that has state and behavior is known as an object e.g. pen, table, car etc. It can be physical or
logical.
An object has three characteristics:
● state: represents data (value) of an object.
● behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc.
● identity: Object identity is typically implemented via a unique ID. The value of the ID is not
visible to the external user. But,it is used internally by the compiler to identify each object
uniquely.
For Example: Pen is an object. Its name is Parker, color is black etc. known as its state. It is used to write,
so writing is its behavior.
Object is an instance of a class. Class is a template or blueprint from which objects are created. So an
object is the instance(result) of a class. For example, you have a class called Vehicle and a car is the
object of that class.
Class and Object are the basic concepts of Object Oriented Programming which revolve around the
real-life entities. A class is a user-defined blueprint or prototype from which objects are created.
Basically, a class combines the fields and methods(member function which defines actions) into a single
unit. In C#, classes support polymorphism, inheritance and also provide the concept of derived classes
and base classes.
A class can extend only one Abstract class A class can implement several interfaces
The member of an abstract class can be private and An Interface can only have public members
protected
Any class can extend an abstract class Only an Interface can extend another
Interface
Methods in an abstract class can be abstract as well All methods in an Interface should be
as concrete abstract
There can be a constructor for Abstract class Interface does not have constructor
An abstract class can Implement methods Interfaces can not contain body of any of its
method
9. What is a Constructor?
C# constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the
object that is why it is known as constructor.
A constructor is a special method that is used to initialize an object. Every class has a constructor,if we
don’t explicitly declare a constructor for any C# class the compiler builds a default constructor for that
class. A constructor does not have any return type. Constructors are responsible for object initialization
and memory allocation of its class.
There are basically two rules defined for the constructor.
1. Constructor name must be same as its class name
2. Constructor must have no explicit return type
You can read more on constructor here.
15. What is the difference between Method Overloading and Method Overriding?
Method Overloading lets you have 2 methods Method Overriding lets you have 2 methods with
with same name and different signature same name and same signature
Method overloading can be overloaded in the Method overriding is only possible in derived class
same class or in the child class. not within the same class where the method is
declared
Example:
public int addition(int a, int b) //Two int parameters with same method
{ return a + b; }
public int addition(int a, int b,int c) //Three int Parameters with same method
{ return a + b + c; }
public float addition(float a, float b,float c,float d) //four float type Parameters with same method
same as above two method
{ return a + b + c + d; } }
Example:
Public class test { public virtual int addition() { return 10; } } public class Amount:test { public
override int balance() { return 500; } }
2 {
1 return 500;
2
}
1
3
1
4
}
16. What is the difference between Abstraction and Encapsulation ?
Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used to hide the code
and data in a single unit to protect the data from the outside world. Class is the best example of
encapsulation. Abstraction refers to showing only the necessary details to the intended user.
Array Collection
You need to specify the size of an array at The size of a collection can be adjusted dynamically
the time of its declaration.It can not be as per users requirement.It does not have fixed size
resized.
The member of an array should be of the Collection can have elements of different types
same data type
Shadowing:
1.You can shadow a base class member in the derived class by using the keyword New.
2.The method signature, access level and return type of the shadowed member can be completely
different than the base class member.
3.A method or function of the base class is available to the child (derived) class without the use of the
“overriding” keyword.
4.The compiler hides the function or method of the base class. This concept is known as shadowing or
method hiding.
5.In the shadowing or method hiding, the child (derived) class has its own function, the same function is
also available in the base class.
Overriding:
1.Method overriding is an important feature of OOPS that allows us to re-write a base class function or
method with a different definition.
2.Overriding is also known as “Dynamic Polymorphism” because overriding is resolved at runtime.
3.The method signature, access level and return type of the hidden member has to be same as the base
class member
4.In other words both methods (base class method and derived class method) have the same name,
same number and same type of parameter in the same order with the same return type.
5.The overridden base method must be virtual, abstract or override.
You can look at C# implementation on Shadowing and Overriding here.
Class Structure
By default, the members of a Class are private By default, the members of a Structure are public
Variables of a Class can be assigned as null Structure members can not have null values
24. What are the similarities between Class and Structure?
● Access specifiers are identically used in structure and classes to restrict the access of their data
and methods outside their body
● Both can have constructors,methods, properties,fields, constants etc..
● Both can implement Interfaces to use multiple-inheritance in code
● Both can have Delegates and Events
Static Polymorphism:
The mechanism of linking a function with an object during compile time is called early binding. It is also
called static binding. C# provides two techniques to implement static polymorphism. They are:
1.Function overloading
2.Operator overloading
Dynamic Polymorphism:
The mechanism of linking methods to an object during run time is called late binding.It is also known as
run-time polymorphism. Method overriding helps to implement Dynamic Polymorphism.