Chapter 4 : Methods and Encapsulation

1. Method Declaration

Access Modifiers

Java offers four choices of access modifier:

public: The method can be called from any class.

private: The method can only be called from within the same class.

protected: The method can only be called from classes in the same package or subclasses.

Default (Package Private) Access The method can only be called from classes in the same package. This one is tricky because there is no keyword for default access. You simply omit the access modifier.

  • private: Only accessible within the same class
  • default (package private) access: private and other classes in the same package
  • protected: default access and child classes
  • public: protected and classes in the other packages Private


the protected rules apply under two scenarios:

  • A member is used without referring to a variable. In this case, we are taking advantage of inheritance and protected access is allowed.
  • A member is used through a variable. In this case, the rules for the reference type of the variable are what matter. If it is a subclass, protected access is allowed. This works for references to the same class or a subclass.

Optional Specifiers

static

abstract

final

synchronized

native

strictfp

Java allows the optional specifiers to appear before the access modifier.


Varags as parameter

A vararg parameter must be the last element in a method’s parameter list. This implies you are only allowed to have one vararg parameter per method.

When calling a method with a vararg parameter, you have a choice. You can pass in an array, or you can list the elements of the array and let Java create it for you. You can even omit the vararg values in the method call and Java will create an array of length zero for you.

int[] perm = new int[] {1,2,3};
int[] array1 = {1,2,3}; //an anonymous array. It is anonymous because you don’t specify the type and size.
 
public static void walk(int start, int... nums) 
{ System.out.println(nums.length); }
walk(1,{2,3}); // does not compile
walk(1); //create an empty array if no parameters are passed for a vararg
walk(1, null); // throws a NullPointerException
 

Since null isn’t an int, Java treats it as an array reference that happens to be null. It just passes on the null array object to walk. Then the walk() method throws an exception because it tries to determine the length of null.


static

5: Koala k = new Koala();// k is a Koala
6: System.out.println(k.count); 
7: k = null;
8: System.out.println(k.count); // k is still a Koala

Java doesn’t care that k happens to be null. Since we are looking for a static, it doesn’t matter. Remember to look at the reference type for a variable when you see a static method or variable.

static initialization

import static

We imported the asList method on line 2. However, we did not import the Arrays class anywhere. This makes it okay to write asList("one"); but not Arrays.asList("one")

if we created an asList method in our StaticImports class. Java would give it preference over the imported one and the method we coded would be used.



2. Overloading

we are not allowed to overload methods with the same parameter list

public void fly(int[] lengths) { } 

public void fly(int... lengths) { } // DOES NOT COMPILE

Java treats varargs as if they were an array


Java calls the most specific method it can

As accommodating as Java is with trying to find a match, it will only do one conversion


the choice of which overloaded method to call (in other words, the signature of the method) is NOT dynamically decided at runtime.

the reference type (not the object type) determines which overloaded method is invoked


To summarize, which overridden version of the method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type of the argument passed at compile time.



3. Constructor

instantiation

default constructor is only supplied if there are no constructors present. This happens during the compile step.


Constructors can be called only by writing new before the name of the constructor. What we really want is for the first constructor to call the second constructor with two parameters. Java provides a solution: this.

If you choose to call it, the this() call must be the first noncommented statement in the constructor.


constructor chaining

By the time the constructor completes, all final instance variables must have been set.

Order of Initialization

  1. If there is a superclass, initialize it first
  2. Static variable declarations and static initializers in the order they appear in the file.
  3. Instance variable declarations and instance initializers in the order they appear in the file.
  4. The constructor.


4. Encapsulation

  • accessor method or getter
  • mutator method or setter


encapsulation refers to preventing callers from changing the instance variables directly.

Immutability refers to preventing callers from changing the instance variables at all.



5. Lambda Expression

import java.util.function.Predicate;
public interface Predicate<T> {
    boolean test(T t);
}



posted @ 2021-01-18 16:48  Leon-HB  阅读(65)  评论(0)    收藏  举报