Access modifier and optional modifier

When define a method in a class, we’ll need to define a method access modifier, optional modifier, return type and name.

The sequence of access modifier and optional modifier can be in order or not in order.

  • Access modifier : public / protected / private / default
  • Optional modifier : final / static / abstract
public class Test() {

  public static final void method1() { }
  public final static void method2() { }
  public final void method3() {}
	
  final public void method4() {} 
  final static void method5() {} // default modifier
	
  static public void method6() {}
  static final public void method7() {}
}

Access modifier

  • public – class / package / subclass / world
  • protected – class / package / subclass
  • default – class / package
  • private – class

Optional modifier

  • static
  • final
  • abstract

Static variable and static method

When define a static variable, this static variable does not belong to any object. Static method are not invoke on object, although you can do it but not recommended and IDE shall warn you. Example, Arrays.asList

@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
}

List<String> list = Arrays.asList("a","b");

Static Initialization & Static Block

Static block will always get called in highest priority. You can have multiple static block in a class, but best practices shall be use one in a class to avoid confusion. Static initialization is done inside static block.

public class StaticInitialization {

  private static final int FIXED_AMOUNT;
  private static int FIXED_DEPOSIT;	
  
  static {
    FIXED_DEPOSIT = 200;
  }

  static {
     FIXED_AMOUNT = 100;
  }
	
  public static void main(String[] args) {
     System.out.println(StaticInitialization.FIXED_AMOUNT);
     System.out.println(StaticInitialization.FIXED_DEPOSIT);
  }
}

Abstract

  • Cannot be instantiated directly.
  • Abstract class/methods cannot be private or final.
  • Allow inherits to another Abstract class.
  • Class extends abstract class must implement it’s inherits abstract methods.
  • Abstract method only declare without implementation.
  • Class extend abstract class will override the methods.
  • abstract and static or final keywords cannot live at the same time.
  • Abstract class can have static method and implementation.
  • Abstract class implement an interface does not need to include body, but the concrete class extended must implement it. Eg
interface Shape {
   abstract int getShape();
}

/* Circle abstract class implements Shape interface
 * does not require to implement getShape() method.
 */
abstract class Circle implements Shape {
   abstract int getDimension();
}

/* ShapeTest class extends Circle must include 
 * Shape interface's body methods getShape().
 */
public class ShapeTest extends Circle {

   @Override
   public int getShape() {
      return 0;
   }

   @Override
   int getDimension() {
      return 0;
   }
}

Final

method with final cannot be override. Example:

package com.packages.two;

public class Printer {

	public Printer() {
		System.out.println("Printer");
	}
	
	public final void print(String name) {
		System.out.println("printing name " + name);
	}
}


public class HpPrinter extends Printer {
	
	public HpPrinter() {
		System.err.println("HP Printer");
	}
	
	@Override
	public void print(String name) {  //compiler complain cannot override final method
		System.out.println("Override HP Printer");
	}

}

Overloading vs Override

Overloading:

  • Same method name, but different parameters, or different return type in one class
  • Allow different access modifier

Override:

  • Same method name, same parameters, and same return type
  • Allow override higher access modifier. Eg protected to public
  • Only implementation is different

Interface

  • variables constant – by default public static final
  • method declaration – by default public and abstract
  • support static method, Need supply body, and default to public
  • static method does not inherit from parent interface.

Interface default method

  • Interface’s default method only defined in interface
  • default method marked with default keywords
  • default method must provides implementations
  • default method not support protected or private
  • can be override in class that implement interface

Access modifier and optional modifier

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.