Please note that this article assumes you have read my previous posts on data types, variables, conditionals and loops, etc. and you are able to write a running Hello, World! program.

Introduction

In Object-Oriented Programming (OOP), the most important concept is the class. As its name implied, this is especially the case in class-based OOP, which is the dominant programming style in C#, Java, and C++. Classes are a way to store properties and attributes of any object, in a user-defined structure – for example, a car, a dog, or a person. In OOP, we manipulate different objects; our focus, however, is on data rather than logic. Any object being manipulated in OOP can be considered an instance of a class. Instances needs to be created, or instantiated. One additional feature of class-based programming is data encapsulation, which provides various ways to hide data from a user. The resulting object not only holds logic, but also a state, and therefore it can be manipulated by other objects.

In this post, I will teach you the very fundamental feature of OOP – Classes, and their use and syntax.

Syntax

The syntax for declaring a class is actually the same in Java and C#:

However, a class wouldn’t make sense without any data. As I mentioned earlier, a class is used to hold data, members (variables and properties of a class), and methods (functions in a class that process the data). You can define them in a class as follows:

Where:

  • access modifier = a keyword or keywords that limit the access of a member/method
    • Example: public, private and protected
  • return type = data type that is returned from a function
    • Example: void (for none), int, bool
  • member name = any variable name
    • Example: carColor
  • method name/parameters = the name of the function, and any arguments you want to make available
    • Example: fillUpGasTank (float gallonsToPutIn, bool isPremiumGas)

For example, if we want to create a class for our object SuperMario, we could do so as follows:

Disclaimer: Mario and Super Mario Bros. are trademarked by Nintendo. We have no affiliation with Nintendo. In no way, shape, or form do I recommend eating random mushrooms, throwing fireballs, or jumping on poor turtles.

In this example, you can see that I defined properties and functions of the Mario character in our class SuperMario. You can define as many members or methods you want, as there is virtually no limit (though if your computer is as old as Mario, defining too much might use all of your computer’s memory… but then again, you probably wouldn’t be using C# or Java.) Nonetheless, in the world of programming on integrated devices, efficiency and minimalism continues to be important, so it’s best to get into good habits. As well as our SuperMario class, we can define other classes:

The Three P’s

So what do these three P’s – public, private, and protected – mean?

They are the access modifiers, which tell the compiler whether it should allow a certain member/method of a class to be accessible within its own class or another classes. The public modifier makes a member/method available to other classes, while private makes it available only within the class where it is declared. The protected modifier 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 the current assembly.)

Instantiation Nation

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:

<class name> <object name> = new <class name>();

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

In our class, we could also declare Mario’s state as protected, because there is no inheritance involved in our program. Due to this, the results would be the same (i.e. you wouldn’t be able to access state in your Main function). Later, in my post on inheritance, I will teach you its use.

You can see how easy it is to use the feature of classes in your programs. If there is anything I forgot to mention about classes that you want to know about , just leave a comment and I will come to your rescue!

Yet Another Disclaimer: Running this program will not really allow you to play Super Mario Bros. on your computer.

Share

Comments are closed.

Post Navigation