Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Classes and Structures

Difference Between Classes and Structures

June 14, 2011 Posted by Indika

Classes vs Structures

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.

Related posts:

Difference Between Objects and Classes Difference Between Graph and Tree Difference Between C and C++ Difference Between Java and C language Difference Between Object Oriented Programming and Procedural Programming

Filed Under: Programming Tagged With: .NET, attributes, C, Class, Classes, Concepts of OO programming languages, Data Structures, methods, OO programming languages, relationships, struct, Structure, VB

About the Author: Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Leave a Reply Cancel reply

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Limonene and D Limonene

Difference Between Limonene and D Limonene

Difference Between iPhone 6 Plus and Galaxy S6 Edge

Difference Between iPhone 6 Plus and Galaxy S6 Edge

Difference Between Phrase and Sentence

Difference Between Phrase and Sentence

Difference Between CPA and CIMA

Difference Between So and Such

Latest Posts

  • Difference Between Symmetric and Asymmetric Stem Cell Division
  • Difference Between Operon and Cistron
  • Difference Between TDS and Salinity
  • Difference Between Artificial Selection and Genetic Engineering
  • Difference Between Direct and Indirect Hormone Action
  • Difference Between Steroid and Corticosteroid
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.