Choose Your Language

Monday, 17 June 2013

How to call a private method and a private variable defined inside abstract class using instance of another class?

public class MyMainClass extends MyAbstractClass{
    public static void main(String a[]){
        MyMainClass myMain=new MyMainClass();
       
        System.out.println(myMain.getPrivateVariable());
        System.out.println(myMain.getPrivateMethod());
    }
}

 abstract class MyAbstractClass {
   
     private int y=20;    
     private int myPrivateMethod(){
         return y;
     }
     public int getPrivateVariable(){
         return y;
     }
     public int getPrivateMethod(){
         return myPrivateMethod();
     }
}

No comments:

Post a Comment