Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. β€Ί
  3. posts
  4. β€Ί
  5. …

  6. β€Ί
  7. 2 4 OOPS

Loading ⏳
Fetching content, this won’t take long…


πŸ’‘ Did you know?

πŸ¦₯ Sloths can hold their breath longer than dolphins 🐬.

πŸͺ This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ⏳
Fetching content, this won’t take long…


πŸ’‘ Did you know?

🍯 Honey never spoils β€” archaeologists found 3,000-year-old jars still edible.
AI-Math

    AI-AgenticAI

    AI-DeepLearning

    AI-GenAI

    AI-Infrastructure

    AI-Machine-Learning

    AI-Math
    • Advance Maths for Machine Learning

    • Algebra for Notation and Geometry

    • ️Advance MultiVariant Linear Algebra

    • MATLAB Fundamentals

    • MATLAB Operators

    • MATLAB Control Flow & Logic

    • MATLAB Object-Oriented Programming (OOP)

    • MATLAB Plotting & Visualization

    • AI-Math Index


    AWS

    Azure

    Hobbies

    kubernetes

    Management

    Programming

    Terraform

    Z_Appendix

    0-root

Cover Image for MATLAB Object-Oriented Programming (OOP)
AI-Math

MATLAB Object-Oriented Programming (OOP)

Introduction to object-oriented programming in MATLAB including classes, properties, methods, constructors, inheritance, encapsulation, and practical examples.

MATLAB
Object Oriented Programming
OOP
Classes
Inheritance
Encapsulation
← Previous

MATLAB Operators

Next β†’

MATLAB Plotting & Visualization

MatLab Programming

Functions

Single Output function


   function out =fnName(param1, param2...)
    ...body
    end  
    

Multi Output function


   function [out1, out2, ..] =fnName(param1, param 2...)
     ...body  
    end   

eg:

  function y = squareThisNumber(x)
  y= x^2
  
  // Multiple Output
  function [y1,y2] = squareAndCubeThisNumber(x)
  y1= x^2
  y2= x^3

Anonymous function

  • Returns only single output

  • Can be pass as input to other function

  • f is called Function Handle

    f = @(params..) expression


Classes

A MATLAB class lives in its own file, named after the class, starting with classdef.

% File: Point.m
classdef Point
    properties
        x
        y
    end

    methods
        % Constructor β€” same name as the class
        function obj = Point(x, y)
            obj.x = x;
            obj.y = y;
        end

        function d = distanceFromOrigin(obj)
            d = sqrt(obj.x^2 + obj.y^2);
        end
    end
end

Usage:

p = Point(3, 4);
p.distanceFromOrigin()   % 5

Properties

Hold the state/data of an object β€” equivalent to fields/attributes in other OOP languages.

properties
    x
    y = 0          % default value
end

properties (Access = private)
    secret          % only accessible inside the class
end

Methods

Functions that operate on an object's properties. The first argument is conventionally obj.

methods
    function obj = setX(obj, newX)
        obj.x = newX;     % MATLAB objects are value types β€” must return obj
    end
end

Constructor

A method with the same name as the class, called automatically when you create an instance (Point(3,4)). If omitted, MATLAB provides a default constructor that takes no arguments.

Inheritance

A subclass extends a base class using < BaseClassName, reusing its properties/methods and overriding what it needs to.

classdef Point3D < Point
    properties
        z
    end

    methods
        function obj = Point3D(x, y, z)
            obj = obj@Point(x, y);   % call superclass constructor
            obj.z = z;
        end

        function d = distanceFromOrigin(obj)
            d = sqrt(obj.x^2 + obj.y^2 + obj.z^2);   % overrides Point's method
        end
    end
end

Encapsulation

Restrict direct access to internal state using Access attributes on properties/methods, exposing only what callers need.

properties (Access = private)
    balance = 0
end

methods
    function obj = deposit(obj, amount)
        obj.balance = obj.balance + amount;
    end

    function b = getBalance(obj)
        b = obj.balance;   % controlled read access, no direct external writes
    end
end
Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Wed Feb 25 2026

Share This on

← Previous

MATLAB Operators

Next β†’

MATLAB Plotting & Visualization

AI-Math/2-4-OOPS
Let's work together
+49 176-2019-2523
hiteshkrsahu@gmail.com
WhatsApp
Skype
Munich πŸ₯¨, Germany πŸ‡©πŸ‡ͺ, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
Β  Home/About
Β  Skills
Β  Work/Projects
Β  Lab/Experiments
Β  Contribution
Β  Awards
Β  Art/Sketches
Β  Thoughts
Β  Contact
Links
Β  Sitemap
Β  Legal Notice
Β  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| Β© 2026 All rights reserved.