Becoming Sanjuro

Fail again. Fail better.

A Note on Overriding in Java

Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.

Can We Override Static Methods in Java?

Let’s try the following Java program to find out the answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Base {
public static void foo() {
System.out.println("Base foo");
}
}
class Derived extends Base {
public static void foo() {
System.out.println("Derived foo");
}
}
public class Main {
public static void main(String[] args) {
Base obj = new Derived();
obj.foo();
}
}

Output:

1
Base foo

From the output, we know that the method according to the type of reference, instead of the one according to the object being referred, is called, which means it’s decided at compile time, instead of run time. So we can conclude that we can’t override static methods in Java.

Can We Override Final Methods in Java?

We can use final specifier to indicate that a method can’t be overridden by subclasses. Definitely the answer to the question is NO. Let’s try the following Java program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Base {
public final void foo() {
System.out.println("Base foo");
}
}
class Derived extends Base {
public final void foo() {
System.out.println("Derived foo");
}
}
public class Main {
public static void main(String[] args) {
Base obj = new Derived();
obj.foo();
}
}

Compiler Error:

1
2
3
4
5
Main.java:8: error: foo() in Derived cannot override foo() in Base
public final void foo() {
^
overridden method is final
1 error

Can We Override Private Methods in Java?

According to Bruce Eckel’s Thinking in Java, private methods are implicitly final, which means the answer is still NO. Let’s try the following Java program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Base {
private void foo() {
System.out.println("Base foo");
}
}
class Derived extends Base {
private void foo() {
System.out.println("Derived foo");
}
}
public class Main {
public static void main(String[] args) {
Base obj = new Derived();
obj.foo();
}
}

Compiler Error:

1
2
3
4
Main.java:16: error: foo() has private access in Base
obj.foo();
^
1 error

A Note on Reservoir Sampling

Introduction

Reservoir sampling is a family of randomized algorithms for randomly choosing a sample of k items from a list S containing n items, where n is either a very large or unknown number. Typically n is large enough that the list doesn’t fit into main memory.

Algorithm

  1. Create an array of size k, and keep the first k items from the list in it.

  2. For the i-th item (where k < i <= n):

    • Generate a random integer j from 1 to i.

    • If 1 <= j <= k, replace the j-th item in the array with the new item.

    • Otherwise, ignore the new item.

Proof of the correctness by induction:

  1. Base case: when n = k, all items are in the array, which is correct.

  2. Inductive step:

    • Assume that when n = i (where i > k), each item is kept in the array with probability of k / i.

    • Then when n = i + 1, the new item would be kept in the array with probability of k / (i + 1).

      Meanwhile, for the first i items, each of them would be kept in the array with probability of (k / i) (1 - (k / (i + 1)) (1 / k)), i.e. k / (i + 1).

      Note that:

      k / i is the probability that one of the first i items was kept in the array before the current step according to the assumption.

      k / (i + 1) is the probability that the new item would be kept in the array.

      1 / k is the probability of choosing an item from the array.

      1 - (k / (i + 1)) * (1 / k) is the probability that an item in the array would not be replaced.

How to Create a Blog with Hexo and GitHub Pages on macOS Sierra

I finally created my own blog last week. Because I had been learning Node.js recently, I wanted to try to use a platform built on it. First I found Ghost. But it seemed hard to create a Ghost blog with GitHub Pages. Then I found Hexo. It looked like exactly what I wanted. Simple and stylish.

Although there are so many tutorials of setting up Hexo and GitHub Pages, I think it is better to write down how I create my blog, and I hope it will help you.

Prerequisites

Before installing Hexo, Node.js and Git should be installed on your Mac. Please follow setup guides on their official websites.

Installing and Initializing Hexo

Now, Hexo can be installed with npm.

1
$ sudo npm install -g hexo

Then, initialize Hexo. Here BLOG is the directory for your blog.

1
$ hexo init BLOG

Next, move to the directory.

1
$ cd BLOG

Then, run the installation command.

1
$ npm install

Next, start the server.

1
$ hexo server

Now, open http://localhost:4000 in your browser.

Hello World!

Hosting on Your GitHub Account

Now, create a new repository named YOURUSERNAME.github.io on GitHub, replaceing YOURUSERNAME with your GitHub username.

Then, add lines into _config.yml in BLOG.

1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repository: https://github.com/YOURUSERNAME/YOURUSERNAME.github.io.git
branch: master

Next, generate static files.

1
$ hexo generate

Then, deploy your blog.

1
$ hexo deploy

If you meet ERROR Deployer not found: git, run the command below.

1
$ npm install hexo-deployer-git --save

Then, run generate and deploy commands again.

1
2
$ hexo generate
$ hexo deploy

Now, open http://YOURUSERNAME.github.io in your browser. For me, it is http://dryear.github.io

Hello World!

Configuring Your Own Domain

I bought my own domain from GoDaddy, and I chose DNSPod as my DNS hosting provider because of my friends in China.

After setting up on both GoDaddy and DNSPod, create a file named CNAME in BLOG/themes/YOURTHEMENAME/source, add your own domain in it. For me, it is dryear.me.

Then, run the commands below to deploy again.

1
2
3
$ hexo clean
$ hexo generate
$ hexo deploy

Now, open your own domain in your browser.

Hello World!