Tuesday, 21 August 2018

Nesting of Methods



Only an object of that class using a dot operator can call a member function of a class. A member function can be called by using its name inside another member function of the same class. This is known as nesting of member functions.
Example Program:
class nest
{
            int m,n;
            void accept(int x,int y)
            {
                        m=x;
                        n=y;
            }
            int largest()
            {
                        if(m>=n)
                                    return m;
                        else
                                    return n;
            }
            void display()
            {
                        int large=largest();        //calling a method
                        System.out.println("Largest Value="+large);
            }
}
class nesting
{
            public static void main(String []args)
            {
                        nest n=new nest();
                        n.accept(6,7);
                        n.display();
            }
}
Output:
D:\ \programs>javac nesting.java
D:\ \programs>java nesting
Largest Value=7          

No comments: