Anda di halaman 1dari 7

OOPS

 What is object oriented programming approach?


Object oriented programming approach is a methodology to implement
computer programs by using class and object.
 A programming language which will support following principle’s
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
then particular programming language will be called as object oriented
programming language.
 The above four principles are called s object oriented programming principle’s.
Examples for object oriented programming language: C++, Java, C#.Net………
 To achieve the above object oriented programming principles every programming
language is using two concepts: 1)Class
2) Object
 Class:
 It is a collection of data members and member function
 To define a class we have to use “class” keyword
Syntax to define a class:
<Access modifier> class <class name>
{
// data members /variables/state
// member function /method/behavior
}
 Class is a logical representation, no memory will allocate for the class
members at the time of defining the class.
 Class is a collection of states and variables.
 State can be called as variable or data member.
 State will be representing some value.
 Behavior can be called as method or member function.
 State/variable:
State/variable is representing a value.
Q) When we will declare state?
A) Whenever we want to represent a value we will declare that member as state.
Eg: - empno, emp name, salary and so…………………..on.
 In C#.Net variables can be classified into three types
1) Local variable
2) Instance variable/Non static variable
3) Static variable
1) Local variable: - A variable which is declared with in a method or function can
be called as Local variable.
 Local variable we can access only within that method or function where we
have declared.
 Local variable should be initialized with some value before accessing
otherwise compiler will generate error.
2) Instance variable:-
 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
}

 According to the parameters methods are classified into two types


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)
{
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)
{
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:
 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.
 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.

Anda mungkin juga menyukai