Monday, July 27, 2020

Class, Object and Methods

Let us now look into what do class, object and methods mean…..

Class − A class can be defined as a template or blueprint that describes the behavior or state that the object of its type supports.

Class structure
 class class_name {
      code block

}

Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behavior such as wagging their tail, barking, eating. An object is an instance of a class.

Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
  • Group of instruction to do a specific task.

Examples:

  • A method to add two numbers.
  •  A method to say Hi to the user.
  •  A method to get the user’s name.
  •  We have a special method called ‘Main’.

Method structure – Each method consists of 4 main parts…

return_type method_name (parameter) {
    code block

}

Note: Every method is written inside a class.

  •  A class is a container of methods.

Calling a method – is basically using the method

     method_name (give parameter);

  • Calling a method is a statement in java.
  • The code block of this method will be executed.

Note: The main() method is automatically called when we run our java program

  • It is the first java method that is called.
  •  It is the starting point of execution of our program.

Examples of object, class and methods and their brief introduction. You will learn about these in a later chapters.


No comments:

Post a Comment