OOP stands for Object Oriented Programming. It is a programming methodology that uses Objects to build a system or web applications using programming languages like C#, Vb.net etc.
They have at most these many concepts :
1] Class
2] Method
3] Object
4] Encapsulation
5] Abstraction
6] Information Hiding
7] Inheritance
8] Polymorphism
9] Constructors
10] Destructors
1) Class :
A class is a collection of objects and represents description of objects that share same attributes and actions. It contains characteristics of the objects such as objects attributes, actions or behaviors.
here is the syntax :
public class Bike
{
//your code goes here..
}
2) Methods
Method is an object’s behavior.
For example, if you consider “Bike” as a class then its behavior is to get bike color, engine, mileage etc. Here is the example of Method:
here is the Code :
public class Bike
{
//here is some properties of class Bike
public string color;
public string engine;
public int mileage;
//here is some behavior/methods of class Bike
public string GetColor()
{
return "red";
}
public int GetMileage()
{
return 65;
}
}
3) Objects
An object is a real-world entity that keeps together property states and behaviors.
For example, A “Bike” usually has common elements such as bike color, engine, mileage etc. In OOP terminology these would be called as a Class Properties or Attributes of a Bike object. Here is the example of Object:
4) Encapsulation
Encapsulation is the process of keeping or enclosing one or more items within a single physical or logical package. In object oriented programming methodology it prevents access to implementation details.
Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. Available access specifiers are public, private, protected, internal etc.
Here is the code:
public class Bike
{
public int mileage = 65;
public string color = "Black";
private string formula = "a*b";
//Its public – so accessible outside class
public int GetMileage()
{
return mileage;
}
//Its public – so accessible outside class
public string GetColor()
{
return color;
}
//Its private – so not accessible outside class
private string GetEngineMakeFormula()
{
return formula;
}
}
public class Program
{
public static void Main(string[] args)
{
Bike objBike = new Bike();
Console.WriteLine("Bike mileage is : " + objBike.GetMileage()); //accessible outside "Bike"
Console.WriteLine("Bike color is : " + objBike.GetColor()); //accessible outside "Bike"
//we can't call this method as it is inaccessible outside "Bike"
//objBike.GetEngineMakeFormula(); //commented because we can't access it
Console.Read();
}
}
5) Abstraction:
Abstraction is the process of providing only essential information to the outside real world and hiding overall background details to present an object. It relies on the separation of interface and implementation.
For example, we continue with “Bike” as an example, we have no access to the piston directly, we can use start button to run the piston. Just imagine if a bike manufacturer allows direct access to piston, it would be very difficult to control actions on the piston. That’s the reason why a bike provider separates its internal implementation from its external interface.
Here is the Code :
public class Bike
{
public int mileage = 65;
public string color = "Black";
private string formula = "a*b";
//Its public – so accessible outside class
public int GetMileage()
{
return mileage;
}
//Its public – so accessible outside class
public string GetColor()
{
return color;
}
//Its private – so not accessible outside class
private string GetEngineMakeFormula()
{
return formula;
}
//Its public – so accessible outside class
public string DisplayMakeFormula()
{
//"GetEngineMakeFormula()" is private but accessible and limited to this class only
return GetEngineMakeFormula();
}
}
public class Program
{
public static void Main(string[] args)
{
Bike objBike = new Bike();
Console.WriteLine("Bike mileage is : " + objBike.GetMileage()); //accessible outside "Bike"
Console.WriteLine("Bike color is : " + objBike.GetColor()); //accessible outside "Bike"
//we can't call this method as it is inaccessible outside "Bike"
//objBike.GetEngineMakeFormula(); //commented because we can't access it
Console.WriteLine("Bike color is : " + objBike.DisplayMakeFormula()); //accessible outside
Console.Read();
}
}
6] Information Hiding:
Information Hiding concept restricts direct exposure of the data. Data is accessed indirectly using safe mechanism, methods in case of programming object. Follow the example given in Abstraction.
7] Inheritance:
Inheritance in OOP allows us to create a new class using an existing one meaning extending one class to another.
This concept can also be related with the real world example. Let’s take a Bike example again. A Bike manufacturer uses same mechanism of existing version of the bike while launching a new version with some additional added functionality. This allows manufacturer to save their time and efforts both.
The main advantage of extending classes is that it provides a convenient way to reuse existing fully tested code in different context thereby saving lots of time with existing coding and its model style
Nanhe SiddiqueRank #1,103 · 1.7K reputationPosted May 29, 2015, 8:22 PM EDT
Nilesh JadavRank #109 · 20.3K reputationPosted May 29, 2015, 10:14 PM EDT
They have at most these many concepts :
For Constructor you can check out this link : Simple Constructor Program with Explanation
Simple Inheritance Program with Explanation
Hope this will work for you and if you satisfy with the answer than make it as accepted
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts