A group of statement that accomplishes a specific task.
Syntax:
access_modifierreturn_typeMethodName (parameter_list) { //statement(s) in method body
}
Access_modifier
Determines the access level or visibility of the method from another class. The modifier can be private or public.
private
Defines that the method can only be called in the class where it is declared.
public
Defines that the method is accessible by all other classes in your application.
If accessibility is not specified, the modifier of the method is declared as private by default.
return_type
specifies whether the method returns a value or not.
return type
Is the data type of the value that the method should return using the return keyword.
void
If the method is not returning any values.
For example, if a method should return an integer value, the return type should be set to int.
MethodName
Is an identifier and is case sensitive.
Method names should always be followed by parentheses.
A method may or may not contain a parameter list.
parameter list
Refers to the required type, order, and number of the parameters of a method.
Parameters
Are used to pass and receive data from a method.
method body
Contains a set of statements that performs the specific task of the method.
Method body are enclosed within two (2) curly braces.
Methods are declared within a class or structure by specifying the access level such as public or private, the return type, the name of the method, and any method parameters. Together, these parts are called method signatures.
The required values, if any, are enclosed within the parentheses. These required values are called arguments.
Method overloading
means that methods of the same name can be declared in the same class as long as they have different sets of parameters which are determined by the number, types, and order of the parameters.
Method overloading
It is used to create several methods with the same name that perform the same or similar task, but on different types or different numbers of arguments.
Structures
Are used to make a single variable that holds related data of various data types.
struct
The keyword used for creating a structure.
Structures
Are used to represent a record.
access_modifier structStructName {
//structure members( fields, methods, and constructors)
}
class
a data structure or a blueprint that defines data and actions in a single unit.
Class is defined using the keyword class.
objects
Dynamically created instances of the class.
Instances of classes are created using the new keyword which allocates memory for a new instance.
Syntax of creating an instance:
ClassNameobjectName = new ClassName(arguments_list);
arguments_list
Refers to the actual value passed on the constructor or method.
Encapsulation
The process of hiding or encapsulating data from the outside world.
A class or structure can specify how accessible each of its members is to code outside of the class or structure.
Encapsulation can be implemented by using the following:
Defining the modifier of class data members, like instance variables, as private
Accessing the private data members through the class methods or by using properties
The return keyword is used in the get accessor to return the value of the property.
The value keyword used in set mutator represents the value being assigned to the property.
this
A keyword refers to the object or the current instance of the class.
this
This can be used within class members.
this
Capability to pass a reference from the current object instance to a method.
this
Helps resolve ambiguity when the name of the data members and parameters are the same.