Saturday, 9 June 2018

Chaudhari Karan

Java – Modifier Types

    No comments:


Java – Modifier Types

Hello friends, In this lecture, we will discuss Java Modifier types.
Java – Modifier Types


There are two types of Modifier.
  • Java Access Modifiers
  • Non Access Modifiers

Java Access Modifiers

Java has a number of access modifiers. That modifier set access levels for classes, variables, methods, and constructors. Here are four access levels:
  • It is visible to the package, the default. No modifiers are needed.
  • Visible to the private class only.
  • Visible to the public world.
  • Visible to the package and all protected subclasses.

Default Access Modifier - No Keyword

It means do not explicitly declare an access modifier for the class, field, method, etc.

A variable and method declared without access modifier. It is available to other class in the same package. The fields in the interface are implicitly public static final and the methods in the interface are by default public.

Example:
Variable and method declared without any access modifier.
Default Access Modifier - No Keyword


Private Access Modifier - Private

Methods, variables, and constructors are declared as private. It can access the declared class itself.

Private access modifier is the restrictive access level modifier.

Class and interfaces are not private.

Variables are declared as private. It can access outside the class if public methods are present in the class.

The private modifier is the object encapsulates itself and hides data from the outside world.

Example:
Java Private Access Modifier - Private


Here, the format variable is private in Example class, but there is no way for other classes to retrieve or set its value directly.

This variable available is the outside world, here is defined two public methods: getFormat(), which returns the value of format, and setFormat(String format), which sets its value.

Public Access Modifier – Public

Any class, method, constructor, interface, etc. declared as public. It can be accessed from other classes. But fields, methods, and blocks declared inside the public class which accessed from any class.

If the public class is trying to access in the different package.

Because of class inheritance method, all public methods and variables of the class are inherited by subclasses.

Example:

Java Public Access Modifier – Public


The main() method of an application is public. Otherwise, it not called by a Java interpreter to run the class.

Protected Access Modifier - Protected

Variables, methods, and constructors,  declared as protected in the superclass. It accessed by the subclasses in other package or class with the package of the protected members' class.

The protected access modifier is not applied to the class and interfaces. Methods, fields are declared as protected, but methods and fields in the interface are not declared protected.

Protected access the subclass to use the method or variable while preventing the nonrelated class.

Example:
Java Protected Access Modifier - Protected

Access Control and Inheritance

Here is rules for inherited methods are enforced:
  • Methods are declared public in the superclass and must be public in all subclasses. 
  • Methods are declared protected in the superclass and must be protected or public in subclasses; it cannot be private. 
  • Methods are declared private are not inherited.

Java Non-Access Modifiers

Java has a number of non-access modifiers to achieve other functionalities.
  • The static modifier is used for creating variables and class methods. 
  • The final modifier is used for finalizing the implementations of methods, classes, and variables. 
  • The abstract modifier is used for creating abstract classes and methods. 
  • The synchronized and volatile modifiers are used for threads.

The Static Modifier

Static Variables

The static variable is used as the static keyword for creating variables. It exists independently of any instances created for the class. The static variable exists regardless of the number of instances of the class.

Static variables are also called as class variables. But,  local variables are not declared as static.



Static Methods

The static keyword is used to creating static methods. It will independently of any instances created for the class.

Static methods do not use instance variables of an object of the class. Static methods take all the data from parameters and compute that parameter, without reference to variables.

Class variables and methods are accessed by the class name.

Example:
Java Static Methods

Output:
Java Static Methods

The Final Modifier

Final Variables

A final variable is explicitly initialized only once. The reference variable declared as final. It cannot reassign to the different object.

The data with the object it can be changed. The state of the object changed but cannot the reference.
The final modifier with variables is used with static to make as the constant the class variable.

Example:
Java Final Variables

Final Methods

 A final method cannot override by any other subclasses.

The main intention of the final method is that the content of the method not be changed by an outsider.

Example:
Java Final Methods

Final Classes

The main purpose of the final class is declared as final to prevent the class from subclassing. If the class is marked as final then the class cannot inherit any feature from the final class.

Example:
Java Final Classes

The Abstract Modifier

Abstract Class

An abstract class never instantiated. If any class is declared as abstract then the class to be extended.
A class cannot be abstract and final. If the class contains abstract methods then the class declared as abstract. Otherwise, a compile give the error.

An abstract class contains both abstract methods and normal methods.

Example:
Java Abstract Class


Abstract Methods

An abstract method is a method declared without any implementation. Abstract methods cannot be final or strict.

If any class extends an abstract class it implements all the abstract methods of the superclass, unless the subclass an abstract class.

If any class contains one or more abstract methods, and the class declared as abstract. An abstract class does not need to contain abstract methods.

An abstract method ends with the semicolon.

Example: public abstract sample();

Java Abstract Methods


The Synchronized Modifier

The synchronized keyword is used to indicate a method can access by only one thread. The synchronized modifier applied with four access level modifiers.

Example:
Java Synchronized Modifier

The Transient Modifier

An instance variable is transient to indicate the JVM to skip the particular variable when it serializing the object containing.

The modifier is included in the statement which creates the variable, preceding the class or data type of the variable.   

Example:
Java Transient Modifier

The Volatile Modifier

The volatile modifier is used to the JVM, that a thread accessing the variable.

Volatile only applied to the instance variables, which are of type object or private. A volatile object reference is null.

Example:
Java Volatile Modifier


run() method is called in one thread, and stop() method is called from another thread.
To use a modifier, need to include its volatile keyword in the definition of a class, method, or variable.
Java Volatile Modifier

Access Control Modifiers

Java has the number of access modifiers to access levels for classes, variables, methods, and constructors. Here is four access level:
  • It is visible to the package, the default. No modifiers are needed.
  • Visible to the private class only.
  • Visible to the public.
  • Visible to the package and all protected subclasses.


Subscribe to this Blog via Email :