Object Oriented Programming Concepts in C#
Object Oriented Programming means set of rules and rules are converted to concepts or features. Object Oriented Programming is used for code suppretion. The code can be suppreted by creating classes and Object Oriented Programming is used to perform multitask. Multitask means getting more than one result at a time. In Object Oriented Programming, class is used to create new datatype or user defined datatype. Class contains variables and methods. Variables are used to store values and methods are used to Manipulate the values. By using a class, object can be created. Object contains state and behaviour. State is used to tell what the object is and behaviour is used to tell what the object can do.
If class is created with variables and methods then memory will not be preserved for them. Memory will be preserved only when object is created. When object is created using new operator then object will be stored in a heap memory. When object is stored in heap memory then it will contain the reference or memory address of the class. By using object, variables and methods in the class can be accessed with the object name by using dot(.) operator. This operator is called member access operator.
Access Specifiers in C#:
- Protected internal
The variables & methods declared as public can be accessed in other classes and can be accessed in main().
If the variables & methods declared as private then they can be accessed within the class. They cannot be accessed in other classes & cannot be accessed even in main().
For the vriables & methods if access specifier is not given then default access specifier is private.
Object Oriented Programming Concepts or Features:
Wrapping of variables & methods as a unit or else as a block is known as encapsulation. If a class is created with variables & methods then it is considered as a encapsulation.
Inheritance means reusability or reusing existing code. The variables and methods existing in one class can be used in other classes.
* Types Of Inheritance:
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
In C#, multiple inheritance is not possible with classes. Instead of classes, interfaces are used.
Polymorphism means, "One name, multiple results". Ploymorphism is of two types: Compile time polymorphism & Runtime polymorphism. Compile time polymorphism can be called as static polymorphism or 'Early binding'. Runtime polymorphism is called dynamic polymorphism or 'Late binding'. Function overloading and operator overloading are the examples of compile time polymorphism. Function overriding or virtual function are the examples of runtime polymorphism.
A class can contain multiple functions with the same name but there should be difference in number of arguments, order of arguments or difference in data types. It is called as compile time polymorphism i.e based on the given arguments or values while calling the function, compiler will decide at compilation time that which method in the class should execute.
1) Example of method overloading:
2) Example of method overloading with different datatype as arguments:
In inheritance, base class and derived class is used. Base class can be called as independent class or parent class. Derived class means, creating a class by using another class. This class can be called as dependent class or child class. If one base class is used in a another derived class then it is called single inheritance. If two interfaces are used in one class then it is called multiple inheritance. If derived class is used in another class then it is called multilevel inheritance.
1) Using base class in derived class:
Protected - It is an access specifier. The variables and methods declared as protected can be used only in derived class. They cannot be accessed in other classes and cannot be accessed even in main().
2) Using base class method in derived class:
3) Using one base class in two derived classes:
ii) Method overriding:
Methods in base class and derived class having the same signature, then method in derived class will overrides the method in base class. Same signature means, method name, number of arguments and order of arguments and datatypes will be same. Method overriding will work as runtime polymorphism i.e.according to the method address, compiler will decide runtime that which method in the class should execute.
4) Example of method overriding:
Base class method should contain virtual so that other functions can override base class function. Derived class method will contain override, so that it can override base class method. 'base' is a predefined object created for base class. To access base class methods ,it is used.
5) Using derived class in another class:
It is similar to a class but instance or object cannot be created with the interface. Interface will contain abstract methods (method without body or statements). The abstract method given in interface will be implemented or created in a class. That class is called implementation class. Interface is used to work with multiple inheritance and used to provide security for the methods in the class.
1) Using one interface in a class:
In Interface, access specifier will not be given. Default access specifier is public.
2) Using two interfaces in one class:
3) Using interface in another interface:
4) Using one interface in two classes:
If you want to use one particular value in the entire program or else in entire application then the value should be stored in the static variable. It is used to preserve memory for the variables and methods at the compilation time. If the class contains static variables and static methods then variables and methods can be accessed with class name.
1) Example of static variables and methods:
2) Example of static variables and class:
In this way, we have learned Object Oriented Programming concepts with examples in this article. I hope this will help beginners to understand OOPs concepts.
You may also be interested in...
- Slack Integration with C#
- Xamarin Forms: Getting Started
- Part 1: Introduction to C#
- Part 2: Arrays and Function in C#
- Part 3: OOPs Concepts in C#
- Part 4: Constructor/Destructor in C#
- Part 5: Abstract class in C#
- Part 6: Indexer,Delegates, Anonymous Method, Lambda expression in C#
- Part 7: Collections in C#
- Part 8: Generics in C#
- Part 9: Attributes in C#
- Part 10: Sealed/Partial class in C#
- Part 11: MultiThreading in C#