Deals

SCJP in short.....



  1. Default access modifier: When a class is declared with a default access modifier in a package, second class can only extend that class if it is in the same package. Otherwise it gives a compilation error saying Class or interface must be public, in same package, or an accessible member class.          Tip: check for the access violations if any in the question to avoid further calculations/analysis. 
  2. Abstract Modifier:  1. If a class extends an abstract class, it should implement all its abstract methods. 2. It is legal to have non-abstract methods in an abstract class.3. Abstract methods end with semicolon rather than curly braces. 4. An abstract class cannot be instantiated.(we will get compilation error saying: class *** is an abstract class. It can't be instantiated. ). 5. If a single method in a class is abstract, the corresponding class must also be abstract.
  3. We cannot declare a class as both abstract and final.
  4. Interface: 1. All methods in an interface are implicitly public and abstract but not static and final. 2. All variables in interface must be constants( not instance variables) i.e., they must be declared public,static and final. 3. An interface can only extend an other interface but not class and it cannot implement other interface/class.                                                                
    Example1:

    1). public Interface MyInterface{
    void myInterfaceMethod()
    }

    2) public Interface MyInterface{
    public abstract void myInterfaceMethod();
    }
     the two lines of codes 1 & 2 are same.

    Example2:
     1). public Interface MyInterface{
    MYVAR =24;
    }
     class MyClass implements MyInterface{
     MYVAR =20;
     }
    It gives a compliation error. Since the variables declared in interface are constants, i.e., they are by default public static final, they cannot be re-assigned.
  5. Var-args:  Syntax: void myMethod(int... x){ }
    In a method declaration, there can be other arguments along with this and var-arg must be declared last. We can have only one var-arg argument for a method. 
  6. Constructors: 1. No return types. 2. Cannot be marked static/final or abstract. 
  7. Variable types: 1. Primitive 2. Reference variables 3. Instance Variables. Instance variables are declared inside class but outside any method.
  8. Encapsulation: If the question is asking about the behavior of the method or the value of a variable with in  the class or ultimate result of the variable, look for encapsulation is justified or not. For example: When there are methods like setters and getters, the variable used in those methods must be private to that class to ensure encapsulation.
  9. Ex:  public class Test  {  public int myVar;  int getMyVar(){ return myVar;}  void setMyVar(int myVar){ int x= myVar/6; }  }     Here in the above class, myVar maynot be always 1/6th of its value. One who uses "Test" classcan actually change it(yes!!As the myVar is declared public you see !!!)
  10. Overriding: 1. When overriding a super class method, the overriding method cannot be less restrictive. 2. That method and overriding method must have the same signature.( or else it becomes overloading!!). 3. If a method cannot be inherited(i.e., it is not re-implemented in the subclass), it cannot be overridden!It means, when we access a subclass and did not implement the method of super class, you cannot access that method using instance of the subclass. 
  11. Example: public class Test{ public static void main(String [] args){ Sub s=new Sub(); s.myMethod();   /* here it is illegal. Because myMethod is not re-implemented */ } } class Super{ void myMethod(){ System.out.println("super"); } class Sub extends Super{ void otherMethod(){} }
........still more to come

No comments:

Post a Comment