Top Ad unit 728 × 90

Welcome !!! To The World of Programming

OOPS FEATURES

Oops

Instance Methods and Class Methods

 Static Methods and Non-Static Methods

A class is a user-defined data-type and is used to model a real-world Entity. The information required of an entity becomes data-members or fields of the class. The instance fields represent the information required of an entity and Class fields are used to store and maintain common data related all the objects or instances of the class.

Instance  Method



Instance Methods:

  • Instance methods are associated with and operate on instances (objects) of a class.

  • It is also known as "object methods." (Non-static Methods)

  • It can access and manipulate the instance-specific data (properties) of an object. It means the properties of the object or instance which is used to invoke or call it.

  • Instance methods are called on specific object instances.

  • Instance methods can be used to perform actions that are specific to individual objects.  
  • It is called using the object or instance of the class – object-name.instanceMethod ().

Class Methods:

  • It also known as "static methods" in some languages.

  • Class methods are associated with the class rather than instances of the class.

  • They do not have access to instance-specific data and cannot manipulate object properties.

  • Class methods are typically used for operations that are related to the class as a whole, not to individual objects.

  • They can be called on the class itself, without needing to create an object instance.

  • Class methods are called or invoked using class and not the object or an instance of the class – className.classMethodName().

  • It is also used to Library of functions.

  class MathLib:
    @classmethod
    def square(cls, number):
        return number * number

   result = MathLib.square(6)  # Calls the class method on the class itself

 
Instance methods are used to perform operations that depend on the state of a specific object. Class methods are used for operations that are independent of object-specific data i.e. are shared across all instances of a class. The choice between instance methods and class methods depends on the specific requirements of application and the nature of the operations needed to be performed.

Instance Methods and Class Methods in Java:

In Java, Class methods, also known as Static methods. A class method belongs to the class. It is not related to any specific instance (object) of the class. It is defined using the static keyword.  It can be called on the class itself without the need to create an instance of the class. 

class MyLib {
    public static void classMethod() {
        System.out.println(" I am Class Method !!!");
    }
    public void instanceMethod() {
        System.out.println(" I am Instance Method !!!");
    }
}
class TestLib {
    public static void main(String[] args) {
        //calling class method
        MyLib.classMethod();
        //calling instance Method
        MyLib myLib = new MyLib();
        myLib.instanceMethod();
    }
}


Instance Methods and Class Methods in C#.Net:

In C#.NET, Class methods are called Static methods. Like in Java, they are defined using the static keyword. It can be called on the class without the need to create an instance of the class.


 using System;
class MyLib {
    public static void classMethod() {
        Console.WriteLine(" I am Class Method !!!");
    }
    public void instanceMethod() {
        Console.WriteLine(" I am Instance Method !!!");
    }
}
public class Test
{
    public static void Main(string[] args)
    {
        //calling class method
        MyLib.classMethod();
        //calling instance Method
        MyLib myLib = new MyLib();
        myLib.instanceMethod();
    }
}
 

Instance Methods and Class Methods in VB.NET:


In VB.NET, class methods are referred to as Shared methods. Shared methods are associated with the class itself rather than any specific instance of the class. It is defined with the Shared keyword. It can be called on the class without needing to create an instance of the class.



public Class MyLib
   public Shared Sub  classMethod()
       Console.WriteLine("I am a Class Method")
   End Sub
   public Sub instanceMethod()
       Console.WriteLine("I am a Instance Method")
   End Sub  
   Public Shared Sub Main()
        ' Create an instance of the class
        Dim myLib As New MyLib()

        ' Call instance method on the object
        myLib.instanceMethod()

        ' Call the shared method on the class itself (no instance required)
        MyLib.classMethod()
    End Sub
End Class


Instance Methods and Class Methods in Python:

In Python, class methods are a specific type of method associated with a class rather than an instance of the class. It is defined using the @classmethod decorator. It is  typically used for methods that operate on the class itself or perform some class-level operation rather than instance-level operations.



class MyLib:
    
    def instanceMethod(self):
        print("I am a Instance method")

    @classmethod
    def classMethod(cls):
        print("I am a Instance method")

# Create an instance of the class
myLib = MyLib()

# Call an instance method on the object
myLib.instanceMethod()

# Call a class method on the class itself
MyLib.classMethod()


Instance Methods and Class Methods in PHP:

In PHP, Like Java and C#.NET, Class methods are also “Static Methods." Static methods belong to the class and does not relate to an instance of the class. They are defined using the static keyword. It requires class to call like other object-oriented languages.

<?php
class MyLib {
    
    // Instance method
    public function instanceMethod() {
        // Can access instance properties
        echo "I am a Instance method \n";
    }

    // Static method (class method)
    public static function classMethod() {
        // Cannot access instance properties; only static members
        echo "I am a Class method \n";
    }
}

// Create an instance of the class
$myLib = new MyLib();

// Call the instance method on the object
$myLib->instanceMethod();

// Call the static method on the class itself (no instance required)
MyLib::classMethod();

?>

Instance Methods and Class Methods in C++:

C++ do not have a built-in concept of class methods like Python or Java. In C++, member functions (methods) of a class operate on instances of that class. However, One can achieve similar functionality by using static member functions. in C++ are functions that belong to the class. It is called with the scope resolution operator(::).



#include <iostream>

class MyLib {

public:
    
    void instanceMethod() {
        std::cout<<"I am a Instance method "<<std::endl;
    }

    static void classMethod() {
        std::cout<<"I am a Class method"<<std::endl;
    }
};

int main() {
    // Create an instance of the class
    MyLib myLib;

    // Call the instance method on the object
    myLib.instanceMethod();

    // Call the static method on the class itself (no instance required)
    MyLib::classMethod();

    return 0;
}







The conclusion is that the concept remains the same. However the syntax or the concept is implemented in a selected Languages and the code is changes. One can master Object-Oriented concepts of one Language and can implement in any other Object-Oriented Language.
Instance Methods and Class Methods Reviewed by Syed Hafiz Choudhary on October 16, 2023 Rating: 5

No comments:

Contact Form

Name

Email *

Message *

Powered by Blogger.