| Object Oriented Concepts in C#
|
Showing posts with label OOPs. Show all posts
Showing posts with label OOPs. Show all posts
Sunday, October 20, 2013
Wednesday, June 5, 2013
Runtime Polymorphism
In C# Dynamic Polymorphism or Runtime Polymorphism is achieved using the concept of inheritance; the methods defined in the base class can be overridden in the derived class and can be called dynamically at runtime.
The following example explains Dynamic or Runtime Polymorphism.
The following example explains Dynamic or Runtime Polymorphism.
Dynamic Polymorphism
In C# Dynamic Polymorphism
or Runtime Polymorphism is achieved using the concept of inheritance; the
methods defined in the base class can be overridden in the derived class and
can be called dynamically at runtime.
The following example explains Dynamic or Runtime Polymorphism.
The following example explains Dynamic or Runtime Polymorphism.
Compile time Polymorphism
In C# Static Polymorphism
or Compile time Polymorphism is
achieved through function/method overloading.
Function overloading refers to having many functions
with the same name but with different signatures i.e the names of the functions
will be same but will vary in any one of the following.
1. Number of parameters
2. Type of parameters
3. Return Type.
The following example explains Static or compile time Polymorphism.
1. Number of parameters
2. Type of parameters
3. Return Type.
The following example explains Static or compile time Polymorphism.
Static Polymorphism
In C# Static Polymorphism
or Compile time polymorphism is achieved through function/method overloading.
Function overloading refers to having many functions
with the same name but with different signatures i.e the names of the functions
will be same but will vary in any one of the following.
1. Number of parameters
2. Type of parameters
3. Return Type.
The following example explains Static or compile time Polymorphism.
1. Number of parameters
2. Type of parameters
3. Return Type.
The following example explains Static or compile time Polymorphism.
Polymorphism
Polymorphism is one of the key concepts
of object oriented programming; polymorphism enables us to have different
methods with the same name. The appropriate method to be called will be decided
at compile/run time.
There are 2 types of Polymorphism.
Static / Compile time Polymorphism
Dynamic / Runtime Polymorphism.
There are 2 types of Polymorphism.
Static / Compile time Polymorphism
Dynamic / Runtime Polymorphism.
Tuesday, June 4, 2013
Inheritance
Inheritance
is one of the key concepts of object oriented programming, Inheritance helps us
to reuse/extend the methods and properties of another class. The class which
extends these members is called derived class and the original class is called
the base class. Apart from reuse/extend inheritance helps in standardizing the
basic features of the classes.
For example let us consider we want to create objects for Car, Bus, Jeep etc. Here all these are vehicles and have some property in common, hence we can create a base class say Vehicle and add the basic properties like Make, Model, Color etc to the base class, this way we make sure that any class which inherits the base class will have these properties.
For example let us consider we want to create objects for Car, Bus, Jeep etc. Here all these are vehicles and have some property in common, hence we can create a base class say Vehicle and add the basic properties like Make, Model, Color etc to the base class, this way we make sure that any class which inherits the base class will have these properties.
In C# inheritance is defined using the colon (:) symbol, with the
following syntax.
class : Derived Class Name : Base Class Name
class
Abstraction
Abstraction in OOPs means,
hiding or abstracting something. Abstraction helps in hiding requirements which
are not required for the actual implementation; this might sound similar to
encapsulation. Yes Encapsulation and Abstraction concepts complement each
other.
Abstraction tends to hide the full complexity of the system form the external world and show only the details which are required to use the features exposed by the object. The actual functionality might depend on many other parameters which are not required to be exposed to the outside world; abstraction hides these and shows only those details which are required to use the system.
The following class explains the concepts of Abstraction.
Abstraction tends to hide the full complexity of the system form the external world and show only the details which are required to use the features exposed by the object. The actual functionality might depend on many other parameters which are not required to be exposed to the outside world; abstraction hides these and shows only those details which are required to use the system.
The following class explains the concepts of Abstraction.
Encapsulation
Encapsulation is a combination
of 2 concepts.
The first one is encapsulating a set of related methods properties into a single unit so that it can be exposed as a single instance to the external world. In C# this is implemented using the concept of classes. A class allows us to group a set of related variables and methods into a single unit and they can be accessed by creating a single instance of the class.
The second one is data hiding, i.e showing only the required details to the external world and hiding the other details which are not required to be exposed. Again this is implemented using classes and a set of access modifiers.
The first one is encapsulating a set of related methods properties into a single unit so that it can be exposed as a single instance to the external world. In C# this is implemented using the concept of classes. A class allows us to group a set of related variables and methods into a single unit and they can be accessed by creating a single instance of the class.
The second one is data hiding, i.e showing only the required details to the external world and hiding the other details which are not required to be exposed. Again this is implemented using classes and a set of access modifiers.
C# OOPs Concepts
Object oriented programming focuses on developing
structured reusable programming modules, most of the modern programming
languages support the OOPs concepts.
Software design patterns are based on the object oriented concepts.
The following are some of the key object oriented concepts.
The following are some of the key object oriented concepts.
Monday, June 3, 2013
IEnumerable Vs IQueryable
IQueryable
The IQueryable interface allows the objects
to be queried based on the LINQ-to-SQL. Objects which implement the IQuerieable
interface can be further queried using LINQ queries and these additional
queries will be executed on the underlying data source.
The below line first queries the list of employees from the datasource, the resultant object empList is further queried based on the department name of the employee, both the filters are done in the underlying datasource and only the final list of employees belonging to the sales department are returned and stored in the memory, using LINQ-to-Sql. Hence more efficient based on IEnumerable based objects.
The IQueryable
The below line first queries the list of employees from the datasource, the resultant object empList is further queried based on the department name of the employee, both the filters are done in the underlying datasource and only the final list of employees belonging to the sales department are returned and stored in the memory, using LINQ-to-Sql. Hence more efficient based on
IQueriable Interface
The
IQueriable interface is used to execute queries against a data source, where
the type of ata returned from the source is not known at design time.
IQueriable interface inherits from the IEnemurable interface, and allows the objects to be queried based on the underlying data source, hence they are more efficient when the types are to be queried/filtered further.
Namespace: System.Linq
IQueriable interface inherits from the IEnemurable interface, and allows the objects to be queried based on the underlying data source, hence they are more efficient when the types are to be queried/filtered further.
Namespace: System.Linq
IEnumerable T Interface
The IEnumerable interface exposes the enumerator,
which supports a simple iteration over a collection of a specified type.
Namespace: System.Collections.Generic.
While using IEnumerable
Sunday, June 2, 2013
IEnumerable Interface
The IEnumerable interface exposes the
enumerator, which supports a simple iteration over a non-generic collection.
Namespace: System.Collections
.Net Framework pre-defined Interfaces
The .Net framework itself is built with a number
of interfaces, each of these
interfaces are pre-defined and have a specific purpose. The classes which
implement these interfaces should override the methods in these interfaces,
this way .Net makes sure that all the classes which implement the interfaces
are consistent. The classes can add their own methods but they should definitely
implement the methods defined in the interface.
The following are some of the commonly used interfaces which are per-defined in the .Net framework.
The following are some of the commonly used interfaces which are per-defined in the .Net framework.
Accessing Methods of Explicit Interfaces
We have
seen on how to implement more than one interface
which have methods/properties with the same name and signature using explicit
interfaces. Now how do we create instance of the class and call these methods
individually.
The following example contains 2 interfaces IEmployee and IDepartment, both the interfaces contain a method GetName() with the same name and signature. The class OfficeClass implements both the interfaces and implements the GetName method from both the interfaces using Explicit Interface Implementation.
The following example contains 2 interfaces IEmployee and IDepartment, both the interfaces contain a method GetName() with the same name and signature. The class OfficeClass implements both the interfaces and implements the GetName method from both the interfaces using Explicit Interface Implementation.
Explicit Interface Implementation
We
know that a class can implement more than one interface, now what if a class implements 2 interfaces and if both
the interfaces contain a method with the same name and signature, how do we
implement these methods in the class body. This is where explicit Interface
comes into play. In such cases the method name should be prefixed with the
interface name and a (.) symbol.
The following example contains 2 interfaces IEmployee and IDepartment, both the interfaces contain a method GetName() with the same name and signature. The class OfficeClass implements both the interfaces and implements the GetName method from both the interfaces using Explicit Interface Implementation.
The following example contains 2 interfaces IEmployee and IDepartment, both the interfaces contain a method GetName() with the same name and signature. The class OfficeClass implements both the interfaces and implements the GetName method from both the interfaces using Explicit Interface Implementation.
Interface Inheritance
Similar
to class inheritance, interfaces
also support inheritance, an
interface can inherit from another interface. Similar to class inheritance,
interface inheritance also makes use of the (:) operator to define inheritance.
The following example defines a parent interface IVehicle, and a child interface IBus, the IBus interface inherits from the IVehicle interface.
The following example defines a parent interface IVehicle, and a child interface IBus, the IBus interface inherits from the IVehicle interface.
What are Interfaces?
An Interface defines a set of methods and
properties which should be implemented in the class which implements the
interface. Interfaces help us in implementing the concept of multiple inheritances
in C#. In C# a class cannot derive from more than one parent class, but can
implement more than one interface, hence they help us in implementing multiple
inheritance.
In the following example the Interface IEmployee defines 2 methods and the class EmployeeClass implements this interface and adds the method definition to both these methods declared in the interface.
In the following example the Interface IEmployee defines 2 methods and the class EmployeeClass implements this interface and adds the method definition to both these methods declared in the interface.
Subscribe to:
Posts (Atom)

