हिंदी

What is meant by a Method. Explain with reference to a class?

Advertisements

प्रश्न

What is meant by a Method. Explain with reference to a class?

स्पष्ट कीजिए
Advertisements

उत्तर

A Method is a block of statements defined within a class that performs a specific task or operation. In Java, methods are used to define the behaviour of objects. A method is executed when it is called or invoked.

Explanation with Reference to a Class:

  • A method is declared inside a class and contains statements to perform a particular operation.
  • A class can contain one or more methods to define the behaviour of its objects.
  • Methods can access and modify the data members of a class, depending on their access permissions.
  • Methods help in code reusability, reduce repetition and make programs easier to understand and maintain. 
class Student 
{
    void display() 
    {
        System.out.println("Welcome to Java");
    }
}

In the above example, Student is the name of the class, and display() is a method defined inside it. The method displays the message "Welcome to Java" when it is called.

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 4: Objects - EXERCISE [पृष्ठ १०६]

APPEARS IN

धीरज मेहरोत्रा Computer Science [English] Class 12
अध्याय 4 Objects
EXERCISE | Q 2. | पृष्ठ १०६
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×