Introduction To Object Oriented Programming Concepts (OOPS) In C#.Net
23:42
C# OOPS CONCEPTS:
CLASS: Class is used to create user defined data
types, Class contains variables and methods,
variables are used to store values and methods are used to manipulate the
values(accepting values from user, doing calculations, displaying result etc)
It is the blueprint/ plan/ template that describe the details of an object. A class is
the blueprint from which the individual objects are created.
Creating a Class:
Class Employee //Creating Class..
{
Public int eno;
//Declaring Variables..
Public string ename;
Public void
GetEmpDetails() //Creating method..
{
//some code….
}
}
OBJECT: Object is instance of a class
An object can be
considered a "thing"
that can perform a set of related activities. The set of activities that the
object performs defines the object's behavior.
Object Creation:
Classname obj=new
Classname();
Here obj =>
Creating variable/instance of class.
New => Operator.
Classname() => Constructor.
Encapsulation: Encapsulation is a process of Binding
Variables and methods as a single unit. If a class is created then it is
treated as an Encapsulation.
Inheritance:
Inheritance is a
process of deriving a new class from already existing class. It is used for
reusing the existing code.
Syntax:
Class ChildClassName :
BaseClassName
{
//Body..
}
Types of Inheritance:
1. Single Inheritance: When a single derived class is created
from a single base class is called single inheritance.
Example:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
singleinheritanceexp
{
class Program
{
public void Display()
{
Console.WriteLine("Display method from base class");
}
class ChildAbc : Program
//ChildAbc is child of Program
{
public void Display1()
{
Console.WriteLine("Display1 method from child class");
}
}
static void Main(string[]
args)
{
ChildAbc objChildAbc = new ChildAbc(); //Creating object of child class
objChildAbc.Display1();
objChildAbc.Display();
//calling base class method
Console.Read();
}
}
}
OutPut:
Display1 method from child class
Display method from base class
2. Hierarchical
Inheritance: When more than one derived
classes are created from a single base class is called Hierarchical
Inheritance.
Example:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
HierarchicalInheritanceexp
{
class Program //Base Class..
{
public void
Display()
{
Console.WriteLine("Display Method from base class");
}
class ChildAbc
: Program //ChildAbc
is child class of Program..
{
public void
Display1()
{
Console.WriteLine("Display1 method from childabc class");
}
}
class ChildDef
: Program //ChildDef
is child class of Program..
{
public void
Display2()
{
Console.WriteLine("Display2 method from childdef class");
}
}
static void
Main(string[] args)
{
ChildAbc objChildAbc = new ChildAbc(); //Creating Object for childclass ChildAbc..
objChildAbc.Display1();
objChildAbc.Display(); // Calling base class
method..
ChildDef objChildDef = new ChildDef(); //Creating Object for childclass ChildDef..
objChildDef.Display2();
objChildDef.Display(); //calling base class
method..
Console.Read();
}
}
}
OutPut:
Display1 method from childabc class
Display Method from base class
Display2 method from childdef class
Display Method from base class
3.Multilevel
Inheritance: When a derived class is
created from a derived class is called as Multilevel Inheritance.
Example:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
multilevelinheritanceexp
{
class Program //base class..
{
public void
Display()
{
Console.WriteLine("Display method from base class");
}
class ChildAbc
: Program //child
class
{
public void
Display1()
{
Console.WriteLine("Display1 method from first childclass");
}
}
class SubChildAbc
: ChildAbc //derived
from child class ChildAbc
{
public void
Display2()
{
Console.WriteLine("Display2 Method from subchild class");
}
}
static void
Main(string[] args)
{
SubChildAbc objSubChildAbc = new SubChildAbc();
//creating object for sub child class
objSubChildAbc.Display2();
objSubChildAbc.Display1();
objSubChildAbc.Display();
Console.Read();
}
}
}
OutPut:
Display2 Method from
subchild class
Display1 method from first
childclass
Display method from base
class
4.Hybrid Inheritance: Combination of Single, Hierarchical and
Multilevel Inheritances is called Hybrid Inheritance.
Example:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
hybridinheritanceexp
{
class Program
{
public void
Display()
{
Console.WriteLine("Display method from base class");
}
class ChildAbc
: Program
{
public void
Display1()
{
Console.WriteLine("Display1 method from child class");
}
}
class SubChildDef
: ChildAbc //derived
from child class ChildAbc..
{
public void
Display2()
{
Console.WriteLine("Display2 method from subchild class1");
}
}
class SubChildGhi
: ChildAbc //derived
from child class ChildAbc..
{
public void
Display3()
{
Console.WriteLine("Display3 method from subchild class2");
}
}
static void
Main(string[] args)
{
SubChildDef objSubChildDef = new SubChildDef();
//cerating object for 1st subchildclass..
objSubChildDef.Display2();
objSubChildDef.Display1();
objSubChildDef.Display();
SubChildGhi objSubChildGhi = new SubChildGhi();
//cerating object for 2nd subchildclass..
objSubChildGhi.Display3();
objSubChildGhi.Display1();
objSubChildGhi.Display();
Console.Read();
}
}
}
OutPut:
Display2 method from subchild class1
Display1 method from child class
Display method from base class
Display3 method from subchild class2
Display1 method from child class
Display method from base class
5.Multiple Inheritance: When a derived class is created from
multiple base classes is called Multiple Inheritance. But Multiple Inheritance
is not supported by .net classes and can be done using Interfaces.
Polymorphism:
One name multiple
forms are called Polymorphism. These
are two types
1. Compile Time
Polymorphism or Early Binding.
2. Run Time Polymorphism
or Late Binding.
1.Compile Time Polymorphism: Compile Time Polymorphism Is Method and
Operator Overloading.
Method Overloading:
Multiple methods having same
name and different signature is called method overloading. In this Compiler
will take decision at compile time which method will execute based on passed arguments.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace methodoverloadingexp
{
class Program
{
public int Sum(int a, int b)
{
return a + b;
}
public int Sum(int a, int b, int c)
{
return a + b + c;
}
class Abc : Program
{
public int Sum(int a, int b, int c, int d)
{
return a + b + c + d;
}
}
static void Main(string[] args)
{
Abc objAbc = new Abc();
Console.WriteLine(objAbc.Sum(25,50,13,24));
Console.WriteLine(objAbc.Sum(25, 50, 13));
Console.WriteLine(objAbc.Sum(25, 50));
Console.Read();
}
}
}
OutPut:
112
88
75
2.Run Time Polymorphism: Run Time Polymorphism Is done by using
Inheritance and Virtual functions.
Method Overriding:
It is possible only with
inheritance, the methods in different classes having same signature then
methods in one class will overrides the method in another class that is derived
class method will override the base class method, same signature means method names
number of arguments , order of arguments, data types will be same, method
overriding is called runtime polymorphism. Based on the method address compiler
will take decision at run time that which method in the class should execute.
Example:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace methodoverridingexp
{
class Program
{
public virtual void
Manipulation(int a, int
b) //creating virtual method..
{
Console.WriteLine(a + b);
}
class Abc : Program
{
public override void
Manipulation(int a, int
b) //creating override method..
{
base.Manipulation(20,
30); //calling base class method..
Console.WriteLine(a - b);
}
}
static void Main(string[]
args)
{
Abc
objAbc = new Abc();
//creating object for derived class..
objAbc.Manipulation(30,
20); //calling override method..
Console.Read();
}
}
}
OutPut:
50
10
Abstraction:
Abstraction is a
process of hiding the implementation but providing the services.
Abstraction is
possible with inheritance and Abstract contains abstract and non abstract
methods if it contains at least one abstract method then only it is treated as
abstract class. And an Abstraction method contains only method declaration,
method implementation in the derived class.
How to Abstract: By using Access Specifiers.
In C# Five Access
Specifies are there these are
1. Public: Accessible outside the class through
object reference.
2. Private: Accessible inside the class only through
member functions. Even in main function also it is not accessible.
3. Protected: It is just like private but Accessible
in derived classes also through member functions.
4. Internal: Accessible with in the Assembly through
objects.
5. Protected Internal: It is the combination of Protected and
internal, Visible inside the assembly and in derived classes outside the
assembly through member functions.
Example:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace abstractexp
{
abstract class Program
{
//Abstract Method..
public abstract void
Display();
//Non Abstract
Method..
public void Display1()
{
Console.WriteLine("Non Abstract Display1 Method");
}
class Abc : Program
{
public
override void
Display() //implementing the Abstract method
{
Console.WriteLine("Abstract Display Method in Abc Class");
}
}
class Def : Program
{
public
override void
Display() //implementing the Abstract method
{
Console.WriteLine("Abstract Display Method in Def Class");
}
}
static
void Main(string[]
args)
{
Abc
objAbc = new Abc();
//creating object for 1st derived class Abc..
objAbc.Display1();
objAbc.Display(); //calling abstarct method..
Def
objDef = new Def();
//creating object for 2nd derived class Def..
objDef.Display1();
objDef.Display(); //calling abstract method..
Console.Read();
}
}
}
OutPut:
Non Abstract Display1
Method
Abstract Display
Method in Abc Class
Non Abstract Display1
Method
Abstract Display
Method in Def Class
Interface : Interface is like a class but with interface we can’t create object,
Interface can contains only Abstract members, Interface members can be Events,
Methods, Properties and Indexers and Interface can contains only declaration
for its members and implementation in the derived class and Interface members
are public abstract by default, With Interfaces Multiple Inheritance is
possible.
Simple Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
simpleinterfaceexp
{
class Program
{
interface IAbc
{
void Sum(int
a, int b);
}
class Abcd : IAbc
{
void IAbc.Sum(int a, int b)
{
Console.WriteLine(a + b);
}
}
static void Main(string[] args)
{
IAbc obj; //Declaration..
obj = new Abcd();//Creating Object..
obj.Sum(20, 30);
Console.Read();
}
}
}
OutPut:
50
Example: Two
Interfaces in one class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
twointerfacesoneclas
{
class Program
{
interface IClient1
//First Interface..
{
void GetEmpDetails(int eno, string
ename);
}
interface IClient2
//Second Interface..
{
void GetEmpDetails(int dno, string
dname);
}
class Client : IClient1, IClient2
//Using Two Interfaces in One class..
{
void IClient1.GetEmpDetails(int eno, string ename)
{
Console.WriteLine(eno + " " + ename);
}
void IClient2.GetEmpDetails(int dno, string
dname)
{
Console.WriteLine(dno + " " + dname);
}
}
static void Main(string[] args)
{
IClient1 objIClient1 = new Client();
objIClient1.GetEmpDetails(1, "Venu");
IClient2 objIClient2 = new Client();
objIClient2.GetEmpDetails(10, "Developer");
Console.Read();
}
}
}
OutPut:
1 Venu
10 Developer
Example: One
Interface In another Interface..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
oneinterfaceinanotherinterface
{
class Program
{
interface IClient1
{
void GetEmpDetails(int eno, string
ename);
}
interface IClient2:IClient1
{
void GetDeptDetails(int dno, string
dname);
}
class Client : IClient2
{
void IClient1.GetEmpDetails(int eno, string
ename)
{
Console.WriteLine(eno + " " + ename);
}
void IClient2.GetDeptDetails(int dno, string
dname)
{
Console.WriteLine(dno + " " + dname);
}
}
static void Main(string[] args)
{
IClient2 objIClient2 = new Client();
objIClient2.GetEmpDetails(1, "Venu");
objIClient2.GetDeptDetails(10, "Development");
Console.Read();
}
}
}
OutPut:
1 Venu
10 Development
Example: One
Interface In Two classes..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
oneinterfaceintwoclasses
{
class Program
{
interface IManipulation
//Creating Interface..
{
void Display(int
a, int b);
}
class MyClass1 : IManipulation //Using
Interface in First Class..
{
void IManipulation.Display(int a, int b)
{
Console.WriteLine("Sum Is:" + (a + b));
}
}
class MyClass2 : IManipulation //Using
Interface In second class..
{
void IManipulation.Display(int a, int b)
{
Console.WriteLine("Sub Is:" + (a - b));
}
}
static void Main(string[] args)
{
IManipulation objIManipulation; //Declaration..
objIManipulation = new MyClass1(); //creating
object for 1st class..
objIManipulation.Display(30, 20);
objIManipulation = new MyClass2(); //creating
object for second class..
objIManipulation.Display(30,20);
Console.Read();
}
}
}
OutPut:
Sum Is:50
Sub Is:10
0 comments :