Oops is abbriviated as object oriented programming systemin which programs are considered as a collection of objects.Each object is nothing but an instace of a class.
Oops have following features :
Object is the basic unit of object-oriented programme.Objects are identified by its unique name.An object represents a particular instance of a class.There can be more than one instanceof an object.Each instance of an object can hold its own relevant data.An object is a collection of data members and associated member functions also known as methods.
Class:
It represents the characteristics and functionality of an object.It is a blueprint of an object.
E.g:-
if class = cars .
then objects are Ford, alto, nano.
properties or members are speed, engine power, colour, year, model.
The methods or functions are start,move,stop.
No memory is allocated when a class is created.
Abstraction:
Abstraction means showing essential features and hiding non-essential features to the user.
E.g:-Yahoo Mail
When you provide the username and password and click on submit button . It will show compose, Inbox, Outbox, Sent Mails . . so and so when you click on compose it will open . . . but user doesn't know what are the actions performed internally.
Encapsulation:
Encapsulation means which binds the data and code (or) writing operations and methods in single unit (class).
E.g:-
A car having multiple parts . . .like steering,wheels,engine . . . etc . . .which binds together to form a single object that is car . So,Here multiple parts of cars encapsulates itself together to form a single object that is car.
Encapsulation is used for security purpose.
Encapsulation = Abstraction + Data Hiding
Inheritance:
Deriving a new class from the existing class , is called Inheritance . Derived class is getting all the features from existing class and also incorporating some new features to the sub-class.
e.g:-
Class Address
{
String name ;
String street ;
}
class latestAddress extends Address
{
string city ;
string state ;
string country ;
}
public class vishal
{
latestAddress la=new latestAddress ( ) ;
}
In the above example class latestAddress getting all features from the Address class . In the latestAddress class we have total 5 properties . 2 are inherited from Address and 3 properties are incorporated .
Polymorphism:
Polymorphism means ability to take more than one from that an operation can exhibit different behaviour at different instance depend upon the data passed in the operation .
E.g:-
We behave differently in front of elders , and friends . A single person is behaving differently at different time.
Consider the stadium of common wealth games . Single stadium but it perform multiple task like swimming,lawn tennis etc.
Virtual Function :
A function defined in the BASE class is redefined in the Derived class . A virtual function or virtual method is an inheritable and overridable function or method for which dynamic dispatch is facilitated .
E.g :-
A base class 'Animal' could have a virtual function 'Eat' . Subclass 'Liama' would implement 'eat( )'differently than subclass wolf , but one can invoke 'eat( )'on any class instance reffered to as Animal , and get the 'eat( )' behaviour of the specific subclass .
Partial class :
The biggest use of partial classes is that using partial classes.Multiple programmes can work on the same class easily . Partial classes are mainly used used by code generator because the code can be added to the class without having to recreate the source file . Moreover , it's easier to categorize code with partial classes .
The partial keyword indicates that other parts of the class can be defined in the same namespace .
Partial classes allow for a single class file to be split up accross multiple physical files , and all parts are combined when the application is compiled .
Ex :-
public class Billing
{
public bool Add ( )
{
}
public bool Edit ( )
{
}
}
public partial class Billing
{
public bool Add ( )
{
}
}
A class defined in two or more files is called a partial class .
Sealed class :
A sealed class is a class that can't be inherited . Sealed classes are used to restrict the inheritance feature of object oriented programming . It has no child class .The main purpose of using a sealed class is to take away the inheritance feature from the user so that they can't derive a class from a sealed class.
Syntax :-
sealed class <clay_name>
{
// Members
}
Inheritance :
A key feature of Oops is reusability . It's always time saving and useful if we can reuse something that already exists rather than trying to create something again and again .
Inheritance is of 4 types :
Single inheritance - which contains one base class and one derived class .
Hierarchical Inheritance - Which contains one base class and Multiple derived classes of the same base class .
Multilevel Inheritance - Which is derived from a derived class.
Multiple Inheritance - Which contains several base classes and a derived class .
N.B :-
C#.Net supports single hierarchical ,and Multilevel Inheritance . It doesn't support Multiple Inheritance directly because Multiple Inheritance supports Multiple base classes and in C# , the classes can't be inherited from more than one base class . You can implement Multiple Inheritances in C# through interfaces.
Polymorphism :
It means "no entity , containing Multiple forms " . It is also called as overloading which means the use of same thing for different purposes . Using polymorphism we can create as many functions . We want to create one function name but with different argument list .
There are two ways to implement polymorphism :
Compile time polymophism - Early binding
Run time polymorphism - Late binding
compile time polymorphism :-
When the compiler compiles a program , the compiler has the information about the method arguments . Accordingly , the compiler binds the appropriate method to the respective object at the compile time itself .This process is called compile time polymorphism or early binding .
This polymorphism is categorized into 2 types :
Method overloading :-It is a concept in which method names are same but with different signature i.e.with different arguments.
e.g. :- public int add (int i)
public int add (int i , int j)
public int add (int i , int j ,int k)
Operator loading :- The mechanism of assigning a special meaning to an operator , according to user defined data types such as classes , is known as operator overloading .
e.g :-
class operator
{
private int no1 ;
private int no2 ;
public operator (int num1 , int num2)
{
no1 = num1 ;
no2 = num2 ;
}
public static operover operator - (operover opr)
{
operator obj = new operator (20 , 40)
obj.no1 = -opr.no1 ;
obj.no2 = -opr.no2 ;
return obj ;
}
}
Run Time Polymorphism :-
It is better known as overriding . It allows a derived class to define a specific implementation for a method , other than the implementation defined by it's base class . This feature is known as run time polymorphism because the compiler binds the method to an object while the program is being compiled .
Overriding :-
It is one of the important property of inheritance is that a reference of base class can reffered to the object of any overridden method of any derived class can be invoked during runtime by loading the object to the base class reference and thereby dynamic polymorphism can achieved .
You use the 'virtual' keyword in a method to indicate that you want to have a base method overridden in a derived class or use 'override' keyword in derived class .
Syntax :-
class imp
{
private int age ;
public virtual void setage(int age1)
{
age = age1 ;
}
}
class imp1 : imp
{
public override void setage(int age)
{
base.setage(age)
}
}
Abstract Classes :-
{
public int addition (int no1 , int no2)
{
int no3 = no1+no2 ;
return no3 ;
}
public abstract void paid1( ) ;
}
class abstderived : abst
{
public override void prd1 ( )
{
//stmt
}
}
class test
{
static void main (string[ ]args)
{
abstderived obj = new abstderived ( ) ;
obj. prd1( ) ; // The abstract method will implement
int res = obj.addition (23 , 45) ;
}
}
Interfaces :-
{
//abstract method declaration
}
Namespace :-
A namespace is a kind of wrapper or area around one or more structural elements that make the elements unique .
It organize the bridge code projects .
The '.' operator delimits it .
It is of 2 categories :
1)user defined namespaces .
2)System defined namespaces .
The ' using ' directive is used to tell the compiler which namespaces you want to use in the program . Namespaces are always public , therefore the declaration of namespace can't include any access modifiers .
Constructor :-
A constructor is a special member function whose task is to initialize the object of it's class .
This is the first method that is run when an instance of a type is created .
Constructors can never return a value , and can be overridden to provide custom initialization functionality .
It is of 3 types :
1) Default Constuctor
2)Parametric Constructor
3)Copy Constructor
Syntax :-
public <classname> (parameterlist)
{
//body
}
Destructor :-
{
//body
}
Difference between new and override :-
The new modifier instructs the compiler to use the new implementation instead of the base class function . whereas , override modifier helps to override the base class function .
Methods :-
A method in C# is a procedure built into the class . They are the block of codes that contain a series of statements thats are executed when called .
There are 2 types of method :-
1)Procedures - Don't return a value .
2)Functions - Return a value .
Array :-
An array is an entity which contains multiple variables of same datatype and with a single name .
Syntax :-
<Datatype>[ ]<array_name> = new type[size] ;
string[ ]Months = new string [12] ;
Multidimensional Array :
type [ , ] <array_name> = new type [<upper>,<upper2>] ;
int [ , ] num = new int [ 2,2 ] ;
Enumeration :-
An enumeration provides a way for attaching the integer type numbers to the names in a program , which in turn increases the readability of a code .
Enumerations are user-defined integer data type .
Syntax :-
enum < enum_name >
{
< value1>
< value2>
}
public enum color
{
red=1 , green , yellow , blue
}
Structure :-
Structures can be defined as a tool for handling a group of logically related data items .
They are user-defined and provide a method for packaging of data together of different types .
Syntax :-
struct <structure_name >
{
<members> ;
}
Value Types and Reference Types :-
Value types and reference types belong to application data memory .
Value Types :-
Manipulator are the functions which can be used in conjunction with the insertion (<<) and extraction (>>) operators on an object . Examples are endl and setw.
Virtual Function :-
Virtual function is a member function of class and it's functionality can be overridden in it's derived class . This function can be implemented by using a keyword called virtual . It declared in base class .
Static Method :-
Call the base method without creating an instance .
E.g :-
public static void method ( )
{
- - -
}
Call that static method :-
Classname.method ( ) ;
Here we don't create an object .
Early and Late Binding :-
'this' pointer :- 'this' pointer refers to the current object of a class .
Default access modifier in a class - Private
Static and Dynamic Binding :-
Binding is nothing but the association of a name with the class .
Static binding is a binding in which name can be associated with the class during compilation time and it is also called as early binding .
Dynamic Binding is a binding in which name can be associated with class during execution time and it is also called as Late Binding .
Oops have following features :
- Object - Instance of class
- Class - Blueprint of object
- Encapsulation - Protecting our data
- Polymorphsm - Different behaviors at different instances .
- Abstraction - Hiding our irrelevant data .
- Inheritnce - One property of object is acquiring to another property of object .
Object:
Object is the basic unit of object-oriented programme.Objects are identified by its unique name.An object represents a particular instance of a class.There can be more than one instanceof an object.Each instance of an object can hold its own relevant data.An object is a collection of data members and associated member functions also known as methods.
Class:
It represents the characteristics and functionality of an object.It is a blueprint of an object.
E.g:-
if class = cars .
then objects are Ford, alto, nano.
properties or members are speed, engine power, colour, year, model.
The methods or functions are start,move,stop.
No memory is allocated when a class is created.
Abstraction:
Abstraction means showing essential features and hiding non-essential features to the user.
E.g:-Yahoo Mail
When you provide the username and password and click on submit button . It will show compose, Inbox, Outbox, Sent Mails . . so and so when you click on compose it will open . . . but user doesn't know what are the actions performed internally.
Encapsulation:
Encapsulation means which binds the data and code (or) writing operations and methods in single unit (class).
E.g:-
A car having multiple parts . . .like steering,wheels,engine . . . etc . . .which binds together to form a single object that is car . So,Here multiple parts of cars encapsulates itself together to form a single object that is car.
Encapsulation is used for security purpose.
Encapsulation = Abstraction + Data Hiding
Inheritance:
Deriving a new class from the existing class , is called Inheritance . Derived class is getting all the features from existing class and also incorporating some new features to the sub-class.
e.g:-
Class Address
{
String name ;
String street ;
}
class latestAddress extends Address
{
string city ;
string state ;
string country ;
}
public class vishal
{
latestAddress la=new latestAddress ( ) ;
}
In the above example class latestAddress getting all features from the Address class . In the latestAddress class we have total 5 properties . 2 are inherited from Address and 3 properties are incorporated .
Polymorphism:
Polymorphism means ability to take more than one from that an operation can exhibit different behaviour at different instance depend upon the data passed in the operation .
E.g:-
We behave differently in front of elders , and friends . A single person is behaving differently at different time.
Consider the stadium of common wealth games . Single stadium but it perform multiple task like swimming,lawn tennis etc.
Virtual Function :
A function defined in the BASE class is redefined in the Derived class . A virtual function or virtual method is an inheritable and overridable function or method for which dynamic dispatch is facilitated .
E.g :-
A base class 'Animal' could have a virtual function 'Eat' . Subclass 'Liama' would implement 'eat( )'differently than subclass wolf , but one can invoke 'eat( )'on any class instance reffered to as Animal , and get the 'eat( )' behaviour of the specific subclass .
Partial class :
The biggest use of partial classes is that using partial classes.Multiple programmes can work on the same class easily . Partial classes are mainly used used by code generator because the code can be added to the class without having to recreate the source file . Moreover , it's easier to categorize code with partial classes .
The partial keyword indicates that other parts of the class can be defined in the same namespace .
Partial classes allow for a single class file to be split up accross multiple physical files , and all parts are combined when the application is compiled .
Ex :-
public class Billing
{
public bool Add ( )
{
}
public bool Edit ( )
{
}
}
public partial class Billing
{
public bool Add ( )
{
}
}
A class defined in two or more files is called a partial class .
Sealed class :
A sealed class is a class that can't be inherited . Sealed classes are used to restrict the inheritance feature of object oriented programming . It has no child class .The main purpose of using a sealed class is to take away the inheritance feature from the user so that they can't derive a class from a sealed class.
Syntax :-
sealed class <clay_name>
{
// Members
}
Inheritance :
A key feature of Oops is reusability . It's always time saving and useful if we can reuse something that already exists rather than trying to create something again and again .
Inheritance is of 4 types :
Single inheritance - which contains one base class and one derived class .
Hierarchical Inheritance - Which contains one base class and Multiple derived classes of the same base class .
Multilevel Inheritance - Which is derived from a derived class.
Multiple Inheritance - Which contains several base classes and a derived class .
N.B :-
C#.Net supports single hierarchical ,and Multilevel Inheritance . It doesn't support Multiple Inheritance directly because Multiple Inheritance supports Multiple base classes and in C# , the classes can't be inherited from more than one base class . You can implement Multiple Inheritances in C# through interfaces.
Polymorphism :
It means "no entity , containing Multiple forms " . It is also called as overloading which means the use of same thing for different purposes . Using polymorphism we can create as many functions . We want to create one function name but with different argument list .
There are two ways to implement polymorphism :
Compile time polymophism - Early binding
Run time polymorphism - Late binding
compile time polymorphism :-
When the compiler compiles a program , the compiler has the information about the method arguments . Accordingly , the compiler binds the appropriate method to the respective object at the compile time itself .This process is called compile time polymorphism or early binding .
This polymorphism is categorized into 2 types :
- Method overloading
- Operator overloading
Method overloading :-It is a concept in which method names are same but with different signature i.e.with different arguments.
e.g. :- public int add (int i)
public int add (int i , int j)
public int add (int i , int j ,int k)
Operator loading :- The mechanism of assigning a special meaning to an operator , according to user defined data types such as classes , is known as operator overloading .
e.g :-
class operator
{
private int no1 ;
private int no2 ;
public operator (int num1 , int num2)
{
no1 = num1 ;
no2 = num2 ;
}
public static operover operator - (operover opr)
{
operator obj = new operator (20 , 40)
obj.no1 = -opr.no1 ;
obj.no2 = -opr.no2 ;
return obj ;
}
}
Run Time Polymorphism :-
It is better known as overriding . It allows a derived class to define a specific implementation for a method , other than the implementation defined by it's base class . This feature is known as run time polymorphism because the compiler binds the method to an object while the program is being compiled .
Overriding :-
It is one of the important property of inheritance is that a reference of base class can reffered to the object of any overridden method of any derived class can be invoked during runtime by loading the object to the base class reference and thereby dynamic polymorphism can achieved .
You use the 'virtual' keyword in a method to indicate that you want to have a base method overridden in a derived class or use 'override' keyword in derived class .
Syntax :-
class imp
{
private int age ;
public virtual void setage(int age1)
{
age = age1 ;
}
}
class imp1 : imp
{
public override void setage(int age)
{
base.setage(age)
}
}
Abstract Classes :-
- This is not used to create objects .
- This is designed to act as a base class .
- This is similar to interfaces .
Syntax :-
abstract class abst{
public int addition (int no1 , int no2)
{
int no3 = no1+no2 ;
return no3 ;
}
public abstract void paid1( ) ;
}
class abstderived : abst
{
public override void prd1 ( )
{
//stmt
}
}
class test
{
static void main (string[ ]args)
{
abstderived obj = new abstderived ( ) ;
obj. prd1( ) ; // The abstract method will implement
int res = obj.addition (23 , 45) ;
}
}
Interfaces :-
- It is similar to abstract class in terms of logic .
- Using interfaces we can achieve multiple inheritance .
- Once you create an interface you need to implement all the methods specified in that interface .
- It is a template or collection of abtract data members such as methods , events , and properties .
Syntax :-
Interface < interface _ name >{
//abstract method declaration
}
Namespace :-
A namespace is a kind of wrapper or area around one or more structural elements that make the elements unique .
It organize the bridge code projects .
The '.' operator delimits it .
It is of 2 categories :
1)user defined namespaces .
2)System defined namespaces .
The ' using ' directive is used to tell the compiler which namespaces you want to use in the program . Namespaces are always public , therefore the declaration of namespace can't include any access modifiers .
Constructor :-
A constructor is a special member function whose task is to initialize the object of it's class .
This is the first method that is run when an instance of a type is created .
Constructors can never return a value , and can be overridden to provide custom initialization functionality .
It is of 3 types :
1) Default Constuctor
2)Parametric Constructor
3)Copy Constructor
Syntax :-
public <classname> (parameterlist)
{
//body
}
Destructor :-
- It is also known as finalizer , is the last method run by a class .
- With in a destructor we can place code to clean up the object after it is used ,which might include decrementing counters or releasing resources .
- It automatically called when the .Net runtime determines that the object is no longer required .
- For destructor , we use tilde(~) symbol .
Syntax :-
~ <class name> ( ) {
//body
}
Difference between new and override :-
The new modifier instructs the compiler to use the new implementation instead of the base class function . whereas , override modifier helps to override the base class function .
Methods :-
A method in C# is a procedure built into the class . They are the block of codes that contain a series of statements thats are executed when called .
There are 2 types of method :-
1)Procedures - Don't return a value .
2)Functions - Return a value .
Array :-
An array is an entity which contains multiple variables of same datatype and with a single name .
Syntax :-
<Datatype>[ ]<array_name> = new type[size] ;
string[ ]Months = new string [12] ;
Multidimensional Array :
type [ , ] <array_name> = new type [<upper>,<upper2>] ;
int [ , ] num = new int [ 2,2 ] ;
Enumeration :-
An enumeration provides a way for attaching the integer type numbers to the names in a program , which in turn increases the readability of a code .
Enumerations are user-defined integer data type .
Syntax :-
enum < enum_name >
{
< value1>
< value2>
}
public enum color
{
red=1 , green , yellow , blue
}
Structure :-
Structures can be defined as a tool for handling a group of logically related data items .
They are user-defined and provide a method for packaging of data together of different types .
Syntax :-
struct <structure_name >
{
<members> ;
}
Value Types and Reference Types :-
Value types and reference types belong to application data memory .
Value Types :-
- Value types are hold the data directly .
- Null can't be stored in value types .
- It will be stored in stack memory at compile time .
- Value types are not accessible to garbage collectior .
- Ex:-Predefined datatypes , structure , enum etc.
- When a variable of value types goes out of scope , it is destroyed and it's memory is reclaimed .
Reference Types :-
- Reference types can't hold the data directly .
- Null can be stored .
- It will be stored in head memory at runtime .
- E.g :- classes , objects , Interfaces , Delegates , Arrays etc.
- A variable of reference type exists in two memory locations and that's why when that variable goes out of scope , the reference to that object is destroyed but the object itself is not destroyed.
Manipulator :-
Virtual Function :-
Virtual function is a member function of class and it's functionality can be overridden in it's derived class . This function can be implemented by using a keyword called virtual . It declared in base class .
Static Method :-
Call the base method without creating an instance .
E.g :-
public static void method ( )
{
- - -
}
Call that static method :-
Classname.method ( ) ;
Here we don't create an object .
Early and Late Binding :-
- Early binding refers to assignment of values to variables during design time .
- Late binding refers to assignment of values to variables during run time .
'this' pointer :- 'this' pointer refers to the current object of a class .
Default access modifier in a class - Private
Static and Dynamic Binding :-
Binding is nothing but the association of a name with the class .
Static binding is a binding in which name can be associated with the class during compilation time and it is also called as early binding .
Dynamic Binding is a binding in which name can be associated with class during execution time and it is also called as Late Binding .
0 Comments