Class

Classes

  • A class describes a group of objects with similar properties (attributes), common behaviour (operations), common relationships to other objects, and common meaning (“semantics”).
  • It's helpful to think of that "class" should be read as "a class of something" or "a classification of something", like in the sense of "Birds are a classification of an animal", they all share common traits.
  • A class is a blueprint or template for creating objects that share common properties and behaviours. It is a user-defined data type that encapsulates data and functions into a single entity, making it easier to organize and reuse code.
  • A class defines the structure and behaviour of objects, including the properties or attributes that an object can have and the methods or functions that can be performed on that object. The attributes of a class are defined as variables or data members, while the methods are defined as functions or member functions.

On the other hand, an object is an instance of a class. It is a specific entity created based on the blueprint provided by the class. Using a "Car" example, an object of the class "Car" could be a specific car such as a red 2019 Honda Civic. It has specific values for its attributes and can perform the methods defined in the class. Objects can be thought of as the cookies created when the cookie cutter is used on the dough.

In summary, a class is a general template that defines the properties and methods of a group of objects, while an object is a specific instance of that class with its own unique values for its properties and methods.

Class Inheritance

Class inheritance is a fundamental concept in object-oriented programming that allows a class to inherit properties and methods from another class. The class that inherits from the other class is called the subclass or derived class, while the class that is inherited from is called the superclass or base class.

The subclass inherits all the attributes and methods of the superclass but can also add its own unique attributes and methods. Inheritance promotes code reuse and helps to organize code by creating a hierarchy of related classes.

For example, consider a class hierarchy of vehicles, where we have a superclass "Vehicle" with attributes such as "make", "model", "year", and "color", as well as methods such as "start", "stop", and "drive". We can create subclasses of "Vehicle" such as "Car", "Truck", and "Motorcycle". Each of these subclasses inherits the properties and methods of "Vehicle" while having their own unique attributes and methods.

  • For instance, the "Car" class can add attributes such as "number of doors" and "fuel efficiency" and methods such as "reverse", while the "Truck" class can add attributes such as "cargo capacity" and methods such as "load" and "unload". By using inheritance, we create a hierarchy of related classes that share common properties and methods while still being able to add specific features to each subclass.

MDriven uses UML and is implemented using C#. When you use MDriven, refer to UML and C# for how most things are implemented.

Watch this video for more insight


This page was edited 153 days ago on 06/17/2024. What links here