• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

True & flase Statements

hi..i need to know if the following sentences are true or false..and why? simple explanation plz

1) subclass variables should be defind as private to hide them from other subclasses. (        )
2) main method can't directly access any non static method and varibales,but using the instance of the class it can.!!!!!!


my comments or concerns..for the following :
1) i guess it should be true.. cuz of the encapsulation.. am i right..or it's not forced to be private .. only it's a better way to hide variables.. ?!?!!/ i'm not sure.just giving what i know..and waiting for ur answer

2) but why other static methods can't use instance of the class?!?!!?

hope i've made myself clear enough to get ur answers..
i'll be waiting..
0
ms_lost
Asked:
ms_lost
1 Solution
 
CEHJCommented:
1. True. As you say 'cuz of the encapsulation'. This is true generally of any class, not just subclasses. This prevents client classes from directly accessing the fields directly. Using accessor methods (get/set) provides a useful layer of indirection, such that the method may do things other than simple assignments (this.someProp = someProp).
2. True. 'but why other static methods can't use instance of the class?!?!!?' They can:

zclass StaticAccess {

  /**
   * Note that this variable must be declared 'static' to be accessible from
   * accessInstance() or any other static method
  */
  static StaticAccess instance;

  public static void accessInstance() {
    instance.printMessage();
  }
     
  public void printMessage(){
    System.out.println("In instance method");
  }

  public static void main(String[] args) {
    // Comment the next line out. What will the result be?
    instance = new StaticAccess();
    StaticAccess.accessInstance();
  }
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now