DEV Community 👩‍💻👨‍💻

Jack Pritom Soren
Jack Pritom Soren

Posted on

Method and Attribute in OOP

Method :

A method is the equivalent of a function in object-oriented programming. A noun is to a verb what a variable is to a method — the methods are the actions that perform operations on a variable. A method accepts parameters as arguments, manipulates these, and then produces an output when the method is called on an object. Methods are similar to functions, but methods are also classified according to their purpose in the class design. In classes, variables are called attributes, so methods often operate on attributes.

public class Main {
  static void myMethod() { //myMethod() is the name of the method
    // code to be executed
  }
}
Enter fullscreen mode Exit fullscreen mode

Calling Method :

public class Main {
  static void myMethod() {
    System.out.println("From Method");
  }

  public static void main(String[] args) {
    myMethod();
  }
}

// Outputs "From Method"
Enter fullscreen mode Exit fullscreen mode

Attribute :

Attributes are data members inside a class or an object that represent the different features of the class. They can also be referred to as characteristics of the class that can be accessed from other objects or differentiate a class from other classes.

public class Main {
  int x = 1;
  int y = 2; 
  // Create a class called "Main" with two attributes: x and y
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.