Parameters and Polymorphism

Parameters
A parameter is a special variable that are used as a subroutine to connect to another piece of an inputted data in that subroutine. Some programming languages refer parameters as arguments. There are two kinds of parameters: formal parameter and actual parameter.


Formal Parameter
Formal parameters are actually any kinds of variables placed inside a parenthesis. In the example below, the formal parameters are num1 and num2

class CodeGalaxy
{
public int sum;

public CodeGalaxy()
{
sum = 0;
}

public void Add(int num1, int num2)
{
sum = num1 + num2;
}
}


Actual Parameters
An actual parameter is the value of the formal parameter. In the example below, the actual parameters are 2 and 3. The values of the actual parameter are passed to the formal parameter. If you refer to the codes above (formal) and the codes below (actual), 2 is passed to the formal parameter num1, while 3 is passed to the formal parameter num2.


public class UseGalaxy
{
public static void mai (String args[])
{
CodeGalaxy planet = new CodeGalaxy();
planet.Add(2,3);
}
}



Polymorphism
Polymorphism originated from the Greek word poly which means many, and morph which means form. Therefore, polymorphism means many forms. In programming, polymorphism is a variable, function, or (commonly) an object that has many forms. The main purpose of polymorphism in programming is for creating one name that can be used for a general class. Method Overriding and Method Overloading is a type of Polymorphism.

For more polymorphism, see next post (Method Overloading and Method Overriding)

No comments:

Post a Comment

Extended Search

Custom Search
Hello World