/* related posts with thumb nails */

Nesting of methods:

Generally, methods in object oriented programming languages are called by an object of that class or class itself in the case of static methods using the dot operator. However, there is an exception to this. A method can be called using only by its name using another method of the same class or a child class provided it is accessible. This is known as nesting of methods. The following program illustrates this concept.

class math

{

public static int fact(int n)

{

int f=1;

for(int i=1; i<=n ; i++)

f=f*i;

return f;

}

public static int NCR(int n, int r)

{

return fact(n)/(fact(n-r)*fact(r)) ;

}

}

class Test

{

public static void main(String as[])

{

System.out.println(NCR(4,3));

}

}

Explanation:

In the above example, the method ‘NCR’ calls another method ‘fact()’ in it. As explained, it has been called without any objects. Here, we are invoking it in the same class.

Related Topics:

0 comments:

Post a Comment