Some of the main concepts of Object Oriented (OO) programming languages are encapsulation, inheritance and polymorphism. Class and Structure are two of the OO constructs/tools, which help programmers to achieve these concepts within most of the exiting OO programming languages (Java does not provide structures). Classes are an abstract representation of real world objects. Structures are very similar to classes with a similar use, but have few limitations compared to classes. Both classes and structure are used to group similar type of data, together.
What are Classes?
Classes depict the abstract representation of real world objects, while the relationships depict how each class is connected to others. Both classes and relationships have properties called attributes. Methods in the classes represent or define the behavior of these classes. Methods and attributes of classes are called the members of the class. Typically, encapsulation is achieved by making the attributes private, while creating public methods that can be used to access those attributes. An object is the instance of a class. Inheritance allows the user to extend classes (called sub classes) from other classes (called super classes). Polymorphism allows the programmer to substitute an object of a class in place of an object of its super class. Typically, the nouns found in the problem definition directly become classes in the program. And similarly, verbs become methods. Public, private and protected are the typical access modifiers used for classes. A class diagram shows the systems’ classes, relationships between classes, and their attributes.
What are Structures?
As mentioned above, structures are very similar to classes with a similar use, but are slightly limited capabilities than classes. In fact, classes can be considered as an extension to structures. For example, structures are same as classes in C++, but they have public members by default. A structure can be defined by the user to represent a type that is composite. Similar to classes, structures contain members that can belong to many types. The keyword struct is used to define a structure in C and C++, while the keyword Structure is used for the same in .NET programming languages.
What is the difference between Classes and Structures?
Even though, both classes and structures are similar constructs found in programming languages, they have subtle differences. Typically, the class is an extension of the structure, and therefore structures have some relative limitations. For example, structures can be considered the same as classes in C++, but members are not public by default in classes (unlike in structures). This actually means that you can define a class and a structure with exactly the same characteristics using the appropriate access modifiers in C++. However in C, structures cannot contain any functions or overloaded operations. Keywords class and struct are used to define a class and a structure in C++, respectively. When it comes to .NET languages (C#, VB.NET, etc.), class is a reference type, while structure is a value type. And usually, structures are used for smaller objects, but classes are used for larger objects that are kept in the memory for longer periods.