Reference from : https://www.aspdotnet-suresh.com/2010/04/introduction-to-object-oriented.html?m=1
OOPS Concepts
Class:
It is a collection of objects.
Object:
It is a real time entity.
An object can be considered a “thing” that can perform a set of related activities. The set of activities that the object performs defines the object’s behavior. For example, the hand can grip something or a Student (object) can give the name or address. In pure OOP terms an object is an instance of a class
The above template describe about object Student
Class is composed of three things name, attributes, and operations
public class student
{
}
student objstudent=new student ();
According to the above sample we can say that Student object, named objstudent, has created out of the student class.
In real world you will often find many individual objects all of the same kind. As an example, there may be thousands of other bicycles in existence, all of the same make and model. Each bicycle has built from the same blueprint. In object-oriented terms, we say that the bicycle is an instance of the class of objects known as bicycles. In the software world, though you may not have realized it, you have already used classes. For example, the Textbox control, you always used, is made out of the Textbox class, which defines its appearance and capabilities. Each time you drag a Textbox control, you are actually creating a new instance of the Textbox class.
Encapsulation:Encapsulation is a process of binding the data members and member functions into a single unit.Example for encapsulation is class. A class can contain data structures and methods.Consider the following classpublic class Aperture{public Aperture (){}protected double height;protected double width;protected double thickness;public double get volume(){Double volume=height * width * thickness;if (volume<0)return 0;return volume;}}
In this example we encapsulate some data such as height, width, thickness and method Get Volume. Other methods or objects can interact with this object through methods that have public access modifier.
To know more about Encapsulation check this Encapsulation in C# with Examples.Abstraction:Abstraction is a process of hiding the implementation details and displaying the essential features.Example1: A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, usb ports, battery, speakers etc. To use it, you don’t need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works. You just need to know how to operate the laptop by switching it on. Think about if you would have to call to the engineer who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone.So here the Laptop is an object that is designed to hide its complexity.
How to abstract: – By using Access Specifiers
To know more about abstraction check this Abstraction in c# with Examples..Net has five access SpecifiersPublic — Accessible outside the class through object reference.Private — Accessible inside the class only through member functions.Protected — Just like private but Accessible in derived classes also through member functions.Internal — Visible inside the assembly. Accessible through objects.Protected Internal — Visible inside the assembly through objects and in derived classes outside the assembly through member functions.Let’s try to understand by a practical example:-public class Class1 { int i; //No Access specifier means private public int j; // Public protected int k; //Protected data internal int m; // Internal means visible inside assembly protected internal int n; //inside assembly as well as to derived classes outside assembly static int x; // This is also private public static int y; //Static means shared across objects [DllImport(“MyDll.dll”)] public static extern int MyFoo(); //extern means declared in this assembly defined in some other assembly public void myFoo2() { //Within a class if you create an object of same class then you can access all data members through object reference even private data too Class1 obj = new Class1(); obj.i =10; //Error can’t access private data through object.But here it is accessible.:) obj.j =10; obj.k=10; obj.m=10; obj.n=10; // obj.s =10; //Errror Static data can be accessed by class names only Class1.x = 10; // obj.y = 10; //Errror Static data can be accessed by class names only Class1.y = 10; } }Now lets try to copy the same code inside Main method and try to compile[STAThread] static void Main() { //Access specifiers comes into picture only when you create object of class outside the class Class1 obj = new Class1(); // obj.i =10; //Error can’t access private data through object. obj.j =10; // obj.k=10; //Error can’t access protected data through object. obj.m=10; obj.n=10; // obj.s =10; //Errror Static data can be accessed by class names only Class1.x = 10; //Error can’t access private data outside class // obj.y = 10; //Errror Static data can be accessed by class names only Class1.y = 10; }What if Main is inside another assembly[STAThread] static void Main() { //Access specifiers comes into picture only when you create object of class outside the class Class1 obj = new Class1(); // obj.i =10; //Error can’t access private data through object. obj.j =10; // obj.k=10; //Error can’t access protected data through object. // obj.m=10; // Error can’t access internal data outside assembly // obj.n=10; // Error can’t access internal data outside assembly // obj.s =10; //Errror Static data can be accessed by class names only Class1.x = 10; //Error can’t access private data outside class // obj.y = 10; //Errror Static data can be accessed by class names only Class1.y = 10; }In object-oriented software, complexity is managed by using abstraction.
Abstraction is a process that involves identifying the critical behavior of an object and eliminating irrelevant and complex details.
Inheritance:Inheritance is a process of deriving the new class from already existing classC# is a complete object oriented programming language. Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective use of inheritance, you can save lot of time in your programming and also reduce errors, which in turn will increase the quality of work and productivity. A simple example to understand inheritance in C#.Using System;Public class BaseClass{ Public BaseClass () { Console.WriteLine (“Base Class Constructor executed”); } Public void Write () { Console.WriteLine (“Write method in Base Class executed”); }}Public class ChildClass: BaseClass{ Public ChildClass () { Console.WriteLine(“Child Class Constructor executed”); } Public static void Main () { ChildClass CC = new ChildClass (); CC.Write (); }}
Polymorphism:When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.Polymorphism is one of the fundamental concepts of OOP.Polymorphism provides following features:
- It allows you to invoke methods of derived class through base class reference during runtime.
- It has the ability for classes to provide different implementations of methods that are called through the same name.
Polymorphism is of two types:
- Compile time polymorphism/Overloading
- Runtime polymorphism/Overriding
Compile Time PolymorphismCompile time polymorphism is method and operators overloading. It is also called early binding.In method overloading method performs the different task at the different input parameters.Runtime Time PolymorphismRuntime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.Caution: Don’t confused method overloading with method overriding, they are different, unrelated concepts. But they sound similar.Method overloading has nothing to do with inheritance or virtual methods.Following are examples of methods having different overloads:void area(int side);void area(int l, int b);void area(float radius);Practical example of Method Overloading (Compile Time Polymorphism)using System;
namespace method_overloading{ class Program { public class Print { public void display(string name) { Console.WriteLine (“Your name is : ” + name); } public void display(int age, float marks) { Console.WriteLine (“Your age is : ” + age); Console.WriteLine (“Your marks are :” + marks); } } static void Main(string[] args) { Print obj = new Print (); obj.display (“George”); obj.display (34, 76.50f); Console.ReadLine (); } }}
