Thursday, November 5, 2015

OOP With Java-Part 2

                                                    Access Modifiers
      There are three main access modifiers available in java.

  1. Private
  2. Protected
  3. Public                
    Other than these three access modifiers there is one more access level called default access level, which does not include any access modifier. Class can be modified only with public modifier or use default access level. (Private and protected not allowed) .If class A need to access members(variables or methods) in class B, class B must be accessible from class A.

                                          Private Access Modifier
  • Private access modifier restricts the accessibility to the class level.
  • It can be applied to member variables and methods.(both static and instance)
  • Constructors can be modified with private.
  • Classes can NOT be applied with private.
  • Method local variables can NOT be modified with private.
                void m(private int x){} //CE
  • Private variables and methods do NOT inherit to sub classes.
                                     Protected Access Modifier
  • Protected modifier is visible to all the class in the same package and to the sub classes outside the package.(Package+Kids)
  • Can be applied to variables(static/instance) and methods (static/instance)
  • Constructors can be modified with protected.
  • Can NOT be applied to top level classes.
  • Can NOT be applied to method local variables.
  • Accessing a protected member in a different package can be done only through inheritance.

     Other modifier applicable for class other than access modifier.
  1. abstract
  2. final
  3. strictfp
                                  Abstract classes and methods
  • "abstract" class can NOT instantiate (can not create objects out of abstract classes)
  • Only classes and methods can be marked as abstract.
  • "abstract" methods do not have a body.
                   public abstract void m(int x);
  • If a class has one or more abstract methods that class MUST be abstract.
  • "abstract" class can have both abstract and non abstract methods.
  • Even without a single abstract method, class can be marked as abstract.
                               abstract class A
                                {
                                          void m(){}
                                }
  • When an abstract method inherited down we must OVERRIDE  the abstract method in the first concrete (non abstract) subclass. If not CE(Compile Error).

Show Part 1



No comments:

Post a Comment