Professional Documents
Culture Documents
PROGRAMMING
CONCEPTS
BY
MR.R.N.REDDY
OOPS
R.N.REDDY
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. Page |2
R.N.REDDY
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. Page |3
R.N.REDDY
A variable which is declared within the class and outside the method without
using static keyword is called as instance variable or Non static variable
Here Instance is nothing but object.
We have to access instance variables with the help of object.
3) Static variable:-
A variable which is declared inside the class and outside the method by using
static keyword is called static variable.
Static variable can be called as class variable because this static variable we have
to access with the help of class name.
Method /behaviour:
Method is a member of a class which will have some behaviour or
functionality.
Q) When we will go for method?
A) Whenever we want to implement some functionality we will go for method.
Eg: - we want to calculate total sal of the employee then we can go for a method
called “calsal ()”.
Syntax for method:
<Access modifier> <return type> (<type><arg1,<type><arg2>)
{
//functionality
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. Page |4
R.N.REDDY
1) Parameter less method ():- while defining a method if we will not declare
any parameters that method is called as parameter less method.
Q) When we will go for parameter less method?
A) According to the required if we don’t want to pass any value to the function then
particular method we can declare as parameter less method.
Eg1: Public void display ()
{
Console.WriteLine(“welcome to C#.Net”);
Console.WriteLine(“welcome to oops”);
Console.WriteLine(“Welcome to Satya”);
}
Eg2: Public void Help ()
{
// displaying help
}
2) Parameterized method ():- At the time of defining a method, if we have declared
some parameters which can be called as parameterized method.
Note: parameter can be called as arguments.
Q) When we will declare parameterized method?
A) According to the requirement if we want to pass any value to the function then we
have declare particular method as parameterized method.
For ex: addition of two numbers.
Public void (int a, int b)
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. Page |5
R.N.REDDY
Int c=a+b;
Console.WriteLine (“result is:”+c);
}
According to the return types, methods are classified into two types:
1. Void method
2. Non void method.
1. Void method:
A method which is not returning any value, that method return type should
be void, which is called as void method.
Ex: public void display ()
{
Console.WriteLine (“hi”);
}
2. Non void method:
If a method is returning some value, then particular method is a non void
method.
Non void method return type should be the type of value which is returning by
that method.
If the method is returning numerical value then method return type should be
numerical data type
If the method is returning floating value, method return type should be any
one of the floating data type and so on...
Ex:
Public int Add (int a, int b)
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. Page |6
R.N.REDDY
Int c=a+b;
Return c;
}
Add (10, 5);
Mainly methods are classified into two types
1. Instance/non static method.
2. Static method.
1. Instance method:
While defining a method, if we dint use static keyword, that method is called
as instance method (or) non static method.
Instance method, we have to access with the help of object (or) instance.
Ex:
Public void show ()
{
}
2. Static method:
While defining a method, if we have used static keyword that method is called
as static method.
Static method, we have to access with the class name.
Ex:
Public static void show ()
{
}
Memory allocation:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. Page |7
R.N.REDDY
No memory will be allocated for class.
No memory will be allocated for method.
Memory will be allocated for variables.
When the object is created, memory will be allocated for instance variable.
When the object is destroyed, memory will be de allocated for instance
variable.
In dot net, object can be destroyed in two ways:
a) By garbage collector.
b) By programmer.
As a part of method execution, memory will be allocated for local variables and
memory will be destroyed for local variables when the method execution is
completed.
Let us define a class like below:
Public class myclass
{
Int a;
Int b;
Public int add (int x, int y)
{
Int c=a+b;
Return c;
}
}
If we want to access the above instance method called add, we required object of
myclass.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. Page |8
R.N.REDDY
To consume my class instance variables, which are a and b which requires memory,
to allocate memory for a and b we require object for my class.
Purpose of object:
To access instance members of a class and to allocate memory for instance
variables, we require object
Object/instance:
What is an object?
An object is a instance of a class
An object is a physical representation of a class
When we create object, memory will be allocated for instance variables of
a class.
To access instance members (class and methods) of a class, we require
object.
Ex: 1. Human being-class
Rama is object of human being class
3. Animal is a class
Tiger is object of animal class
Syntax to create object:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. Page |9
R.N.REDDY
New is a keyword, it will allocate the memory for instance variables of a
class at the time of creating object.
Syntax to access instance variables:
<Object name>.<instance variable>;
Syntax to access instance methods:
<Object name>.<method name ()>;
Programming in oops
Prgm: write a console program to define a class with two instance methods
a) Void method without parameters
b) Void method with parameters
myclass
Void Display();
Void Add(int a, int b);
namespace voidexample
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 10
R.N.REDDY
public class myclass
{
public void display()
{
Console.WriteLine("welcome to oops");
Console.WriteLine("welcome to Satya");
}
public void Add(int a, int b)
{
int c = a + b;
Console.WriteLine("Addition result is:" + c);
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.display();
mc.Add(10, 20);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 11
R.N.REDDY
Prgm: Implement above program by accepting the first number and second number
from the user
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 12
R.N.REDDY
{
myclass mc = new myclass();
Console.WriteLine("enter first number:");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("enter second number:");
int y = int.Parse(Console.ReadLine());
mc.Add(x, y);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 13
R.N.REDDY
Myclass
namespace NonVoidExample
{
public class myclass
{
// non void with parameters
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
Console.WriteLine("enter first number:");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("enter second number:");
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 14
R.N.REDDY
int y = int.Parse(Console.ReadLine());
int z= mc.Add(x, y);
Console.WriteLine("Addition result is:" + z);
string k = mc.Uppercase();
Console.WriteLine("uppercase string is:" + k);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 15
R.N.REDDY
Prgm: Write a program to define a class to calculate or to perform below two tasks
a) Calculating the square
b) Calculating the cube
Calculations
namespace viodmethod
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 16
R.N.REDDY
public class calcultions
{
public void sqr(int a)
{
int b = a * a;
Console.WriteLine("square is:" + b);
}
public void cube(int a)
{
int b = a * a * a;
Console.WriteLine("cube is " + b);
}
}
class Program
{
static void Main(string[] args)
{
calcultions obj = new calcultions();
obj.sqr(2);
obj.cube(2);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 17
R.N.REDDY
calculations
namespace viodmethod
{
public class calcultions
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 18
R.N.REDDY
{
public int sqr(int a)
{
int b = a * a;
return b;
}
public int cube(int a)
{
int b = a * a * a;
return b;
}
}
class Program
{
static void Main(string[] args)
{
calcultions obj = new calcultions();
Console.WriteLine("enter your number");
int n = int.Parse(Console.ReadLine());
int res= obj.sqr(n);
Console.WriteLine("square is:" + res);
int res1=obj.cube(n);
Console.WriteLine("cube is:" + res1);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 19
R.N.REDDY
Calculations
namespace nonvoidmethodexample
{
public class calculations
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 20
R.N.REDDY
public int Add(int a, int b)
{
int c = a + b;
return c;
}
public int sub(int a, int b)
{
int c = a - b;
return c;
}
public int mul(int a, int b)
{
int c = a * b;
return c;
}
public double div(int a, int b)
{
double c = a / b;
return c;
}
}
class Program
{
static void Main(string[] args)
{
calculations obj = new calculations();
int res1 = obj.Add(10, 5);
Console.WriteLine("Addition result is:" + res1);
res1 = obj.sub(10, 5);
Console.WriteLine("subraction result is:" + res1);
res1 = obj.mul(10, 5);
Console.WriteLine("mul result is:" + res1);
double res2 = obj.div(10, 5);
Console.WriteLine("division result is:" + res2);
Console.ReadLine();
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 21
R.N.REDDY
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 22
R.N.REDDY
{
public class calculations
{
public int Add(int a, int b)
{
int c = a + b;
return c;
}
public int Sub(int a, int b)
{
int c = a - b;
return c;
}
public int mul(int a, int b)
{
int c = a * b;
return c;
}
public double div(int a, int b)
{
double c = a / b;
return c;
}
}
class Program
{
static void Main(string[] args)
{
calculations obj = new calculations();
Console.WriteLine("enter first number");
int n1 = int.Parse(Console.ReadLine());
Console.WriteLine("enter second number");
int n2 = int.Parse(Console.ReadLine());
int res1 = obj.Add(n1, n2);
Console.WriteLine("Addition result is:" + res1);
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 23
R.N.REDDY
res1 = obj.Sub(n1, n2);
Console.WriteLine("sub result is:" + res1);
res1 = obj.mul(n1, n2);
Console.WriteLine("mul result is:" + res1);
double res2 = obj.div(n1, n2);
Console.WriteLine("div result is:" + res2);
Console.ReadLine();
}
}
}
Output:
Prgm: In the above program when we are performing the division if user entered
second number as zero, display a user friendly message to accept second number
other than zero like below
namespace nonvoidmethodexample
{
public class calculations
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 24
R.N.REDDY
public int Add(int a, int b)
{
int c = a + b;
return c;
}
public int Sub(int a, int b)
{
int c = a - b;
return c;
}
public int mul(int a, int b)
{
int c = a * b;
return c;
}
public double div(int a, int b)
{
while (b == 0)
{
Console.WriteLine("pls enter other than zero");
b = int.Parse(Console.ReadLine());
}
double c = a / b;
return c;
}
}
class Program
{
static void Main(string[] args)
{
calculations obj = new calculations();
Console.WriteLine("enter first number");
int n1 = int.Parse(Console.ReadLine());
Console.WriteLine("enter second number");
int n2 = int.Parse(Console.ReadLine());
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 25
R.N.REDDY
int res1 = obj.Add(n1, n2);
Console.WriteLine("Addition result is:" + res1);
res1 = obj.Sub(n1, n2);
Console.WriteLine("sub result is:" + res1);
res1 = obj.mul(n1, n2);
Console.WriteLine("mul result is:" + res1);
double res2 = obj.div(n1, n2);
Console.WriteLine("div result is:" + res2);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 26
R.N.REDDY
Note: In the above program when user enter second number as zero while
performing division it is throwing divide by zero error which is run time error
which is nothing but exception.
In c#.net we can handle the exception (run time) in two ways
1) By using logic
2) By using exception handling mechanism
In the above program we are handling the run time error by using logic.
Prgm: Write a console program to define a class with two methods like below.
Student
namespace VoidMethod
{
public class student
{
public void getstudentinfo(int sid, string sname)
{
Console.WriteLine("student id is:" + sid);
Console.WriteLine("student name is:" + sname);
}
public void getresuls(int m1, int m2, int m3)
{
int totmarks = m1 + m2 + m3;
int avg = totmarks / 3;
if (m1 < 35 || m2 < 35 || m3 < 35)
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 27
R.N.REDDY
Console.WriteLine("result is fail");
}
else if (avg >= 60)
{
Console.WriteLine("first class");
}
else if (avg >= 50)
{
Console.WriteLine("second class");
}
else
{
Console.WriteLine("third class");
}
}
}
class Program
{
static void Main(string[] args)
{
student obj = new student();
Console.WriteLine("enter student id:");
int sid = int.Parse(Console.ReadLine());
Console.WriteLine("enter stuent name:");
string sname = Console.ReadLine();
obj.getstudentinfo(sid, sname);
Console.WriteLine("enter m1 marks:");
int m1 = int.Parse(Console.ReadLine());
Console.WriteLine("enter m2 marks:");
int m2 = int.Parse(Console.ReadLine());
Console.WriteLine("enter m3 marks:");
int m3 = int.Parse(Console.ReadLine());
obj.getresuls(m1, m2, m3);
Console.ReadLine();
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 28
R.N.REDDY
}
}
}
Output:
namespace EmployeeExample
{
public class Employee
{
// instance variables
public int eno;
public string ename;
public int esal;
//instnace method
public void getempinfo()
{
Console.WriteLine("enter emp no:");
eno = int.Parse(Console.ReadLine());
Console.WriteLine("enter emp name:");
ename = Console.ReadLine();
Console.WriteLine("enter salary:");
esal = int.Parse(Console.ReadLine());
}
public void dispalyempinfo()
{
Console.WriteLine("emp no is:" + eno);
Console.WriteLine("emp name is:" + ename);
Console.WriteLine("emp salary is:" + esal);
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 30
R.N.REDDY
class Program
{
static void Main(string[] args)
{
Employee rama = new Employee();
rama.getempinfo();
Employee raju = new Employee();
raju.getempinfo();
rama.dispalyempinfo();
raju.dispalyempinfo();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 31
R.N.REDDY
When we run the above program CLR will starts program execution from main
method
CLR will execute first below statement
Employee rama =new Employee ();
It will create an object for employee class by allocating the memory for employee
class three instance variables like below with default values.
Object
Reference Eno=0
Variable
Ename=null
Object
Eno=121
Ename=rama
Esal=10000
After this then again control will go back to main method an it will execute the
following statement
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 32
R.N.REDDY
i.e.; Employee raju = new Employee (); this statement is creating
another object of employee class i.e. raju with default values of employee class
instance variables like below
Object
Eno=0
Ename=null
Esal=0
After this statement then control will execute the following statement called
raju.getempinfo () here control will invoke the method and that method will accept
empinfo and it will store into raju object like below
Object
Reference
Eno=122
Variable
raju after Ename=raju
executing Esal=2000 the
method control will come back to main
method and it will execute the following statement rama.displayempinfo (); this
method will display rama object info below
Empno is: 121
Empname is: rama
Salary is: 1000
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 33
R.N.REDDY
After this control will come back to main method and it will execute the following
method raju.displayempinfo ().This method will display raju object info like
below
Empno is: 122
Empname is: raju
Emp salary is: 2000
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 34
R.N.REDDY
Constructor name should be same as class name.
Constructor will not return any value due to that reason it will not have any return
type.
Purpose of constructor
To initialize instance variables at the time of creating object we will take the help of
constructors.
Syntax:
<access modifier> <class name ()>
{
// initialization
}
When we run the above program, program execution start from main method first it
will execute write Line method it will print the given message i.e. “Before object is
creating”. After that control will execute the following statement i.e. myclass mc=new
myclass (), in this statement first control will execute left hand side i.e. myclass mc it
will create a reference variable mc of myclass type like below
Myclass
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 36
R.N.REDDY
Reference variable Mc
After this in this statement right hand side part will execute, first it will execute new
operator, it will allocate memory for myclass instance variables like below is
nothing but forming a object like below
Myclass
Object
a(4 bytes)
b(4 bytes)
After this control will execute the myclass () is nothing but invoking the myclass
constructor.
It will execute the myclass constructor first statement i.e. write line () it will print a
message like below Constructor is calling. After this statement it will execute the
following statements a=10, b=20 these statements will initialize the instance variables
like below
Object
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 37
R.N.REDDY
a=10
b=20
After this initialization constructor execution is completed then control will come
back to the main method and it will execute assignment operator, because of this
operator myclass object address will be assigning into mc reference variable like
below
Myclass
Object
Mc a=10
1010
b=20
1010
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 39
R.N.REDDY
Instance constructor:
While defining a constructor if we didn’t use static keyword which can be called
as instance constructor or non static constructor
The purpose of instance constructor is to initialize only instance variable but
within the instance constructor we can initialize static variables also
Instance constructor will execute automatically at the time of object is creating
Instance constructor are again classified into four types
1) Default constructor or parameter less constructors
2) Parameterized constructor
3) Copy constructor
4) Private constructor
1) Default constructor/parameter less constructor:
Default constructor is nothing but parameter less constructor. It is a zero
parameterized constructor.
It is of two types
a) User defined default constructor.
b) System defined default constructor.
a) User defined default constructor:-a constructor which is defined by the
programmer without using parameters & without static keyword is called as user
defined default constructor.
A class can contain only one default constructor
For example:
namespace DefaultConstructorExample
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 40
R.N.REDDY
public class employee
{
int eno;
string ename;
int esal;
public employee()
{
eno=123;
ename="rama";
esal=1000;
}
public void displayempinfo()
{
Console.WriteLine("emp no is:"+eno);
Console.WriteLine("emp name is:"+ename);
Console.WriteLine("salry is:"+esal);
}
}
class Program
{
static void Main(string[] args)
{
employee obj = new employee();
obj.displayempinfo();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 41
R.N.REDDY
In default constructor for every object we will have some value, to overcome this
i.e. to maintain different values from one object to another object we have to go for
parameterized constructor.
b) System defined default constructor:
If programmer will not define any default constructor within the class like below
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 42
R.N.REDDY
Int esal;
}
Then compiler will generate a default constructor is nothing but system defined
default constructor like below.
class Program
{
static void Main(string[] args)
{
employee obj = new employee();
obj.displayempinfo();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 44
R.N.REDDY
When we compile and run the above application within the employee class there is no
constructor due to that reason C#.Net compiler will define system defined default
constructor and at the time of creating the object that system defined constructor will
initialize default values like below
Employee
Object
obj Eno=0
Ename=null
2010
2010
Due to that Esal=0 reason the output
will be like below
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 45
R.N.REDDY
Emp no is: 0
Emp name is:
Emp sal is: 0
Purpose of default constructor:-
The purpose of the default constructor is to initialize the default values
In real time we will use default constructor to open the connections to data base.
2) Parameterized constructor:-
If we define a constructor with parameters and without static keyword which is
called as parameterized constructor.
A class can contain multiple parameterized constructors but from one
constructor o another constructor we should differentiate like below i.e. it should
satisfy any one of below three options
1) Number of parameters
2) Types of parameters
3) Order of parameters
Q) What is constructor overloading?
A) Implementing multiple constructors within as single class with different signature
(different number of parameters or different types of parameters or different order of
parameters) is called as constructor overloading.
Prgm: Example for parameterized constructor
namespace ParmeterizedConstructorExample
{
public class employee
{
int eno;
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 46
R.N.REDDY
string ename;
int esal;
public employee(int empno, string empname, int empsal)
{
eno = empno;
ename = empname;
esal = empsal;
}
class Program
{
static void Main(string[] args)
{
employee emp1 = new employee(121,"rama",1000);
emp1.displayempinfo();
employee emp2 = new employee(122, "raju", 2000);
emp2.displayempinfo();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 47
R.N.REDDY
Employee
object
Eno=121
Emp1 Ename=rama
2010 Esal=1000
2010
Employee
Object
Emp2 Eno=122
Ename=raju
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 48
Esal=2000
R.N.REDDY
2020
In the above program if we will write the below statement within the main method
it will throw a compile time error because within the employee class we didn’t
define default constructor(Employee emp=new Employee ())
If we write the below statement within the above program main method
Employee emp = new Employee (120, “sita”) it will throw a compile
time error because we didn’t define two parameterized constructor within the
employee class.
3) Copy constructor:
Using this constructor we can copy the values from one object to another object.
Prgm: Example for copy constructor
namespace CopyConstructorExample
{
public class employee
{
int eno;
string ename;
int esal;
public employee()
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 49
R.N.REDDY
{
Console.WriteLine("enter emp no:");
eno = int.Parse(Console.ReadLine());
Console.WriteLine("enter emp no:");
ename=Console.ReadLine();
Console.WriteLine("enter salary:");
esal = int.Parse(Console.ReadLine());
}
public employee(employee obj)
{
eno = obj.eno;
ename = obj.ename;
esal = obj.esal;
}
class Program
{
static void Main(string[] args)
{
employee emp1 = new employee();
employee emp2 = new employee(emp1);
emp1.displayempinfo();
emp2.displayempinfo();
Console.ReadLine();
}
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 50
R.N.REDDY
Output:
Namespace privateconstructorexample
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 51
R.N.REDDY
{
Public class myclass
{
Private Myclass ()
{
}
}
Class program
{
Static void main (string [] args)
{
Myclass mc=new Myclass ();
Console.Readline ();
}
}
}
Output is compile time error because within myclass we have private constructor.
Purpose of private constructor:
Whenever we want to restrict to create object for a class, within that class we have to
define private constructor.
Prgm: Example for class overloading
namespace ClassloadingExample
{
public class employee
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 52
R.N.REDDY
int eno;
string ename;
int esal;
public employee()
{
eno = 121;
ename = "rama";
esal = 1000;
}
public employee(employee obj)
{
eno = obj.eno;
ename = obj.ename;
esal = obj.esal;
}
class Program
{
static void Main(string[] args)
{
employee emp1 = new employee();
emp1.displayempinfo();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 53
R.N.REDDY
In .net when we run the application class loader will load the classes from application
to CLR, according to program order.
In console application program execution starts from main method due to that reason
first it will load the class which is having main method then it will start the execution
of main method.
While executing the main method it will load the remaining classes on statement
execution.
For example: In the above program first class loader will load program class and it
will start the execution from main method.
In main method while executing the first statement Employee obj=new Employee ();
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 54
R.N.REDDY
To execute this statement CLR required employee class due to that reason class
loader will load employee class then it will execute the above statement.
In the same program when we want to execute the last statement i.e.
Console.Readline (); here we required console class due to that reason class loader
will load console class.
Static constructor or Non-static constructor:
While defining a constructor we have used static keyword which can be called as
static constructor.
Memory allocation for static variable:
At the time of class is loading memory will be allocated for static variables and
at the time of unloading the class memory will be deallocated for static
variable.
Static variable and static constructor is not related to object which are related
to only class.
For static variable memory will be allocated out of the object but not within the
object.
Within the object we will have only instance variables.
Static constructor executes at the time of loading the class.
If a class is having one static constructor and one instance constructor then first
control will execute static constructor because class will load first and object
will create.
Static constructor will not contain any parameters which is a parameter less
constructor.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 55
R.N.REDDY
A class can contain maximum one static constructor.
While defining the static constructor we should not have access modifier.
Prgm: Example for static constructor
namespace ClassloadingExample
{
public class employee
{
//instance variables
int eno;
string ename;
int esal;
//static variable
static string CompanyName;
static employee()
{
CompanyName = "Microsoft";
}
public employee(int empno, string empname, int empsal)
{
eno = empno;
ename = empname;
esal = empsal;
}
public void displayempinfo()
{
Console.WriteLine("emp no is:"+eno);
Console.WriteLine("emp name is:"+ename);
Console.WriteLine("salry is:"+esal);
Console.WriteLine("companny name is:" + CompanyName);
}
}
class Program
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 56
R.N.REDDY
static void Main(string[] args)
{
employee emp1 = new employee(121,"rama",1000);
emp1.displayempinfo();
employee emp2=new employee(122,"raju",2000);
emp2.displayempinfo();
Console.ReadLine();
}
}
}
Output:
Employee class
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 57
R.N.REDDY
2010
In the above example we have created two objects emp1 and emp2 memory will be
allocated for instance variables two times, but static variable called company name
memory is allocated only one time if suppose we are declaring company name as
instance variable then for that variable memory will be allocated two times
suppose, if we have 100 employee objects then if we declare company name as a
instance variable then it will allocate memory for company name variable 100
times but for all the employees company name variable value is same.
By implementing static we can save the memory which improves the performance of
the application.
Q) When we will go for static variable or purpose of static variable?
A) According to the requirement whenever the value is common for all the objects
then particular variable we will declare as static.
Ex: 1) with in employee class companyname.
2) With in student class college name.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 58
R.N.REDDY
Prgm: Example for static and instance constructor
namespace staticandinstanceconstructor
{
public class myclass
{
int a;
static int b;
public myclass()
{
a = 10;
Console.WriteLine("instnace constructor is calling");
}
static myclass()
{
b = 10;
Console.WriteLine("static constructor is calling");
}
public void display()
{
Console.WriteLine("a is:" + a);
Console.WriteLine("b is:" + b);
}
}
class Program
{
static void Main(string[] args)
{
myclass obj1 = new myclass();
obj1.display();
myclass obj2 = new myclass();
obj2.display();
Console.ReadLine();
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 59
R.N.REDDY
}
Output:
In the above program when CLR executing main method within the method we are
creating two objects for myclass obj1 and obj2.
But before creating obj1 myclass will load into CLR immediately at the time of
loading myclass memory will be allocated for static variable b and static constructor
will execute.
But at the time of creating object or before second object i.e. obj2 class will not load
once again because already myclass is available within the CLR.
With this we can say static constructor will execute only one at the time of loading
the class but instance constructor will execute for every object creation.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 60
R.N.REDDY
Prgm: Example to define a class with one static constructor and one instance
constructor
namespace staticandinstanceconstructor
{
public class myclass
{
int a;
static int b;
public myclass()
{
a = 10;
}
static myclass()
{
b = 10;
}
public void display()
{
Console.WriteLine("a is:" + a);
a++;
Console.WriteLine("b is:" + b);
b++;
}
}
class Program
{
static void Main(string[] args)
{
myclass obj1 = new myclass();
obj1.display();
myclass obj2 = new myclass();
obj2.display();
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 61
R.N.REDDY
myclass obj3 = new myclass();
obj3.display();
Console.ReadLine();
}
}
}
Fig
Output:
In the above example we have two variables one instance variable i.e. one static
variable b, with in the main method we have created three objects for myclass they
are obj1, obj2, obj3 but static variable b is creating only once at the time of myclass
is loading but in static variable a is creating 3 times i.e. for obj1 one time, for obj2
one time, for obj3 one time.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 62
R.N.REDDY
With this example static variable will create only once irrespective of number of
objects but instance variable will create for every object.
Advantage of static variable:
It will create only once i.e. for that variable memory will be allocated only once,
even though we have created 100 objects with this we save the memory.
Static variable nature is creating once.
Instance variable nature is creating multiple times i.e. for every object.
Prgm: Example to initialize static variable and instance variable within the instance
constructor.
Highlighting or initializing the problem when we initialize static variable within the
instance constructor.
namespace staticandinstanceconstructor
{
public class myclass
{
int a;
static int b;
public myclass()
{
a = 10;
b = 10;
}
public void display()
{
Console.WriteLine("a is:" + a);
a++;
Console.WriteLine("b is:" + b);
b++;
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 63
R.N.REDDY
}
class Program
{
static void Main(string[] args)
{
myclass obj1 = new myclass();
obj1.display();
myclass obj2 = new myclass();
obj2.display();
myclass obj3 = new myclass();
obj3.display();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 64
R.N.REDDY
Static constructor nature is it executes only once when the class is loading because
we have to initialize static variable only once that is the nature of the static variable
and static constructor.
Instance constructor will invoke for every object creation i.e. multiple times because
instance variable should be initialized separately for every object that is the nature
of instance variable and instance constructor.
In the above program we have initialized static variable within the instance and we
have created three objects for myclass i.e. obj1, obj2, obj3 because of these three
objects instance constructor is invoking three times due to that reason static
variable is initializing 3 times.
Actually according to nature of the static variable it should be initialized only once
because of instance constructor it is initializing multiple times with this we are
losing nature of the static variable due to that reason it is not recommended to
initialize static variable within the instance constructor.
Prgm: Example to initialize static variable and instance variable within the static
constructor.
namespace staticconstructorexample
{
Public class myclass
{
Int a;
Static int b;
Static myclass()
{
a = 10;
b = 20;
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 65
R.N.REDDY
}
}
The above code will generate a compile time error because within the static
constructor we cannot initialize instance variables.
Prgm: Example for static constructor with parameters
namespace staticconstructorexample
{
Public class myclass
{
Static int b;
Static myclass (int x)
{
b = x;
}
}
The above code is invalid because a static constructor cannot contain parameters.
Q) When complier will provide system defined default constructor within the
class?
A) Within the class if programmer will not define any constructors then compiler
will generate system defined default constructor.
Only in the below case compiler will generate a system defined default constructor
Ex:
namespace systemdefinedexample
{
public class myclass
{
int a = 10;
int b = 20;
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 66
R.N.REDDY
public void Display()
{
Console.WriteLine("a is:" + a);
Console.WriteLine("b is:" + b);
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.Display();
Console.ReadLine();
}
}
}
Output:
}
}
When we run the above program at the time of creating the object
myclass mc=new myclass () it will create a object and it will invoke the system
defined default constructor will initialize the default values like below
Object
a=0(4 bytes)
Mc
1010 b=0(4 bytes)
After that within the instance variables will be reassigned the given values 10 and
20 like below Object
a=10(4 bytes)
Mc
1010 b=20(4 bytes)
Initialization vs Assignment:-
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 68
R.N.REDDY
namespace Constructorexample
{
public class myclass
{
int a = 10;
int b = 20;
public myclass()
{
Console.WriteLine("a value before assignment:" + a);
Console.WriteLine("b value after assignment:" + b);
a = 10;
b = 20;
}
public void Display()
{
Console.WriteLine("a is:" + a);
Console.WriteLine("b is:" + b);
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.Display();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 69
R.N.REDDY
Note: whenever a constructor is executing first it will initialize the default values
then it will assign the user defined values.
In the above example the given default constructor first initializing the default
values like below then it will assign the user defined values like below
Object
a=0 (4 bytes)
Mc 10
1010 b=0 (4 bytes)
20
Here 0, 0 are default values initialized by the constructor but 10, 20 are user defined
values assigned by the constructor with this we can say that a constructor will do
two duties 1) Initialization
2) Assignment
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 70
R.N.REDDY
Importance of this keyword: - “this” is a keyword which is representing
current class object or instance.
Q) When we will go for this keyword:-
A) Whenever we want to access instance variables within the class with the help
of object, then we can access particular instance member with the help of this
keyword like below
class Program
{ static void Main(string[] args)
{
myclass mc = new myclass(10,20);
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 71
R.N.REDDY
mc.add();
Console.ReadLine();
}
}
}
Whenever local variables name & instance variables name are same then control
will give priority to local variables
In the above example within the constructor local variable names & instance
variable names are same due to that reason which is initializing 10 & 20 to local
variables but end of the program instance variables are having only default values
which are only 0, 0.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 72
R.N.REDDY
To overcome this situation that means to differentiate instance variables from
local variables we have to access instance variables within the help of this
keyword like below
Update the above program like below
Public myclass (int a, int b)
{
this. a=a;
this. b=b;
}
Note:-
When we are using parameterized constructor the parameter name & instance
variable name should be same
Ex: this. Eno=eno;
this. Ename=ename;
Q) When the object class default constructor is calling?
A) Object is the super class for all the .net classes within the object class
Microsoft has defined one default constructor like below
Class object
{
Public object
{
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 73
R.N.REDDY
When a constructor is invoking within a user defined class first it will execute
the object class default constructor then it will execute the current class
constructor
Example
namespace objectclassconstructorexaple
{
public class myclass
{
public myclass()
{
Console.WriteLine("default constructor is calling...........");
}
public myclass(int a)
{
Console.WriteLine("parameterized constructor is calling...........");
}
class Program
{
static void Main(string[] args)
{
myclass mc1 = new myclass();
myclass mc2 = new myclass();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 74
R.N.REDDY
When we compile the above program compiler will add base () keyword to every
constructor like below
Class myclass
{
Public myclass: base ()
{
}
Public myclass (int a): base ()
{
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 75
R.N.REDDY
When we run the above program when control is executing the below statement
Myclass mc1=new myclass ();
Here control will goto myclass default constructor.
When it is executing the default constructor heading because of base() keyword
like below
Public myclass:base()
Here control will goto object class and it will execute the object class default
constructor after that it will come back to myclass default constructor
When the control is executing the below statement
Myclass mc2=new myclass ();
Here control will goto myclass and it will try to execute myclass 1
parameterized constructor heading like below
Public myclass (int a):base()
Here because of base () control will goto object class and it will execute the
object class default constructor after that control will come back to myclass then
it will execute myclass 1 parameterized constructor.
Note:- Base() is a keyword which is representing the instance of super class
Constructor calling & constructor executing:-
Constructor is calling bottom to top but it is executing top to bottom.
Static method:-
While defining a method if we use static keyword which can be called as static
method.
Q) How to access static method?
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 76
R.N.REDDY
A) Syntax:-
Class name. <static method name>
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 77
R.N.REDDY
static void Main(string[] args)
{
employee.displaycompanydetails();
Console.ReadLine();
}
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 78
R.N.REDDY
Due to that reason whenever we are defining a method first we have to ive
priority for static method.
Q) Why main() is a static method?
A) When we run the application CLR has to invoke the main(),if the main() is a
instance methods again it requires object.
Q) To overcome that main() is defined as static method?
A) Within the main() we will not write any logic just it is a entry point for
program execution as well as we will invoke the other class members from
main().
Prgm: Write a program to create an object in 2 lines:-
namespace ConsoleApplication18
{
public class myclass
{
public int a = 10;
}
class Program
{
static void Main(string[] args)
{
myclass mc;
mc = new myclass();
Console.WriteLine("a is :"+mc.a);
Console.ReadLine();
}
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 79
R.N.REDDY
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 80
R.N.REDDY
Mc
NullNull
After executing the 2nd statement
Mc=new myclass();
Then it is creating the object and assigning the object into reference variable like
below
Mc object
Null
A=0
10
1010
With this we can say new keyword is allocating the memory for object
Note:- by default reference variable will have “null” value
Prgm: Example to create 2 reference variables for single object
namespace ConsoleApplication18
{
public class myclass
{
public int a = 10;
}
class Program
{
static void Main(string[] args)
{
myclass obj1 = new myclass(); ;
myclass obj2 = obj1;
Console.WriteLine("obj1 value is:"+obj1.a);
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 81
R.N.REDDY
}
}
}
Output:
Obj1
1010
A=10
Obj2 20
1010
1010
Prgm:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 82
R.N.REDDY
namespace objectexample2
{
public class myclass
{
public int a = 10;
}
class Program
{
static void Main(string[] args)
{
myclass obj1 = new myclass(); ;
myclass obj2 = obj1;
obj1.a = 20;
Console.WriteLine("obj1 value is:"+obj1.a);
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 83
R.N.REDDY
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 84
R.N.REDDY
}
}
}
In the above program first it will create 2 object, When control is executing the
below statement
Obj1=NULL;
Then first object is not printing by any reference variable due to that reason
garbage collector will recognize particular object as unused object and it will
deallocate memory for that object like
Obj1 object
1010 A=10
NULL
1010
Obj2 object
1020 A=10
1020
After that when control is executing the statement like below
Console.writeline(“obj1 value is:”+obj1.a); is throwing NULL reference
exception
Q) When NULL reference exception will be throwing?
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 85
R.N.REDDY
A) Whenever we are trying to access an object with the help of a reference
variable which contains NULL value.
Note:- Execute the above program by making the comment, the statement which
is throwing an exception
Q) How to destroy an object? or how to make an object as unused object?
A) By initializing NULL value to the reference variable
Prgm:
namespace objectexample5
{
public class myclass
{
public int a = 10;
}
class Program
{
static void Main(string[] args)
{
myclass obj1 ;
Console.ReadLine();
}
}
}
namespace ConsoleApplication18
{
public class myclass
{
public int a = 10;
public void PRINT()
{
Console.WriteLine(a);
}
public void show()
{
Print();
}
}
}
Whenever we want to access instance members within
the class like above we don’t require any object we can access directly
B) Accessing instance members from outside the class
namespace ConsoleApplication19
{
public class myclass
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 88
R.N.REDDY
According to above code or above program we can say that whenever we want
to access instance members of a class from outside the class we require object
Accessing the static members:-
class Program
{
static void Main(string[] args)
{
Console.WriteLine(myclass.b);
myclass.print();
Console.ReadLine();
}
}
}
With the above program we can say that whenever we want to access static
members from outside the class we can’t access directly, we have to access with
the help of class name
Q) Can we access instance variable within the static method?
A) Yes, like below
public class myclass
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 90
R.N.REDDY
Within the static method we can access instance members but we require object
within the class.
Access Specifiers/ Access Modifiers:-
Access specifiers are keywords which are specifying accessibility of a class or
class members.
In C#.net we have 5 types of access specifiers
1) Public
2) Private
3) Protected
4) Internal
5) Protected internal
1) Public :-
If we declare a class or class member modifies as public which can be
accessed by all the classes of current project, all the classes of all the projects
within that application.
For public member there is no restriction of access within that application.
Prgm: Example for public
namespace publicexample
{
public class class1
{
public int a=10;
public void method1()
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 91
R.N.REDDY
Console.WriteLine("method1 value is:"+a);
}
}
public class class2
{
public void method2()
{
class1 c1obj = new class1();
Console.WriteLine("method2 value is:" + c1obj.a);
}
}
class Program
{
static void Main(string[] args)
{
class1 c1obj1 = new class1();
Console.WriteLine("main method value is:" + c1obj1.a);
c1obj1.method1();
class2 c2obj2 = new class2();
c2obj2.method2();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 92
R.N.REDDY
2) Private:
If we declared a class or class member access modifier as private which we can
access only within that class.
Prgm: Example for private
namespace privateexmple
{
public class class1
{
private int a = 10;
public void method1()
{
Console.WriteLine("method1 value is:" + a);
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 93
R.N.REDDY
class Program
{
static void Main(string[] args)
{
class1 c1obj1 = new class1();
c1obj1.method1();
Console.ReadLine();
}
}
}
Output:
Note: Among all the access modifiers private will provide more security
3) Protected:
If we declare a class or class member access modifier as a protected this can access
by current class members as well as derived class member.
Prgm: Example for protected
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 94
R.N.REDDY
namespace protectedexample
{
public class class1
{
protected int a = 10;
public void method1()
{
Console.WriteLine("method1 value is:" + a);
}
}
public class class2 : class1
{
public void method2()
{
Console.WriteLine("method2 value is:" + a);
}
}
class Program
{
static void Main(string[] args)
{
class2 c2obj1=new class2();
c2obj1.method1();
c2obj1.method2();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 95
R.N.REDDY
4) Internal:
If we declare a class or class member access modifier as internal this can be
accessed by all the classes of the current project or assembly.
Prgm: Example for internal
namespace internalexample
{
public class myclass1
{
internal int a = 10;
public void method1()
{
Console.WriteLine("method1 value is:" + a);
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 96
R.N.REDDY
}
public class myclass2
{
public void method2()
{
myclass1 c1obj1 = new myclass1();
Console.WriteLine("method2 value is:" + c1obj1.a);
}
}
class Program
{
static void Main(string[] args)
{
myclass1 c1obj1 = new myclass1();
Console.WriteLine("main value is:" + c1obj1.a);
c1obj1.method1();
myclass2 c2obj1 = new myclass2();
c2obj1.method2();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 97
R.N.REDDY
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 98
R.N.REDDY
Q) How to pass a value to the function?
A) To pass a value to the function we are taking the help of parameters.
Q) What is parameter?
A) A variable which is declared within the method signature can be called as
parameter.
namespace myexample
{
public class myclass
{
public static void add(int a, int b)
{
}
}
class Program
{
static void Main(string[] args)
{
int x = 10;
int y = 20;
myclass.add(x, y);// x and y are actual parameters
}
}
}
Actual parameters: Calling function parameters are actual parameters
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 99
R.N.REDDY
C#.net will support passing parameter mechanism in three ways
1) Call by value or pass by value
2) Call by reference or pass by reference
3) Call by out or pass by out
1) Call by value:
In call by value when formal parameters are modified, the modifications will
not be reflected back to actual parameter.
In call by value while passing the parameter we will pass actual value
Prgm: Example for call by value
namespace callbyvalueExample
{
public class college
{
public void payfee(int a) // a is formal parameter
{
a = a + 1500;
Console.WriteLine("a value is:" + a);
}
}
class Program
{
static void Main(string[] args)
{
college obj = new college();
int x = 1000;
obj.payfee(x);
Console.WriteLine("x value is :" + x);// x is actual parameter
Console.ReadLine();
}
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 100
R.N.REDDY
Output:
2) Call by reference:
In call by reference when formal parameters are modified the modifications
will be reflecting back to actual parameters.
In call by reference while passing the parameters and while catching the
parameters we should use “Ref” keyword
Ref parameter should be initialized with some value otherwise compiler will
generate an error.
In call by reference while passing the parameter we are sending the address of
the value.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 101
R.N.REDDY
Prgm: Example for call by reference
namespace callbyreferenceexample
{
public class college
{
public void payfee(ref int a)
{
a = a + 1500;
Console.WriteLine("a value is:" + a);
}
}
class Program
{
static void Main(string[] args)
{
college obj = new college();
int x = 1000;
obj.payfee(ref x);
Console.WriteLine("x value is :"+x);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 102
R.N.REDDY
Call by out:-
In call by out when formal parameters are modified those modifications will be
reflected back to actual parameters it is almost all same as call by reference.
In call by out while passing the parameters and while catching the parameters we
have to use out keyword.
Out parameter is not required to initialize with any value if we initialize also CLR
will ignores that value.
In call by out also while passing the parameters we are passing the address of the
parameter.
Prgm: Example for call by out
namespace callbyoutexample
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 103
R.N.REDDY
public class employee
{
public void payfee(out int totfee)
{
int adminfee=1200;
int semfee=1400;
totfee=adminfee+semfee;
Console.WriteLine("total fee is :"+totfee);
}
}
class Program
{
static void Main(string[] args)
{
employee obj = new employee();
int x;
obj.payfee(out x);
Console.WriteLine("x value is :" + x);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 104
R.N.REDDY
}
For this class by default access modifier will be internal
Q) What is the default access modifier of a class member?
A) Private.
Default access modifier of an instance variable, static variable, instance method
and static method is private.
Q) What is the default access modifier for instance constructor?
A) Private.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 106
R.N.REDDY
Constant:
Constant is a data member which is representing a fixed value.
Constant value should be initialized at the time of declaration i.e. in design
time but we cannot initialize in run time.
By default constant is a static member, we don’t require to use any static
keyword.
Constant we have to access with the help of class name.
class Employee
{
public const int MinworkHrs = 9;
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("minimum working hours per day is: " +
Employee.MinworkHrs);
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 107
R.N.REDDY
Console.ReadLine();
}
}
}
Output:
In the above constant example program minimum working hours field we have
declared as constant but there is a chance that company may change min working
hrs per day is 8.
In this situation we cannot declare it as constant
For ex: Let us assume we have declared company name as constant but there is a
chance that company name also may change i.e. before 2008 company name is
Satyam computers after 2008 company name is Mahindra satyam due to that
reason company name, college name which also cannot declare as constants.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 108
R.N.REDDY
Minimum working hours, company name, college name which are all having data
for all objects but there is a chance of change in the future due o that reason these
fields we have to declare as static variables
Q) When we go for Constant?
A) In business applications (Enterprise application) in general we will use constants
very rarely instead of that we will use static variables.
When we are developing scientific application to represent universal fixed values
like pi, speed of light and so………..on
Public const double pi=3.14;
Public const long speed of light=300000;
Note: speed of light is 300000 km/sec.
Comparision between static and constant
Constant Static variable
1) Const 1) Static
2) By default constant is static i.e. 2) Static variable program has to
we don’t require to use any static declare as static.
keyword.
3) Constant should be initialized at 3) Static variable can be initialized at
the time of declaration which we the time of declaration as well as
cannot initialize in run time. we can initialize in run time.
4) Constant value cannot be 4) Static variable can be changed.
changed. 5) When we will go for static
5) When we will go for constant? variable?
Value which is common for all the A value which is common for all
objects and value should be fixed the objects and there is a chance to
forever particular field we will change value then particular field
declare as constant. we will declare as static variable.
Ex: pi=3.14, speed of Ex: college name, companyname.
light=300000.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 109
R.N.REDDY
Readonly:
Readonly is a data member which is representing a value.
To declare readonly we have to use readonly keyword
Readonly can be initialized only at the time of declaration
Readonly can be initialized in runtime i.e. it should be initialized within the
constructor.
By default readonly is a non-static member.
Readonly cannot be initialized within the method, as well as readonly value
we cannot change or modify within the readonly.
According to oops:
1) Constructor purpose is initializing the variable values only.
2) Method is to change the variable values.
Prgm: Example for readonly
namespace wipro
{
class Employee
{
readonly int EmpNo;
readonly string EmpName;
public Employee(int EmpNo, string EmpName)
{
this.EmpNo = EmpNo;
this.EmpName = EmpName;
}
public void Dispaly()
{
Console.WriteLine("employee no is:" + EmpNo);
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 110
R.N.REDDY
Console.WriteLine("employee name is:" + EmpName);
}
}
class Program
{
static void Main(string[] args)
{
Employee emp1 = new Employee(123, "jhon");
emp1.Dispaly();
Employee emp2 = new Employee(124, "David");
emp2.Dispaly();
Console.ReadLine();
}
}
}
Emp1 Object
Empno=123
1010 Empname=john
1010
Emp2 Object
Empno=124
1020
Empname=David
1020
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 111
R.N.REDDY
In our old program empno and empname we have declared as instance variables but
instance variables allow to change the values with the help of instance methods, but
empno and empname will not require to change because which are fixed for object
to object(emp to emp) due to that reason we have declared empno and empname as
readonly variables.
Example for readonly: In student class student id, student name. In customer class
customer id and customer name.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 113
R.N.REDDY
Readonly variable cannot be modified once we are initialized to overcome this in
the above example sal we have to declare as instance variable like below
Double sal;
Now executing the program
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 114
R.N.REDDY
Readonly. No keyword.
Non static. Non static.
It can be initialized at the time of Instance variable can be initialized at
declaring or runtime (only within the the time of declaration or at the run
constructor). time with in the constructor or within
the method.
Readonly variable cannot be Instance variable value can be
modified. changed.
When we will go for readonly? When we will go for instance
Whenever we want to initialize a variable?
value in runtime and value will be Whenever we want to initialize a
fixed for object to object value in run time and that value
Ex: empno, empname. should allow changing.
Ex: sal.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 116
R.N.REDDY
static void Main(string[] args)
{
Program obj = new Program();
Console.WriteLine("instance variable value is:" + obj.a);
obj.show();
Console.WriteLine("static variable value is:" + b);
print();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 117
R.N.REDDY
With the above program we can say that whenever we are accessing instance
members from a static method even though from the same class we require object.
Write the C#.net code program within the notepad and compile, execute from
visual studio.net command prompt
Step1: open notepad.
Step2: Write the below code within the notepad.
Using System;
Using System.Collections.Generics;
Using System.Linq;
Using System.Text;
namespace notepadexample
{
Class program
{
Static void main(string[] args)
{
for(int i=1;i<=10;i++)
{
Console.WriteLine(“satya”);
}
Console.ReadLine();
}
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 118
R.N.REDDY
Step3: Save this program with program.cs in d:\ drive
Step4: With this process one C#.net class file will save within the D:\ drive
Step5: Compiling the above program
Start => programs=> visual Studio=>visual studio Tools=>visual studio command
prompt
It will open visual studio.net command prompt window change path to D:\drive
D:\csc program.cs
With this process compilation will complete i.e. it will convert high level language
code (C#.net) into MSIL code finally within the D:\ drive it will generate
program.exe file; this .exe file will contain MSIL code.
Executing the program:
D:\ program
Satya
Satya
Satya
Satya
Satya
Satya
Satya
Satya
Satya
This process will execute the above program and will give the output like above
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 119
R.N.REDDY
Properties:
Properties is a member of a class which we can use to assign a value into class
level variable as well as we can retrieve the value from class level variable
Using property we can write a property into variable as well as we can read the
value of variable.
Property will not hold any value or data which we can use to transfer the data.
To assign the value to retrieve the value property definition will have two
accessories (methods).
1) Set accessor
2) Get accessor
Set accessor:
It will assign the value to variable.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 120
R.N.REDDY
Syntax:
<Access modifier> <data type> <property name>
{
Set
{
<data member>=value;
}
}
Whenever we are assigning a value then set accessor will call, as part of set
accessor execution first assigned value will be into value predefined variable,
and then it will be assigning into user defined variable.
Get accessor:
This accessor will be reading the value or retrieving the value from variable.
Syntax:
<access modifier> <data type> <property name>
{
Get
{
Return <data member>;
}
}
Whenever we are retrieving a value with the help of property that concern
property get accessor will execute.
Types of properties:-
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 121
R.N.REDDY
Write properties: - This property definition will have only set accessor.
Read and write properties: - This property definition will contain set accessor
and get accessor.
Syntax:
<Access modifier> <data type> <property>
{
Get
{
Return <data member>;
}
Set
{
<Data member>= value;
}
}
According to the modifier or keyword which we are using to define a property,
again properties are classified into two types
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 122
R.N.REDDY
1) Non static property or instance property.
2) Static property.
Instance property: - while defining a property if we didn’t use static keyword
which can be called as instance property.
Using instance property we can assign value to an instance variable as well as
we can retrieve value from instance variable.
Static property: - while defining a property if we use static keyword then that
property is called as static property.
Using this property we can assign a value to static variable as well as we can
retrieve value from static variable.
Note:
Variable name and the property which we are using to access that variable
name should be same.
Variable name can start with lower case letter but property name should start
with upper case.
Ex: 1) Variable name is: empno
Property name is: Empno
2) Variable name is: empname
Property name is: Empname
To access one variable for assignment for retrieval we have to define one
property i.e. one variable one property.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 123
R.N.REDDY
For ex:-
class employee
{
int empno;
public int Empno
{
get
{
return empno;
}
set
{
empno = value;
}
}
}
By default access modifier of a class is private, in general we will access
variable only within the class.
Whenever we want to access a variable for assignment and retrieval we don’t
require property mechanism.
Whenever a variable of a class for assignment retrieval if we want to
implement from outside class we require a mechanism called property.
Purpose of property is providing assignment and retrieval facility to a variable
from outside class without directly accessing the variable.
Property will provide security to variable data.
Property will provide validation facility for variable data at the time of
assigning.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 124
R.N.REDDY
Prgm: Example for instance property
namespace instnacepropertyexample
{
class employee
{
int empno;
string empname;
double empsal;
public int Empno
{
get
{
return empno;
}
set
{
empno = value;
}
}
public string Empname
{
get
{
return empname;
}
set
{
empname = value;
}
}
public double Empsal
{
get
{
return empsal;
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 125
R.N.REDDY
}
set
{
empsal = value;
}
}
}
class Maincalss
{
static void Main(string[] args)
{
employee emp1 = new employee();
emp1.Empno = 121;
emp1.Empname = "jyothi";
emp1.Empsal = 1000;
Console.WriteLine("Employee no is:" + emp1.Empno);
Console.WriteLine("Employee name is" + emp1.Empname);
Console.WriteLine("Employee salary is" + emp1.Empsal);
Console.ReadLine();
}
}
}
Output:-
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 126
R.N.REDDY
In every property definition within the set accessor we have to use a predefined
variable called value.
Here value variable is a implicit typed variable i.e. when we will use this
variable within the int property then that variable data type will be int.
If value variable we are using within the string property then that variable data
type will be string.
Prgm: Example for static property
namespace Staticpropertyexample
{
class employee
{
static string companyname;
public static string Companyname
{
get
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 127
R.N.REDDY
{
return companyname;
}
set
{
companyname = value;
}
}
}
class Program
{
static void Main(string[] args)
{
employee.Companyname="microsoft";
Console.WriteLine("Comapny name is:"+employee.Companyname);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 128
R.N.REDDY
Encapsulation:-
Wrapping states and behaviors is called as encapsulation.
Binding variables and methods is called as encapsulation.
Q) How to encapsulate?
A) By implementing class.
Abstraction
Abstraction means hiding
Abstractions are 2 types
1) Data abstraction 2) Method abstraction
1) Data abstraction:-
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 129
R.N.REDDY
Data abstraction means hiding the unwanted data.
Prgm: Example for data abstraction
namespace dataabstraction
{
class employee
{
readonly int empno;
readonly string empname;
double empsal;
string designation;
public employee(int empno, string empname, double empsal, string
designation)
{
this.empno = empno;
this.empname = empname;
this.empsal = empsal;
this.designation = designation;
}
public void display()
{
Console.WriteLine("empno is"+empno);
Console.WriteLine("empname is"+empname);
Console.WriteLine("empsal is"+empsal);
Console.WriteLine("designation is"+designation);
}
public void hike()
{
double incsal = empsal * 10 / 100;
Console.WriteLine("empno:"+empno+"empname:"+empname+"u
pdated salary details");
empsal = empsal + incsal;
Console.WriteLine("updated salary is:"+empsal);
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 130
R.N.REDDY
}
class Program
{
static void Main(string[] args)
{
employee emp = new employee(121,"sri",1000,"s/w engg");
emp.display();
emp.hike();
Console.ReadLine();
}
}
}
Output:
In the above program within the employee class we have 4 variables they are
empno, empname, empsal, designation
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 131
R.N.REDDY
Within the hike () we are accessed only empno, empname, empsal but we are
not accessed designation because designation is unnecessary data for hike ()
that means we are implemented data abstraction within the hike ()
Inheritance:-
Inheriting or deriving members from one class to another class is called as
inheritance
To implement inheritance minimum we require 2 classes
Syntax:-
Class c1
{
}
Class c2:c1
{
}
The class which is giving is called as super class & the class which is
receiving is called as sub class
Because of inheritance derived class can access derived class members as
well as super class members
Similarly using derived class object can access derived class members as
well as super class members.
But super class cant access derived class members
C#.net will support 5 types of inheritance
1) Single inheritance
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 132
R.N.REDDY
2) Multi level inheritance
3) Multiple inheritances
4) Hierarchal inheritance
5) Hybrid inheritance
1) Single inheritance:-
Inheriting from one class to another class is called as single inheritance
Syntax:-
Class c1
{
}
Class c2:c1
{
}
Prgm: Example1 for single inheritance
namespace singleinheritance1
{
class bc
{
protected int a = 10;
public void bcmethod()
{
Console.WriteLine("bcmethod value is"+a);
}
}
class dc : bc
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 133
R.N.REDDY
{
public void dcmethod()
{
Console.WriteLine("dcmethod value is" + a);
}
}
class Program
{
static void Main(string[] args)
{
dc obj = new dc();
obj.bcmethod();
obj.bcmethod();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 134
R.N.REDDY
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 135
R.N.REDDY
Console.WriteLine("enter emp name:");
empname = Console.ReadLine();
Console.WriteLine("enter empsal:");
empsal = double.Parse(Console.ReadLine());
}
public void empdisplay()
{
Console.WriteLine(" empno:" + empno);
Console.WriteLine(" emp name:" + empname);
Console.WriteLine(" empsal:" + empsal);
}
}
class Program
{
static void Main(string[] args)
{
employee emp = new employee();
emp.setbranchdata();
emp.display();
emp.setempdata();
emp.empdisplay();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 136
R.N.REDDY
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 140
R.N.REDDY
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 141
R.N.REDDY
}
}
class employee : branch
{
int empno;
string empname;
string Designation;
protected void setempdata()
{
base.setbranchdata();
Console.WriteLine("enter empno:");
empno = int.Parse(Console.ReadLine());
Console.WriteLine("enter emp name:");
empname = Console.ReadLine();
Console.WriteLine("enter Designation:");
Designation = Console.ReadLine();
}
protected void empdisplay()
{
base.displayBranchdata();
Console.WriteLine(" empno:" + empno);
Console.WriteLine(" emp name:" + empname);
Console.WriteLine(" Designation is:"+Designation);
}
}
class salary : employee
{
double basic, hra, da, gross;
internal void setsal()
{
base.setempdata();
Console.WriteLine("enter basic sal:");
basic = double.Parse(Console.ReadLine());
}
internal void calgrosssal()
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 142
R.N.REDDY
{
hra = 0.4 * basic;
da = 0.2 * basic;
gross = basic + hra + da;
}
internal void displaysal()
{
base.empdisplay();
Console.WriteLine("basic sal is:" + basic);
Console.WriteLine("hra sal is" + hra);
Console.WriteLine("da sal is:" + da);
Console.WriteLine("gross sal is:" + gross);
}
}
class Program
{
static void Main(string[] args)
{
salary obj = new salary();
obj.setsal();
obj.calgrosssal();
obj.displaysal();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 143
R.N.REDDY
Base():-
It is a keyword which is representing the super class instance using base()
keyword we can access super class instance members from derived class
Multiple inheritance:-
Inheriting from multiple classes to single class is called as multiple
inheritance
Syntax:-
Class c1
{
}
Class c2
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 144
R.N.REDDY
{
}
Class c3:c1, c2
In c#.net multiple inheritance is not possible by using classes which is
possible with the help of interface
Q) Why c#.net will not support multiple inheritances with the help of
classes?
Hierarchal inheritance:-
Inheriting one class into multiple classes is called as hierarchal inheritance
In this class we have one base class and multiple derived classes
Syntax:-
Class c1
{
}
Class c2:c1
{
}
Class c3:c1
{
}
Prgm: Example for hierarchal
namespace hierarchal
{
class headoffice
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 145
R.N.REDDY
string headlocation = "banglore";
long headofficenum = 080 - 6666822;
internal void displayheadoffice()
{
Console.WriteLine("location is"+headlocation);
Console.WriteLine("headoffice phno is"+headofficenum);
}
}
class branch1 : headoffice
{
int branchid = 123;
string branchname = "manikonda";
internal void displaybranch1()
{
Console.WriteLine("branch1 id is:"+branchid);
Console.WriteLine("branch1 name is:"+branchname);
Console.WriteLine("head office details are:");
base.displayheadoffice();
}
}
class branch2 : headoffice
{
int branchid = 124;
string branchname = "madhapur";
internal void displaybranch2()
{
Console.WriteLine("branch2 id is:" + branchid);
Console.WriteLine("branch2 name is:" + branchname);
Console.WriteLine("head office details are:");
base.displayheadoffice();
}
}
class Program
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 146
R.N.REDDY
{
static void Main(string[] args)
{
branch1 obj = new branch1();
obj.displaybranch1();
branch2 obj1 = new branch2();
obj1.displaybranch2();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 147
R.N.REDDY
Hybrid inheritance
A combination of 2 inheritances is called as hybrid inheritance.
At the time of implementing inheritance we are implemented single
inheritance and multi level inheritance(or) multi-level inheritance &
multiple inheritance (or) multiple inheritance & hierarchal inheritance can
be called as hybrid inheritance.
CLR inside or CLR internals:-
When we run the .net application .net execution engine called CLR will be
loading into Ram, if we will execute java application java execution engine
called JVM will be loading into Ram memory.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 148
R.N.REDDY
Class loader of the CLR will be loading the classes of the application one by
one into CLR according to the requirement.
To allocate the memory for class members mainly CLR will be divide into 4
parts (we will have some more parts)
They are
1) Stack.
2) Heap.
3) Method Area.
4) Execution engine.
Stack: - CLR is allocating memory for local variables and reference variables within
the stack.
Heap: - CLR will create objects within the heap memory. As part of creating object
it is allocating memory for instance variables and that object will have the references
of methods.
Method area: - Method area will be divided into 3 parts like below
Method area
Static variables Constructors methods
When the class is loading concern class static variables will be allocating
memory within the method area then it is loading all the constructors and
methods into method area.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 149
R.N.REDDY
In method area only static variables are getting allocating memory but for
methods and constructors no memory is allocating.
Execution Engine:-
This is using by the CLR to maintain the block (method) which is executing at
present
When we run the application it requires first main method due to that reason
CLR will move the main method from method area to execution engine.
Within the main method it will execute the instruction by instruction, as part of
this whatever methods it requires those methods it will move from method area
to execution engine.
Once a particular method execution is completed then CLR will remove that
method from execution engine like below
Execution Engine
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 150
R.N.REDDY
Main ()
{
M1 ();
M2 ();
.
.
.
.
}
M1 ()
{
.
.
.
}
M2
{
.
.
}
Prgm: Example
namespace CLRExample
{
class calculate
{
int a;
int b;
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 151
R.N.REDDY
internal void Add(int x, int y)
{
a = x;
b = y;
int res1 = a + b;
Console.WriteLine("addition result is:" + res1);
}
}
class Program
{
static void Main(string[] args)
{
calculate obj = new calculate();
obj.Add(10, 5);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 152
R.N.REDDY
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 153
R.N.REDDY
Static heap method area
a=10
Static main () add ()
(4 bytes)
{ {
b=5 . .
(4 bytes) . .
} }
CLR
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 155
R.N.REDDY
Among collection of methods with the same name, to invoke one method,
compiler is taking in runtime is called as dynamic polymorphism or runtime
polymorphism is nothing but late binding.
Dynamic polymorphism we can achieve while we are implementing function
overriding.
Q) What is function overloading?
A) Having multiple methods with the same name, but a different number of
arguments or different type of arguments or different order of arguments in a single
class or in a combination of base and derived class.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 156
R.N.REDDY
internal void Add(double a, double b)
{
double res = a + b;
Console.WriteLine("add of 2 double result is:" + res);
}
internal void Add(double a, double b, double c)
{
double res = a + b + c;
Console.WriteLine("add of 3 double result is:" + res);
}
}
class Program
{
static void Main(string[] args)
{
calculate obj = new calculate();
obj.Add(10, 5);
obj.Add(10, 5, 2);
obj.Add(10.5, 20.5);
obj.Add(10.5, 20.5, 30.5);
Console.ReadLine();
}
}
}
When we compile the above program because of this statement obj.Add (10, 5)
compile is binding to first add method.
Because of this statement obj.Add (10, 5, 2), compile is binding to second add
method.
Because of this statement obj.Add (10.5, 20.5), compile is binding to third add
method.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 157
R.N.REDDY
Because of this statement obj.Add (10.5, 20.5, 30.5), compile is binding to
fourth add method.
These are all bindings are happening at compilation which are called as early
bindings.
When we run the above program the above 4 statements are invoking the
methods which are binded at compile time like below due to that reason we can
say that we are achieving the static polymorphism in function overloading.
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 158
R.N.REDDY
1) It will depend on different number of arguments to a function.
2) It will depend on different type of arguments to a function.
3) It will depend on different order of arguments to a function.
Prgm: Example 2 for function overloading with two different order of parameters of
a function
namespace functionoverload2
{
class myclass
{
internal void print(int a, string s)
{
Console.WriteLine("a is:" + a + ",s is:" + s);
}
internal void print(string s, int a)
{
Console.WriteLine("s ia:" + s + ", a is:" + a);
}
}
class Program
{
static void Main(string[] args)
{
myclass obj = new myclass();
obj.print(10, "satya");
obj.print("satya", 10);
Console.ReadLine();
}
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 159
R.N.REDDY
Output:
The above two functions we can overload based on different order of parameters
1) Number of arguments to a function:
Ex:
namespace functionoverload
{
class myclass
{
internal void print()
{
Console.WriteLine("Zero argument function is calling");
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 160
R.N.REDDY
}
internal void print(int a)
{
Console.WriteLine("one argument function value is:" + a);
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.print();
mc.print(10);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 161
R.N.REDDY
2) Type of parameters to a function:
namespace functionoverload4
{
class myclass
{
internal void print(int a)
{
Console.WriteLine("int value is :"+a);
}
internal void print(string s)
{
Console.WriteLine("string value is :"+s);
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.print(10);
mc.print("sitha");
Console.ReadLine();
}
}
}
Output:-
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 162
R.N.REDDY
In the above program we can overload two print functions because of different
type of parameters i.e. first print function is having one int parameter and second
print function is having one string parameter.
With this we can say that function overloading is depending on type of parameters
to a function.
Function overloading will not depend on following two functions
1) Return type of a function.
2) Name of the parameter to a function.
Prgm: Example for return type of a function
namespace functionoverload5
{
class myclass
{
internal void print()
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 163
R.N.REDDY
{
Console.WriteLine("void function is calling");
}
internal int print()
{
return 123;
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.print();
int i = mc.print();
Console.ReadLine();
}
}
}
In the above program at the time of compiling the below statement
mc.print();
Here compiler will try to map with print () function with zero parameters, but within
the class we have two print methods with zero parameters due to the reason,
compiler unable to bind and it will throw an error with this we can say, we cannot
overload two functions only by differentiating return types
Prgm: Example for name of the parameter to a function
namespace functionoverload6
{
class myclass
{
internal void print(int a)
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 164
R.N.REDDY
{
Console.WriteLine("a is :" + a);
}
internal void print(int b)
{
Console.WriteLine("b is :" + b);
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.print(10);
mc.print(20);
Console.ReadLine();
}
}
}
When we compile the above program, at the time of compiling below statement
mc.print(10)
here compiler will try to bind with a print function which is having one int
parameter, but within the myclass we have two print () functions with one int
parameters and compiler will unable to bind it will throw an error, with this we can
say that we cannot overload with two functions by differentiating only with the
name of parameters, finally we can say function overload is not depending on the
name of the parameters.
Conclusion: -
Function overloading means multiple methods with the same name but with
different signature in a single class or in a combination base and derived class.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 165
R.N.REDDY
Prgm: Example 6 for function overloading
namespace functionoverload6
{
class myclass
{
internal void method1(int a)
{
Console.WriteLine("a is :" + a);
}
internal int method1()
{
return 10;
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.method1(10);
int a = mc.method1();
Console.WriteLine("returned value is:" + a);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 166
R.N.REDDY
Q) Can we overload the two methods i.e. one method in super class and another
method in sub class?
namespace functionoverload7
{
class bc
{
internal void print()
{
Console.WriteLine("bc print is calling");
}
}
class dc:bc
{
internal void print(int a)
{
Console.WriteLine("dc print value is calling");
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 167
R.N.REDDY
}
}
class Program
{
static void Main(string[] args)
{
dc obj=new dc();
obj.print();
obj.print(10);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 168
R.N.REDDY
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 169
R.N.REDDY
With this above example we can say that we can overload static functions.
Q) Can we overload one static and one instance function?
A) Yes
Q) Can we overload the two instance constructors?
A) Yes
Q) Can we overload static constructor?
A) No, because a class can contain only one static constructor.
Q) When we will go for function Overloading?
A) example for function overloading
namespace functionoverload8
{
class Employee
{
int empno;
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 170
R.N.REDDY
string empname;
double Basic, Hra, Da, Ta, GrossSal;
public Employee(int empno, string empname)
{
this.empno = empno;
this.empname = empname;
}
internal void calsal(double Basic, double Hra)
{
GrossSal = Basic + Hra;
Console.WriteLine("Empno is:" + this.empno + "Emp name is:" +
this.empname + "grosssal is:" + this.GrossSal);
}
internal void calsal(double Basic, double Hra, double Da)
{
this.GrossSal = Basic + Hra + Da;
Console.WriteLine("Empno is:" + this.empno + "Emp name is:" +
this.empname + "grosssal is:" + this.GrossSal);
}
internal void calsal(double Basic, double Hra, double Da, double Ta)
{
this.GrossSal = Basic + Hra + Da + Ta;
Console.WriteLine("Empno is:" + this.empno + "Emp name is:" +
this.empname + "grosssal is:" + this.GrossSal);
}
}
class Program
{
static void Main(string[] args)
{
Employee softwareengineer = new Employee(121, "jhon");
softwareengineer.calsal(1000, 400);
Employee manager = new Employee(122, "david");
manager.calsal(2000, 800, 400);
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 171
R.N.REDDY
Employee CEO = new Employee(123, "philips");
CEO.calsal(3000, 1200, 600, 300);
Console.ReadLine();
}
}
}
Output:
In the above example we have class but we have 3 types of employee’s software
engineer, manager, CEO
Within this class we have to calculate the salary for employees
We have to implement one method i.e. calsal but behavior of the calculating salary
for software engineer, manager, CEO is different, due to that reason we have
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 172
R.N.REDDY
implemented 3 calsal methods with different signature with this we can say in the
above example we are implemented function overloading
Note:
Whenever we want to implement same method with the different functionalities then
we have to go for function overloading.
Q) List out the overloaded methods which are provided by the Microsoft with in
console class
Namespace system
{
Class console
{
//1
Console.WriteLine (long)
{
}
//2
Console.WriteLine (int a)
{
}
//3
Console.WriteLine (float a)
{
}
.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 173
R.N.REDDY
.
.
.
.
.
.
.
//19
Console.WriteLine ()
{
}
// 1
Console. Write (int a)
{
}
//2
Console. write ( float a)
{
}
.
.
.
.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 174
R.N.REDDY
.
//18
Console.Write( long )
{
}
Function Overriding
1. Function overriding means having multiple methods with the same name and
with the same signature in a combination of base and derived class.
2. To implement function overriding for base class method, we should use virtual
keyword and for derived class method we should use override keyword.
representing by signature.
For example:
Internal void show ()
{
----
----
}
Q) What do you mean by same signature?
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 175
R.N.REDDY
A) 1.Both methods i.e. virtual method and override method number of
arguments, type of arguments and order of arguments should be same to
same.
2. In overriding both methods return type should be same to same.
3. In function overriding both class methods access modifier can be same to
same or cannot be same it is based on requirement i.e. there is no
condition that both access modifiers should be same to same.
Q) Can we implement overriding in single class?
A) No, we have to go for two classes
1. Base class 2.Derived class.
Function overriding is depending on inheritance concept, due to that reason we
have to
Understand the below concept of inheritance.
Prgm: Example for inheritance by creating base class reference variable with
derived class object.
namespace baseclassrefex
{
class bc
{
int a = 10;
internal void bcmethod()
{
Console.WriteLine("bc method value is :" + a);
}
}
class dc : bc
{
int b = 20;
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 176
R.N.REDDY
internal void dcmethod()
{
Console.WriteLine("dc method value is :" + b);
}
}
class Program
{
static void Main(string[] args)
{
bc b = new dc();
b.bcmethod();
Console.ReadLine();
} method area
}
}
bc method()
object of dc class
Bc class {
Object of bc class }
b
1010 a=0 10
1010 dc method ()
b=0 20 {
}
1020
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 177
R.N.REDDY
output:
Method abstraction:
Method abstraction means hiding the methods.
namespace methodabstex
{
class myclass
{
internal void print()
{
}
internal void print(int a)
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 178
R.N.REDDY
{
}
internal void print(string s)
{
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.print();
Console.ReadLine();
}
}
}
In the above program when the control is executing the below statement i.e.
mc.print();
Here control will invoke the first print and it is hiding the remaining two print
functions is nothing but method abstraction.
Q) What do you mean by compile time binding?
A) Compile time binding is achieving in function overloading like below:
namespace compilebindingex
{
class myclass
{
internal void print()
{
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 179
R.N.REDDY
internal void print(int a)
{
}
internal void print(string s)
{
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
mc.print();
}
}
}
When we compile the above program myclass mc, here it will check for the
myclass is available or not, if myclass is not available then it will throw compile
time error.
After this, it will compile mc.print (); with this statement first it will check a
print function which is having zero arguments if it is not available it will generate
compile time error.
If it will bind to first print function like below.
Print ()
Mc {
}
When we run the above program, because of below statement i.e. new myclass();
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 180
R.N.REDDY
It will create a object of myclass
After that it will execute below statement myclass mc=,it will create a reference
variable of myclass type and which is holding the object of myclass type, here
reference variable type and object type both are same due to that reason when we
execute the below statement mc.print () will invoke the first print function which
was bind at compile time.
In the above example whatever method is binded at compile time is executing in
run time, due to that function overloading is nothing but static binding (or) early
binding (or) compile time binding.
Prgm: Example for function overriding.
namespace functionoverridingex2
{
class bc
{
internal virtual void display()
{
Console.WriteLine("bc display is calling");
}
}
class dc:bc
{
internal virtual void display()
{
Console.WriteLine("dc display is calling");
}
}
class Program
{
static void Main(string[] args)
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 181
R.N.REDDY
{
bc b=new dc();
b.display();
Console.ReadLine();
}
}
}
Output:
In the above example, when we compile, because of the below two statements, it
will bind to the class display method.
bc b;
b.display ();
When we compile the above program because of below statement bc b=new
dc(); it will Create a object of dc class & reference variable of bc class, due
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 182
R.N.REDDY
to that reason here control will go to bc class we have virtual method due to
that reason which will try to execute the override method which is available
in dc class.
In the above example at compile tie because of bc b (); it is binding to bc class
display method like below.
bc b=new dc(); class
b.display (); {
Display ()
{
}
}
bc b=new bc();
b.display ();
O/p: bc display is calling.
A) No, it is not a dynamic polymorphism because in compile time we are
binding bc display(),in runtime also we are invoking the bc display()
dc d= new dc ();
d.display ();
In compile time, because of dc d; d.display (); here control will go to dc
class, but dc class will have two display methods.
1) Virtual method. 2) Override method.
Now control will give priority for override due to that reason it will bind
override method.
In runtime also control is invoking the override display method; due to that
reason we can say that it is not dynamic polymorphism.
Q) Why we have to define in base class & derived class two methods with
the same name or when we will go for function overriding?
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 184
R.N.REDDY
A) Whenever we have common requirement in two classes with different
behavior we have to define two methods with the same name in a combination of
base and derived class with the same signature with different behavior is nothing
but function overriding.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 186
R.N.REDDY
namespace functionoverridingex4
{
class employee
{
internal virtual void calsal(double basicsal)
{
double hra=0.4*basicsal;
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 187
R.N.REDDY
double grosssal=basicsal+hra;
Console.WriteLine("employee grosssal is :"+grosssal);
}
}
class manager : employee
{
internal override void calsal(double basicsal)
{
double hra = 0.4 * basicsal;
double da = 0.2 * basicsal;
double grosssal = basicsal + hra + da;
Console.WriteLine("manager grosssal is :" + grosssal);
}
}
class vicepresident:employee
{
internal override void calsal(double basicsal)
{
double hra = 0.4 * basicsal;
double da = 0.2 * basicsal;
double ta = 0.3 * basicsal;
double grosssal = basicsal + hra + da + ta;
Console.WriteLine("vicepresident grosssal is :" + grosssal);
}
}
class Program
{
static void Main(string[] args)
{
employee obj=new manager();
obj.calsal(1000);
obj = new vicepresident();
obj.calsal(2000);
Console.ReadLine();
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 188
R.N.REDDY
}
}
}
Obj object
Vicepresident
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 189
R.N.REDDY
An object address can hold by that class reference variable and super class
reference variable.
In the above example, here
employee obj=new manager();
Obj. Calsal (1000);
Here it is creating a object of manager class and assigning manager class address
into obj reference variable of employee class and invoking manager class
method.
With this manager class object usage is completed i.e. here we don’t require
manager class object now we have o create vice-president class object due to that
reason we have written the below statement
Obj=new vicepresident ();
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 190
R.N.REDDY
It is creating vice president class object and assigning that object address into obj
reference variable of employee class.
With this manager class object reference will be disconnected and vicepresident
class object reference will be connected as above figure.
Now manager class object will be recognized as unused object, then garbage
collector will destroy the manager object.
In this way, we can create multiple objects of derived classes and holding that
object by super class reference variable by destroying the unused objects, because
of super class reference variable.
With this at a time only one object i.e. current object will be alive.
Finally, we can say that by implementing this we can save the memory and
because of single object at a time it improves the performance.
Q) Can we invoke a method of super class by using super class refernce
variable and subclass object?
A) yes, but we should not implement virtual and override.
Q) can we override static method?
A) No, we cannot define static method as virtual, override, abstract, because
function overriding is related to or depending on instance or object.
Q) Can we invoke subclass method by using super class reference variable
and subclass object?
A) Yes, methods should be virtual and override.
METHOD HIDING:
Hiding the super class method within the subclass is called method hiding.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 191
R.N.REDDY
To implement method hiding base class method we don’t required a keyword
like virtual but derived class method required a keyword called “new”.
NOTE:
Implementation of function overriding and method hiding will be same
but for function overriding we will use two keywords:
1. Virtual keyword for base class method.
2. Override keyword for derived class method.
For method hiding
1. For base class method no keyword.
2. For derived class method new keyword.
Whenever we want to hide the super class method within the subclass and sub
class method do required to invoke with derived class reference variable and
derived class object then we will go for method hiding.
Prgm: Example for method hiding
namespace methodhidingex
{
class bc
{
internal void display()
{
Console.WriteLine("bc display is calling:");
}
}
class dc : bc
{
internal new void display()
{
Console.WriteLine("dc display is calling");
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 192
R.N.REDDY
}
}
class Program
{
static void Main(string[] args)
{
dc d = new dc();
d.display();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 194
R.N.REDDY
{
double hra = 0.4 * basicsal;
double da = 0.2 * basicsal;
double ta = 0.2 * basicsal;
double carallowance = 0.3 * basicsal;
double grossal = hra + da + ta + carallowance + basicsal;
Console.WriteLine("ceo grossal is:" + grossal);
}
}
class Program
{
static void Main(string[] args)
{
ceo obj = new ceo();
obj.calsal(5000);
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 195
R.N.REDDY
namespace HIDINGANDOVERRIDINGEX
{
class bc
{
internal virtual void display()
{
Console.WriteLine("bc display is calling:");
}
}
class dc : bc
{
internal new void display()
{
Console.WriteLine("dc new display is calling:");
}
}
class tc:bc
{
internal override void display()
{
Console.WriteLine("tc override display is calling");
}
}
class Program
{
static void Main(string[] args)
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 197
R.N.REDDY
bc b = new bc();
b.display();
dc d = new dc();
d.display();
b = new tc();
b.display();
Console.ReadLine();
}
}
}
Output:
Abstract class:
Abstract class is a collection of abstract members and bon abstract
members.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 198
R.N.REDDY
To define abstract class we have to use abstract keyword.
Syntax:
Abstract class <class name>
{
//abstract members
//non abstract members
}
Q) What do you mean by abstract members?
A) While defining a class member, if we have used abstract keyword which can
be called as abstract member.
Abstract members are empty members, which will not have any
implementation, that means abstract members will have only declaration part
within the abstract class; we have to define or implement these abstract
members within the derived class by using override keyword.
Q) What do you mean by declaration part and what do you mean by
definition part?
A) For ex:
Void display()
{
Console.writeline(“hi”);
}
By default, abstract members, while declaring the abstract we don’t
require to use virtual keyword
We cannot declare a variable as abstract.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 199
R.N.REDDY
We cannot declare a static member as a abstract member.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 200
R.N.REDDY
are reimplementing within these abstract members we
The derived class by using have to implement within he
override derived class by using
keyword override keyword
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 201
R.N.REDDY
A) No
}
class dc : vehicle
{
//implementing abstract members using override keyword
internal override void drive()
{
Console.WriteLine("vehicles has been driving");
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 202
R.N.REDDY
}
internal override void park()
{
Console.WriteLine("vehicle has not been parked");
}
string color;
internal override string Color
{
get
{
return color;
}
set
{
color = value;
}
}
internal void myfun()
{
Console.WriteLine("dc myfun is calling");
}
class Program
{
static void Main(string[] args)
{
vehicle obj1 = new dc();
obj1.drive();
obj1.park();
obj1.color = "red";
Console.WriteLine("vehicle color is:" + obj1.color);
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 203
R.N.REDDY
obj1.stop();
dc dcobj = new dc();
dcobj.myfun();
}
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 205
R.N.REDDY
Interface
It is a collection of abstract members.
To define an interface, we will use interface keyword
Syntax to define an interface
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 206
R.N.REDDY
Interface members, we have to implement within the derived class without using
override keyword
By default access modifier of interface members will be public.
An interface can contain properties and methods but which cannot contain
constructors and fields
Within interface, we cannot have a static member
We cannot create object for interface but we can create a reference variable for
interface.
Interface will support single as well as multiple inheritance
Note:
Interface name should start with capital ‘I’.
For ex:
Ibank
Icollege
Iemployee
namespace interfacexample
{
interface Imyinterface
{
void print();
int X
{
get;
set;
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 207
R.N.REDDY
}
class DC : Imyinterface
{
public void print()
{
Console.WriteLine("print is calling");
}
int x;
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
internal void show()
{
Console.WriteLine("dc show is calling");
}
}
class Program
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 208
R.N.REDDY
DC d = new DC();
d.show();
Console.ReadLine();
}
}
}
Output:
namespace interfacex2
{
interface Inokia1
{
void calling();
void receiving();
void endcall();
void sendmessage();
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 209
R.N.REDDY
class nokia1100 : Inokia1
{
public void calling()
{
Console.WriteLine("CALLING FROM NOKIA1100 MOBILE");
}
public void receiving()
{
Console.WriteLine("call recieved by 1100");
}
public void endcall()
{
Console.WriteLine("call ended by 1100");
}
public void sendmessage()
{
Console.WriteLine("message is sending from nokia1100");
}
}
interface Inokia2
{
void bluetooth();
void wifi();
}
class nokiaasha : Inokia1, Inokia2
{
public void calling()
{
Console.WriteLine("calling from asha");
}
public void receiving()
{
Console.WriteLine("receiving from asha");
}
public void endcall()
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 210
R.N.REDDY
{
Console.WriteLine("calling ended from asha");
}
public void sendmessage()
{
Console.WriteLine("message is sent from nokiaasha");
}
public void bluetooth()
{
Console.WriteLine("using bluetooth from nokiasha");
}
public void wifi()
{
Console.WriteLine("using wifi from nokiaasha");
}
public void videocalling()
{
Console.WriteLine("videocalling from nokiasha");
}
}
class Program
{
static void Main(string[] args)
{
Inokia1 objinokia1 = new nokia1100();
objinokia1.calling();
objinokia1.receiving();
objinokia1.endcall();
objinokia1.sendmessage();
objinokia1 = new nokiaasha();
objinokia1.calling();
objinokia1.receiving();
objinokia1.endcall();
objinokia1.sendmessage();
objinokia1 = null;
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 211
R.N.REDDY
Inokia2 objinokia2 = new nokiaasha();
objinokia2.bluetooth();
objinokia2.wifi();
objinokia2 = null;
nokiaasha objinokiaasha = new nokiaasha();
objinokiaasha.videocalling();
Console.ReadLine();
}
}
}
Output
Q) Can we inherit a combination of one class and two interfaces into another
class?
namespace interfaceex3
{
interface Inokia1
{
void calling();
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 212
R.N.REDDY
void receiving();
void endcall();
void sendmessage();
}
class nokia1100 : Inokia1
{
public void calling()
{
Console.WriteLine("CALLING FROM NOKIA1100 MOBILE");
}
public void receiving()
{
Console.WriteLine("call recieved by 1100");
}
public void endcall()
{
Console.WriteLine("call ended by 1100");
}
public void sendmessage()
{
Console.WriteLine("message is sending from nokia1100");
}
}
interface Inokia2
{
void bluetooth();
void wifi();
}
class myclass
{
internal void print()
{
Console.WriteLine("print is calling");
}
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 213
R.N.REDDY
class nokiaasha:myclass,Inokia1,Inokia2
{
public void calling()
{
Console.WriteLine("calling from asha");
}
public void receiving()
{
Console.WriteLine("receiving from asha");
}
public void endcall()
{
Console.WriteLine("calling ended from asha");
}
public void sendmessage()
{
Console.WriteLine("message is sent from nokiaasha");
}
public void bluetooth()
{
Console.WriteLine("using bluetooth from nokiasha");
}
public void wifi()
{
Console.WriteLine("using wifi from nokiaasha");
}
public void videocalling()
{
Console.WriteLine("videocalling from nokiasha");
}
}
class Program
{
static void Main(string[] args)
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 214
R.N.REDDY
{
Inokia1 objinokia1 = new nokia1100();
objinokia1.calling();
objinokia1.receiving();
objinokia1.endcall();
objinokia1.sendmessage();
objinokia1 = new nokiaasha();
objinokia1.calling();
objinokia1.receiving();
objinokia1.endcall();
objinokia1.sendmessage();
objinokia1 = null;
Inokia2 objinokia2 = new nokiaasha();
objinokia2.bluetooth();
objinokia2.wifi();
objinokia2 = null;
nokiaasha objinokiaasha = new nokiaasha();
objinokiaasha.videocalling();
objnokiaasha.print();
Console.ReadLine();
}
}
}
Whenever we want to inherit one class and multiple inheritances, which is
possible but we have to write the class name then we have to write interfaces
names like above program.
namespace interfaceex4
{
interface Iaccounts
{
void openaccount(int accno, string accname, double intbal);
}
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 215
R.N.REDDY
class currentaccount : Iaccounts
{
public void openaccount(int accno, string accname, double intbal)
{
double bal = intbal;
Console.WriteLine("your account is created and details are like
below:");
Console.WriteLine("your account number is :" + accno);
Console.WriteLine("your account name is :" + accname);
Console.WriteLine("your curret balance is:" + bal);
}
}
class savingaccount : Iaccounts
{
public void openaccount(int accno, string accname, double intbal)
{
double bal = intbal;
if (bal >= 1000)
{
Console.WriteLine("your account is sreated and details are:");
Console.WriteLine("your account number is:" + accno);
Console.WriteLine("your account name is :" + accname);
Console.WriteLine("your currenet balance is:" + bal);
}
else
{
Console.WriteLine("sorry We cannot create account with min
1000 balance");
}
}
}
class program
{
static void Main(string[] args)
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 216
R.N.REDDY
Iaccounts obj=new currentaccount();
obj.openaccount(111,"jhon",0)
obj=new savingaccount();
obj.openaccount(222,"david",1200);
Console.ReadLine();
}
}
}
Output:
Q) When we will go for abstract class and when we will go for interface class
A) Whenever we want to declare some members and implement some members then
we have to go for abstract class.
For example in the above abstract class program we have defined one method
called about information and we have to declare one method called pay bill due
to that reason which we have declared as abstract class
Whenever we want to declare all the members i.e. there is no definitions then
particular class we will go for interface.
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 217
R.N.REDDY
For example: in the above program i.e. bank example to declare one behavior
called open account due to that reason we have declared an interface called
accounts.
Q) What an abstract method is telling?
A) An abstract is telling what to do but not how to do. How to do is deciding by
derived class implemented methods.
Q) What an interface is telling?
A) An interface is a contract or agreement which is telling what to do but not how to
do. How to have to decide by derived class implementing methods.
Q) What is concrete class?
A) A class which is having full implementation is nothing but concrete class or
normal class or Non-Abstract class.
Concrete class is a collection of concrete methods or Non-abstract methods.
Q) What is a concrete method?
A) While defining a method if we didn’t use any keyword like abstract those
methods is called as Concrete method or Non-Abstract method.
Difference between abstract class and interface class
Abstract Interface
Abstract Interface
Abstract class is a collection of Interface is a collection of abstract
abstract members and non abstract members i.e. by default interface
members members are abstract members
Default access modifier of a abstract Default access modifier of a interface
class will be private class will be public
It is a partially implemented class No implementation
An abstract class can contain variable No
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 218
R.N.REDDY
Within the abstract class we can Within the interface we cannot
implement or define a property implement a property but we can
declare a property
It can contain constructor No
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 219
R.N.REDDY
Static No keyword
Have to access with the help of class With the help of object name
name
Memory is allocated at the time of At the time of object is creating
class is loading
Static variable will have the common Value will differ from one object to
value for all the objects another object
When is the class is unloading static When the object is destroyed
variable will destroy
Memory is allocated within the Memory is allocated within the heap
method area
We can’t save the memory because it
We can save the memory is creating multiple variables
This method will use to access static This method will use to access
variable instance and static variable
namespace program
{
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 220
R.N.REDDY
class bc
{
internal virtual void display()
{
Console.WriteLine("bc method is calling");
}
}
class dc : bc
{
internal override void display()
{
Console.WriteLine("dc method is calling");
}
}
class tc : dc
{
internal override void display()
{
Console.WriteLine("tc display is calling");
}
}
class Program
{
static void Main(string[] args)
{
dc d = new tc();
d.display();
Console.ReadLine();
}
}
}
Output:
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 221
R.N.REDDY
In the above program tc class will have two override display methods
1) Inheritance from dc class
2) Inheriting tc class method
Control will give property for current class override method
Advantages of oops:
1) Reusability
2) Extensibility
These two we can achieve with the help of inheritance
3) Reimplementation
This we can achieve with the help of polymorphism
4) Modularity
This we can achieve with the help of class
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 222
R.N.REDDY
5) Easy to modify
This we can achieve with the help of classes and methods
6) Easy to implement real world programming
7) Security : we can achieve with the help of access modifiers
II-Floor, Sri Sai Arcade, Beside: Adithya Trade Center, Ameerpet, Hyderabad-500038. Ph: 040-65538958/68/78. P a g e | 223