Object Oriented Programming
Simply stated, object-oriented programming is an approach to programming
that breaks a programming problem into objects that interact with each other.
Objects are created from templates known as classes. You can think of a class
as the blueprint of a building. An object is the actual “building” that we build
based on the blueprint.
Classes
To write our own class, we use the class keyword, following by the name of
the class.
Class className {
}
Let's dive more deeper into the concepts.
- Fields: A field is simply a variable that is declared inside a class. Like any othervariables, they are used to store data
class Person {
public string name;
public int age;
}
Notice, that there are public keyword in front of every field/variables, they are known as access modifiers, they are gate keepers, that controls who has access to the fields or methods.
- Methods: functions/methods are basically a block of code which we can use anytime we want without repeating ourselves.
class Person {
public string name;
public int age;
public void GetName() {
Console.WriteLine(name);
}
}
Objects
An object is created from a class. We have already created the class named Person, so now we can use this to create objects. A object is basically an instance of a class. To create a object of the class we use the new keyword.
class Person {
public string name;
public int age;
public void GetName() {
Console.WriteLine(name);
}
}
public class Program {
public static void Main(string[] args) {
Person P = new Person();
}
}
As we have created an instance of the class we can also access the fields and methods availiable inside the class from our program class and make modifications to it.
class Person {
public string name;
public int age;
public void GetName() {
Console.WriteLine(name);
}
}
public class Program {
public static void Main(string[] args) {
Person P = new Person();
P.name = "Priyanshu Bhattacharjee";
P.age = 17;
P.GetName();
}
}
We can also create multiple instances of the Person class without any issue.
class Person {
public string name;
public int age;
public void GetName() {
Console.WriteLine(name);
}
}
public class Program {
public static void Main(string[] args) {
Person P = new Person();
P.name = "Priyanshu Bhattacharjee";
P.age = 17;
P.GetName();
Person P1 = new Person();
P1.name = "Rohan Mallick";
P1.age = 23;
P1.GetName();
}
}
- NOTE: Fields and Methods inside a class is often referred as Class Members.
Constructors
A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. It can be used to set initial values for fields:
class Person {
public string name;
public int age;
public Person() {
name = "Default Human";
age = 0;
}
public void GetName() {
Console.WriteLine(name);
}
}
public class Program {
public static void Main(string[] args) {
Person P = new Person();
P.GetName();
}
}
- A constructor name must match the class name, and it cannot have a return type
- A constructor is called when the object is created.
- All classes have constructors by default: if you do not create a class constructor yourself, C# creates one for you. However, then you are not able to set initial values for fields
Constructor Parameters
Constructors can also take parameters, which is used to initialize fields and this can save a lot of time.
class Person {
public string name;
public int age;
public Person(string _name, int _age) {
name = _name;
age = _age;
}
public void GetName() {
Console.WriteLine(name);
Console.WriteLine(age);
}
}
public class Program {
public static void Main(string[] args) {
Person P = new Person("Rohit", 17);
P.GetName();
}
}
Access Modifiers
Access modifiers basically defines from where we can access our methods and variables from.
So far there are 2 access modifiers that you should know.
Public means that we can access the class members from outside the class whereas as Private doesn't allows us to access the class members outside the class but can only be accessed within the class.
private string name;
private int age;
Static Keyword
Static Keyword actually helps us to get access to the class members such as variables, methods, etc without needing to create an instance/object of the class whereas in non-static class members we have to create object of the class to access the class members.
class Person {
public static string name;
public static int age;
public static void GetName() {
Console.WriteLine(name);
Console.WriteLine(age);
}
}
public class Program {
public static void Main(string[] args) {
Person.name = "Priyanshu";
Person.age = 17;
Person.GetName();
}
}
Static Classes
In addition to having static methods, fields, properties and constructors, we
can also have static classes. A static class can only contain static members.
static class Person {
public static string name;
public static int age;
public static void GetName() {
Console.WriteLine(name);
Console.WriteLine(age);
}
}
public class Program {
public static void Main(string[] args) {
Person.name = "Priyanshu";
Person.age = 17;
Person.GetName();
}
}
Object Oriented Programming
Simply stated, object-oriented programming is an approach to programming that breaks a programming problem into objects that interact with each other. Objects are created from templates known as classes. You can think of a class as the blueprint of a building. An object is the actual “building” that we build based on the blueprint.
Classes
To write our own class, we use the class keyword, following by the name of the class.
Class className { // Contains fields and methods }
Let's dive more deeper into the concepts.
class Person { public string name; public int age; }
Notice, that there are public keyword in front of every field/variables, they are known as access modifiers, they are gate keepers, that controls who has access to the fields or methods.
class Person { public string name; public int age; public void GetName() { Console.WriteLine(name); } }
Objects
An object is created from a class. We have already created the class named Person, so now we can use this to create objects. A object is basically an instance of a class. To create a object of the class we use the new keyword.
class Person { public string name; public int age; public void GetName() { Console.WriteLine(name); } } public class Program { public static void Main(string[] args) { Person P = new Person(); } }
As we have created an instance of the class we can also access the fields and methods availiable inside the class from our program class and make modifications to it.
class Person { public string name; public int age; public void GetName() { Console.WriteLine(name); } } public class Program { public static void Main(string[] args) { Person P = new Person(); P.name = "Priyanshu Bhattacharjee"; P.age = 17; P.GetName(); } }
We can also create multiple instances of the Person class without any issue.
class Person { public string name; public int age; public void GetName() { Console.WriteLine(name); } } public class Program { public static void Main(string[] args) { Person P = new Person(); P.name = "Priyanshu Bhattacharjee"; P.age = 17; P.GetName(); Person P1 = new Person(); P1.name = "Rohan Mallick"; P1.age = 23; P1.GetName(); } }
Constructors
A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. It can be used to set initial values for fields:
class Person { public string name; public int age; public Person() { name = "Default Human"; age = 0; } public void GetName() { Console.WriteLine(name); } } public class Program { public static void Main(string[] args) { Person P = new Person(); P.GetName(); } }
Constructor Parameters
Constructors can also take parameters, which is used to initialize fields and this can save a lot of time.
class Person { public string name; public int age; public Person(string _name, int _age) { name = _name; age = _age; } public void GetName() { Console.WriteLine(name); Console.WriteLine(age); } } public class Program { public static void Main(string[] args) { Person P = new Person("Rohit", 17); P.GetName(); } }
Access Modifiers
Access modifiers basically defines from where we can access our methods and variables from.
So far there are 2 access modifiers that you should know.
Public means that we can access the class members from outside the class whereas as Private doesn't allows us to access the class members outside the class but can only be accessed within the class.
private string name; private int age;
Static Keyword
Static Keyword actually helps us to get access to the class members such as variables, methods, etc without needing to create an instance/object of the class whereas in non-static class members we have to create object of the class to access the class members.
class Person { public static string name; public static int age; public static void GetName() { Console.WriteLine(name); Console.WriteLine(age); } } public class Program { public static void Main(string[] args) { Person.name = "Priyanshu"; Person.age = 17; Person.GetName(); } }
Static Classes
In addition to having static methods, fields, properties and constructors, we can also have static classes. A static class can only contain static members.
static class Person { public static string name; public static int age; public static void GetName() { Console.WriteLine(name); Console.WriteLine(age); } } public class Program { public static void Main(string[] args) { Person.name = "Priyanshu"; Person.age = 17; Person.GetName(); } }