11:18 pm - Sunday July 15, 2018

Access Modifiers In Java

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

ACCESS MODIFIER:

Public : public class is visible in other package,field is visible everywhere(class must be public too)

Private:private variables or methods may be used only by instance of the same class that declares the variable or method,a private feature may only be accessed by the class that owns the feature.

Protected: Is available to all clasas in the same package and also available to all subclasses of the class that owns the protected feature.

Default: what you get by default ie.without any access modifier(ie,public,privateor protected).It means that it is visible to all within a particular package.
NON ACCESS MODIFIER:

Static:

  • Static means one per class,not one for each object no matter how many instance of a class might exist.this means that you can use them without creating an instance of a class.
  • Static methods are implicitly final,because overriding is done based on the type of the object ,and static methods are attached to a class,not an object.
  • A static method in a superclass can be shadowed by another static method in a subclass,as long as the original method was not declared final.
  • However,you can’t override a static method with a nonstatic method.
  • In other words,you can’t change a static method into an instance method in a subclass.

final:    

  • A final class can’t be extended ie.,final class may not be subclassed.
  • A final method can’t be overridden when its class is inherited.     You can’t change value  of a final variable(is a constant).Abstract:
  • An abstract class is a class designed with implementation gaps for subclasses t fill in and is deliberately incomplete.
    A calss cannot be both final and abstract .
  • abstract methods cannot be private.
  • abstract methods cannot be final.

Static block:

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. Here is an example:


static

{

// whatever code is needed

// for initialization goes here

}

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

There is an alternative to static blocks — you can write a private static method:


class Whatever

{

public static varType myVar =  initializeClassVariable();

private static varType initializeClassVariable() {

// initialization code goes here

}

}

The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.

BACK

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply