In the real world, you often have many objects of the same kind. For example, your bicycle is just one of many bicycles in the world. Using object-oriented terminology, we say that your bicycle object is an instance of the class of objects known as bicycles. Bicycles have some state (current gear, current cadence, two wheels) and behavior (change gears, brake) in common. However, each bicycle’s state is independent of and can be different from other bicycles.

When building bicycles, manufacturers take advantage of the fact that bicycles share characteristics by building many bicycles from the same blueprint–it would be very inefficient to produce a new blueprint for every individual bicycle they manufactured.

In object-oriented software, it’s also possible to have many objects of the same kind that share characteristics: rectangles, employee records, video clips, and so on. Like the bicycle manufacturers, you can take advantage of the fact that objects of the same kind are similar and you can create a blueprint for those objects. Software “blueprints” for objects are called classes.

For example, you could create a bicycle class that declares several instance variables to contain the current gear, the current cadence, and so on, for each bicycle object. The class would also declare and provide implementations for the instance methods that allow the rider to change gears, brake, and change the pedaling cadence.

The values for instance variables are provided by each instance of the class. So, after you’ve created the bicycle class, you must instantiate it (create an instance of it) before you can use it. When you create an instance of a class, you create an object of that type and the system allocates memory for the instance variables declared by the class. Then you can invoke the object’s instance methods to make it do something. Instances of the same class share the same instance method implementations (method implementations are not duplicated on a per object basis), which reside in the class itself.

In addition to instance variables and methods, classes can also define class variables and class methods. You can access class variables and methods from an instance of the class or directly from a class–you don’t have to instantiate a class to use its class variables and methods. Class methods can only operate on class variables–they do not have access to instance variables or instance methods.

The system creates a single copy of all class variables for a class the first time it encounters the class in a program–all instances of that class share its class variables. For example, suppose that all bicycles had the same number of gears. In this case, defining an instance variable for the number of gears is inefficient–each instance would have its own copy of the variable, but the value would be the same for every instance. In situations such as this, you could define a class variable that contains the number of gears. All instances share this variable. If one object changes the variable, it changes for all other objects of that type.

Bear in mind that in object-oriented programming, a class is a template definition of the method s and variable s in a particular kind of object. Thus, an object is a specific instance of a class; it contains real values instead of variables.

The class is one of the defining ideas of object-oriented programming. Among the important ideas about classes are:

  • A class can have subclasses that can inherit all or some of the characteristics of the class. In relation to each subclass, the class becomes the superclass.
  • Subclasses can also define their own methods and variables that are not part of their superclass.
  • The structure of a class and its subclasses is called the class hierarchy.

Objects vs. Classes

You probably noticed that the illustrations of objects and classes look very similar to one another. And indeed, the difference between classes and objects is often the source of some confusion. In the real world, it’s obvious that classes are not themselves the objects that they describe–a blueprint of a bicycle is not a bicycle. However, it’s a little more difficult to differentiate classes and objects in the software. This is partly because software objects are merely electronic models of real-world objects or abstract concepts in the first place. But it’s also because many people use the term “object” inconsistently and use it to refer to both classes and instances.

In the figures, the class is not shaded because it represents a blueprint of an object rather than an object itself. In comparison, an object is shaded, indicating that the object actually exists and you can use it.

The Benefit of Classes

Objects provide the benefit of modularity and information hiding. Classes provide the benefit of reusability. Bicycle manufacturers reuse the same blueprint over and over again to build lots of bicycles. Software programmers use the same class, and thus the same code, over and over again to create many objects.

Class vs. Type

In casual use, people often refer to the “class” of an object, but narrowly speaking objects have type: the interface, namely the types of member variables, the signatures of member functions (methods), and properties these satisfy. At the same time, a class has an implementation (specifically the implementation of the methods) and can create objects of a given type, with a given implementation. In the terms of type theory, a class is an implementation‍—‌a concrete data structure and collection of subroutines‍—‌while a type is an interface. Different (concrete) classes can produce objects of the same (abstract) type (depending on type system); for example, the type Stack might be implemented with two classes – SmallStack (fast for small stacks, but scales poorly) and ScalableStack (scales well but high overhead for small stacks). Similarly, a given class may have several different constructors.

Class types generally represent nouns, such as a person, place, or thing, or something nominalized, and a class represents an implementation of these. For example, a Banana type might represent the properties and functionality of bananas in general, while the ABCBanana and XYZBanana classes would represent ways of producing bananas (say, banana suppliers, or data structures and functions to represent and draw bananas in a video game). The ABCBanana class could then produce particular bananas: instances of the ABCBanana class would be objects of type Banana. Often only a single implementation of a type is given, in which case the class name is often identical with the type name.

Which programming languages have classes?

Java, Python, C++, Lisp, and Perl are all examples of popular object-oriented programming languages. They support programming using the classes and objects paradigm.

Five of the most popular object-oriented languages include:

  • Java
  • Python
  • C++
  • Ruby
  • C#

Java – Java is everywhere, and it is one of the most used and in-demand languages of all time. Java’s motto is ‘write once, run anywhere,’ and that is reflected in the number of platforms it runs on and places it’s used.

Python – Python is a general purpose and used in many places. However, Python has a strong foothold in machine learning and data science. It’s one of the preferred languages for that new and ever-growing field.

C++ – C++has the speed of C with the functionality of classes and an object-oriented paradigm. It’s a compiled, reliable, and powerful language. In fact, it’s even used to build compilers and interpreters for other languages.

Ruby – Ruby is another general-purpose programming language. It was built for simplicity. With that said, Ruby is an incredibly powerful language. The creator of Ruby, Yukihiro “Matz” Matsumoto, has said, “Ruby is very simple in appearance, but is very complex inside, just like our human body.”

C# – C# is a programming language designed by Microsoft. It was designed to improve upon existing concepts in C. C# powers the Microsoft .NET framework alongside many web apps, games, desktop apps, and mobile apps.

There are other object-oriented languages that we haven’t covered above. Perl, Objective-C, Dart, Lisp, JavaScript, and PHP are all object-oriented too or support object-oriented principles.

Content Protection by