Open In App

Class Constructor Methods in MATLAB

Last Updated : 04 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Class constructors are special methods in MATLAB that are used to create an instance of a class. In other words, they are used to initialize the properties of an object when it is created.

Class constructors typically have the same name as the class itself, and they are defined as methods within the class definition.

The concept of class constructors is important in object-oriented programming, as they allow you to create objects that have specific properties and behavior.

To illustrate the concept of class constructors in MATLAB, let’s consider the following example. Suppose we want to create a class called “Cricketer” that has two properties: “name” and “position”. We can define this class as follows:

Example 1:

Matlab




classdef Cricketer
    properties
        name
        position
    end
      
    methods
        function obj = Cricketer(name, position)
            obj.name = name;
            obj.position = position;
        end
    end
end


In this example, the class “Cricketer” has two properties: “name” and “position”. The class also has a method called “Cricketer”, which is the class constructor. The class constructor takes two input arguments: “name” and “position”.

To create an instance of the “Cricketer” class, we can use the “Cricketer” constructor as follows:

Matlab




p1 = Cricketer('Virat', 3);


This creates an object called “p1” of the “Cricketer” class, with the name “Virat” and position 3.

We can access the properties of the object using the dot notation:

Matlab




p1.name % Output: 'Virat'
p1.position % Output: 3


Output:

 

The first line of code accesses the name property of the Cricketer object stored in the p1 variable and displays its value, which is ‘Virat’.

The second line of code accesses the position property of the Cricketer object stored in the p1 variable and displays its value, which is 3.

We can also modify the properties of the object using the dot notation:

Matlab




p1.name = 'Yuvraj';
p1.position = 4;


Then, we try to get an output again:

Matlab




disp(p1.name) % Output: 'Yuvraj'
disp(p1.position) % Output: 4


Output:

 

The first line of code displays the value of the name property of the Cricketer object stored in the p1 variable, which is ‘Yuvraj’. The second line of code displays the value of the position property of the Cricketer object stored in the p1 variable, which is 4.

Key Points

There are several other concepts related to class constructors in MATLAB that you may want to explore.

One such concept is default property values. By default, the properties of an object are initialized to empty values. However, you can specify default values for the properties of an object by using the “DefaultValue” attribute in the class definition.

Another concept is inheritance. In inheritance, you can create a new class that inherits the properties and methods of an existing class. This can be useful when you want to create a class that is similar to an existing class but with some additional or modified behavior.

Finally, you can also use class constructors to create objects with private properties. Private properties can only be accessed or modified by methods within the class, and are not visible to other functions or classes. This can be useful for encapsulation, as it allows you to control the way that the properties of an object are accessed and modified.

Conclusion

In conclusion, class constructors are an important concept in object-oriented programming in MATLAB. They allow you to create objects that have specific properties and behavior, and they provide a way to initialize the properties of an object when it is created. There are also several other concepts related to class constructors that you may want to explore, such as default property values, inheritance, and private properties.

There are several other concepts related to class constructors in MATLAB that you may want to explore.

One such concept is default property values. By default, the properties of an object are initialized to empty values. However, you can specify default values for the properties of an object by using the “DefaultValue” attribute in the class definition.

Another concept is inheritance. In inheritance, you can create a new class that inherits the properties and methods of an existing class. This can be useful when you want to create a class that is similar to an existing class but with some additional or modified behavior.

Finally, you can also use class constructors to create objects with private properties. Private properties can only be accessed or modified by methods within the class, and are not visible to other functions or classes. This can be useful for encapsulation, as it allows you to control the way that the properties of an object are accessed and modified.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads