In this article, I am going to explore c#, an OOP language, in order to showcase and understand OOP concepts.
Introduction: -
C# is an object oriented programming language which is used to develope the various types of application like windows application , web application and android application.
C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg.
To understand more about C# concepts please visit here.
Lets have a look with simple c# program -
using System;
namespace Console_Application
{
class Intro
{
public static void Main(string [] args)
{
//this is comment.
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
Class:-
A class is a blue print of an object that contains variables for storing data and functions to perform operation on the data.
Class is a reference type variable which consist of data and behavior. Class data is represented to its fields and behavior is represented by its method.
Lets have a look with a simple examle.
public class Human
{
//Class fields
string name = "Javed";
int age = 22;
//Class method
public void printDetails()
{
Console.WriteLine("Name={0},Id={1}", name , age);
}
}
Object:- Object are the basic run-time entities of an object oriented system.They may represent a person,a place or any item.We can say we are the object of Human class.
Example- Here I'm going to create object of 'Human' class which I have created first and invoked the method of it.Which represent the Human details further.
Human mm = new Human();
mm.printDetails();
Inheritance:- When a class use the properties of another class it is known as Inheritance.
Example-Suppose there is class of 'HumanBeing' which contains two common properties like Ear & Mouth. Also there is some action define such as can speak and can hear.
public class HumanBeing
{
public bool mouth = true;
public bool ear = true;
public void canSpeak()
{
Console.WriteLine("Yes! can speak.");
}
public void canHear()
{
Console.WriteLine("Yes! can hear.");
}
}
Lets create two another class of 'Man' & 'Women' which is inherit from 'HumanBeing'.
Which uses its common properties further.
public class Man : HumanBeing
{
public void ManProperties()
{
canSpeak();
canHear();
}
}
public class Women : HumanBeing
{
public void WomenProperties()
{
canSpeak();
canHear();
}
}
So,the advantage of Inheritance is that we can reuse the code functionality by defining the common properties at one place.
Lets have a look with simple c# program -
using System;
namespace Console_Application
{
class Intro
{
public static void Main(string [] args)
{
//this is comment.
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
OOP Concept:- OOP stand for Object Oriented Programming.
In the class-based object-oriented programming paradigm, “object” refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures. A good understanding of OOPs concepts can help in decision making when designing an application. How you should design an application and what language should be used.Object Oriented programming is a programming style which is associated with the concepts like class,object, Inheritance,
Encapsulation ,Abstraction, Polymorphism.
Encapsulation ,Abstraction, Polymorphism.
A class is a blue print of an object that contains variables for storing data and functions to perform operation on the data.
Class is a reference type variable which consist of data and behavior. Class data is represented to its fields and behavior is represented by its method.
Lets have a look with a simple examle.
public class Human
{
//Class fields
string name = "Javed";
int age = 22;
//Class method
public void printDetails()
{
Console.WriteLine("Name={0},Id={1}", name , age);
}
}
Object:- Object are the basic run-time entities of an object oriented system.They may represent a person,a place or any item.We can say we are the object of Human class.
Example- Here I'm going to create object of 'Human' class which I have created first and invoked the method of it.Which represent the Human details further.
Human mm = new Human();
mm.printDetails();
Inheritance:- When a class use the properties of another class it is known as Inheritance.
Example-Suppose there is class of 'HumanBeing' which contains two common properties like Ear & Mouth. Also there is some action define such as can speak and can hear.
public class HumanBeing
{
public bool mouth = true;
public bool ear = true;
public void canSpeak()
{
Console.WriteLine("Yes! can speak.");
}
public void canHear()
{
Console.WriteLine("Yes! can hear.");
}
}
Lets create two another class of 'Man' & 'Women' which is inherit from 'HumanBeing'.
Which uses its common properties further.
public class Man : HumanBeing
{
public void ManProperties()
{
canSpeak();
canHear();
}
}
public class Women : HumanBeing
{
public void WomenProperties()
{
canSpeak();
canHear();
}
}
So,the advantage of Inheritance is that we can reuse the code functionality by defining the common properties at one place.
ghjghjgjh
ReplyDelete