Choose Your Language

Friday, 5 December 2014

How to access the private member of a class in another class?

public class MyMainClass{
   
    public static void main(String args[])
    {
    MySubClass mySubClass=new MySubClass();
    System.out.println(mySubClass.getPrivateMethod());
    }
    }

class MySubClass {         
           
         private String myPrivateMethod(){
         return "Aravind Sankaran";
         }
        
         public String getPrivateMethod(){
             return myPrivateMethod();
         }
    }

Output:
Aravind Sankaran

No comments:

Post a Comment