0%

Notes while learning Java from Marcus Biel’s tutorials .

enum:

Test Case:

## Note1-enum

enum:

Test Case:

Output in Console:

## Note2-logging

First, import the framwork:

Class:

Test Case:

Output in Console:

## Note3-main method

Class:

Now I can run the class Lesson1 or the main method directly.
But still I can write a test case.
Test Case:

## Note4-exception handling

Write some switch cases to throw an exception:

Test Case

Output in Console:

But it isn’t actually handlling the exception. To handle it, add a try-catch block:

Output in Console:

Now, I start to understand some error messages and maybe even how to resolve some. :)

## Note5-interface

Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship. – Wikipedia

For example, a car and a tank can both drive and stop:

Test Case:

Output in console:

To introduce the interface Behavior:

Then update the classes to implement the interface:

Test Case then can be re-written as follows:

The output will be the same. Now I can iterate different classes in the same for-loop.

## Note6-inheritance

The inheritance seems to be not so recommend, it can be used to avoid duplicated codes as interface but with (maybe) less flexibility.

To inherite it, use extends:

Test Case:

Output in console:

I think I can use interface and inheritance interchangeably, but maybe not.

## Note7-clone method and more

Actually there were three ways of copying (i.e. the ‘more’), see if I can repeat them all.

1. clone method in Object class

a. Good practice:

Output in console:

Why it is good? Because the contains are strings - immutable objects.

b. Not so good but ok practice:
Create a class and override the clone method:

Test Case:

So car1 and car2 are not the same object but the owner is the same. Which shows that car2 is a copy of car1.
But why not so good? Because of the codes I have to wirte to override clone method. Why still ok? Because the contains are still strings, and this is a shallow copy - if the contains are objects they are not copied.

If the contains are objects and I create a shallow copy of it:
Class:

and also change it in Car class

Output in console:

Which shows that the owner is not copied.
So to copy the owner, first to override clone method in Owner class:

then to rewrite clone method in Car class:

Test Case:

Output in console:

It works now, but indeed it is too complicated. It is a good way of practicing though.

2. constructor
To make a copy of itself, create a constructor with itself as the parameter.

Test Case:

But constructors don’t have meaningful names so can be confusing.

3. static factory method

Consider static factory methods instead of constructors. – Joshua Bloch

Static factory method has a distinct method name but the logic and the structer looks the same - create a method with itself as the parameter, and return a new object of its type.

Test Case:

That’s the end of the first learning notes. I will continute with specific topics then.