Declaring Methods Using Non-access Modifiers

Hi Friends, Last post we discussed about access modifiers. Apart from access modifiers, the non-access modifiers can also be used in declaring  the class members(methods) in some particular situations. The following are Non-access modifiers:

  • final
  • abstract
  • synchronized
  • native
  • Methods with variable Argument List(var-args)

The final Methods and Arguments

In Java programming the final keyword- is used to declare a method or variable which cannot be further changed. Means the final keyword prevents overriding of a method in the subclass. The class members are declared as final, so that a programmer cannot change the behavior of the final method in a new class.

many Java standard library classes are declared as final. For Ex: the System and String classes of the java.lang package are declared as final and, implicitly, their methods are also final.

Here we declares the MySuperClass class with show() method declared as final:


  1. package com.vbbs;
  2. public class MySuperClass {
  3. //declaring the final method
  4. public final void show(){
  5. System.out.println(“Declaring the Final  method”);
  6. }
  7. }

Now,let’s create the MySubClass class, which overrides the show() method.


  1. package com.vbbs;
  2. public class MySubClass extends MySuperClass {
  3. //overriding the final method of the super class
  4. public final void show() {
  5. System.out.println(“overriding the Final method”);
  6. }
  7. }

On compiling the above code, a compilation error occurs as a subclass cannot override  the final method of its superclass.

Now let’s discuss the final arguments, which are provided between the parentheses in a method declaration. like


  1. public Book getBook(String bookName, final int bookId)
  2. {
  3. //providing method body
  4. }

In the above code, the bookId variable is declared as final. It cannot be re-assigned a new value within the getBook() method. while calling getBook() method, the value passed to the bookId argument will remain the same, as the value of final argument cannot be modified.

The Abstract Methods:

An abstract method is an empty-bodied method, which can be declared but not implemented.

In simple words, a method is declared as abstract when it needs to be overridden in its subclasses. For example, we can define an abstract method brakes() in the Car class as each car has different types of brakes. Therefore each subclass of the Car class can implement the abstract method brakes().


  1. package com.vbbs;
  2. public abstract class Car {
  3. //declaring an abstract method
  4. abstract void brakes();
  5. }

In the above code, note that since the brakes() abstract method is defined in the Car class, so the Car class also be declared as abstract. Here only declares the  brakes() abstract method and no implementation or method body is provided within the curly braces. If we declares an abstract method in a class,  like


  1. package com.vbbs;
  2. public class Car {
  3. //declaring an abstract method
  4. abstract void brakes();
  5. }

In the above code, it is not authroized to declare an abstract method in a class which is not explicitly declared abstract. However an abstract class can be declared without an abstract method. Any class that extends an abstract class must implement all abstract methods of the superclass, unless the subclass is also abstract.


Note: A non- abstract class must implement all the abstract methods of a superclass. However, if the subclass is abstract, then it is not mandatory to implement all abstract methods of a superclass.


A method cannot be declared as both abstract and final. The reason behind this is:

  • a final method cannot be overridden in a subclass.
  • an abstract method is always overridden in a subclass, provided that the subclass is not an abstract class.

These two concepts are contradictory therefore, the final and abstract keywords cannot be used together.

Even a method cannot be declared as both abstract and private:

  • A private class member of a superclass cannot be inherited in a subclass
  • An abstract method of a superclass is implemented only in its subclass.

The above two statements are contradictory, the abstract and private method can never be should not be used together.

Synchronized Methods

The synchronized keyword declares the synchronized methods, which needs to be accessed by only one thread at a time. The synchronized modifiers can be applied to methods only and not variables and classes. The following code declares the getBook() synchronized method:


public synchronized Book getBook(String bookName);


In the above code, the synchronized getBook() method is declared as public. It is to be noted that the synchronized method can be declared as public,private,protected, or default access.

Native Methods

The methods declared using the  native modifier implies that the method is implemented in platform-dependent code,often in C.

The native modifier can be used only for methods and not for classes and variables.

The declaration of the native methods ends with a semicolon and they do not have a method body, such as abstract methods.

Methods with var-args

With the introduction of Java 5, the functionality of creating methods that accept variable-length argument lists, was provided. Var-args is a feature that allows a method to accept more than one argument as a parameter.

Some rules for this

  •  While declaring a var-args parameter, the type of argument must be specified.
  • While declaring a method with a var-args parameter, a programmer should follow the type with an ellipsis(…), a space, and then the name of the array, which holds the value of received parameters.
  • The other parameter can also be passed to a method using a var-args list.
  • The var-args parameter must be the last parameter in the method’s signature.
  • There can be only one var-args  parameter in a method.

Some examples of legal var-args declarations:

  • void run(int…i){ }
  • void run(char ch, int…i) { }

some examples of illegal var-args declarations:

  • void run(int i…) { }// wrong syntax
  • void run(int…i,char…ch){ } // more than one var-args list

To understand var-args  clearly we take one example varargsExample class.


  1. package com.vbbs;
  2. public class VarargsExample
  3. {
  4. public static void displayNames(String …names)
  5. {
  6. for(string mynames:names)
  7. {
  8. System.out.println(mynames);
  9. }
  10. }
  11. public static void main(String args[])
  12. {
  13. displayNames(‘Alex”);
  14. displayNames(“Alex”,”Pandey”);
  15. displayNames(“Alex”,”Pandey”,”Jacob”);
  16. }
  17. }

In the above the displayNames() method takes an unspecified number of string objects as input.This is indicated by the use of ellipses in the parameter declaration String… names.

 

Leave a Reply

Your email address will not be published. Required fields are marked *