Classes in Object Oriented Programming (C# and Java)

 In Object-Oriented Programming (OOP), there are two types of programming: class-based programming and prototype-based programming. Class is the most important concept in OOP, especially in class-based OOP (which is the dominant programming style in C#, Java, C++ etc.). It is a way to store properties and attributes of any object in a user-defined type, such as a car, dog, man etc. Think of it as a blueprint where you store all the information and then execute the operation according to the structure. In OOP, we manipulate different objects to perform something, which means that programming in OOP will force you to focus on data and not so much on logic and we are interested to know what the data holds. Basically, you would start with creating the structures first and the logic second.

We need an object or instance of class to manipulate itself and the other objects, which is created through a given class. You might have already heard of data encapsulation when talking about OOP. Class provides the support for data encapsulation by supporting various ways to hide data from a user, thus the user will be able to use only those features that you intended. The created instance (or object) not only holds a logic (code, procedures, etc.), but also a state, and thus can be manipulated by other objects.

In this tutorial, I will teach you the basics of Class in OOP using Java and C# as examples. Let’s get started!

Understanding the syntax

The syntax for declaring a class is same in both Java and C#.

As I mentioned earlier that a class is used to hold data, so it wouldn’t to create a class without a data!A class can hold members (i.e., variables) and methods (i.e., functions in a class that process the data). Following is the syntax to define variables/methods in a class:

Where:

  • access modifier = keywords that limits the access of a member/method, i.e., public, private and protected
  • return type = data type that is returned from a function
  • member name = any variable name

So for example, if we want to create a class for our object “Player”, we can do so as follows:

Here you can see that I defined properties and functions of player in our class “Player”. You can define as many members or methods you want, there is virtually no limit, though older computers will take much memory. Similarly, we can define other classes too:

Understanding public, private and protected

They are the access modifiers for telling a compiler whether it should allow any member/method of a class to be accessible within its class or in other classes too. ‘public’ makes a member/method available to other classes, while ‘private’ makes it available only within the class where it is declared. ‘protected’ makes it available within the class and derived class – which is a bit hard to explain, as it involves the concept of inheritance that I will teach you in later tutorials.

(Note: In C#, there is an another access modifier called “Internal” which limits the access of member/method to current assembly)

Creating an instance of a class

Recall that I said, you need to create an instance of a class in order to use it in your awesome program. The simple formula to create an instance of a class is as follows:

Using the formula, we can easily create an instance of a class in our program:

In our class, we could also declare “state” as protected because there is no inheritance involved in our program, so the results would be the same; that is, you wouldn’t be able to access “state” in your “Main” function.

Conclusion

You can now see how easy it is to use the feature of classes in your OOP programs. A class is arguably the best way to store data to easily manipulate various states in a program.

If you have any additional questions, then you can ask in the comments section below!

Leave a Reply

Your email address will not be published. Required fields are marked *