Tuesday, November 6, 2012

If two classes are within the same package you dont have to use import statement

(a)
package pack1;
public class ClassA extends ClassB {

    public static void main(String[] args) {
        /*
          You can see from here when we are importing a class within the same package
          say pack1 you need not explicitly import the package say import pack1.ClassB
          also in the class A if you see we have not given any declaration for the integer
          variable i (without any declaration it will take it as default or else protected or public)
        */
        ClassB obj = new ClassB();
        System.out.println("The Output in Class A is " + obj.i);

    }

}

(b)
package pack1;
public class ClassB {
    //Note : If you declare it as private it will show error in ClassA as the classes are not same
    //Can be declared protected, public or (just like that..that is default)
    int i=10;
}

Answer
----------
The Output in Class A is 10

No comments:

Post a Comment