Week 0. Java: basics.


Credits: Ali



Classes and objects

Definitions

Java is object-oriented programming language. It means that you can represent any entities in terms of objects.

Class is a prototype, which describes what it has and how it behaves. Object, in turn, is a specific instance of a class. For example, we can model person as a class with name and age attributes, and give it specific functions to represent its behavior.

Classes have fields and methods.

Example

Properties

Inheritance

Classes can inherit fields and methods from another class through inheritance mechanism. To inherit from a class, you need to use extends keyword.

Example


Variables

private, protected and public:

Explanations

final and static final

For final, it can be assigned different values at runtime when initialized:

Class Test {
  public final int a;
}

Test t1  = new Test();
t1.a = 10;
Test t2  = new Test();
t2.a = 20; //fixed

Thus each instance has different value of field a.

For static final, all instances share the same value, and can’t be altered after first initialized:

Class TestStatic {
      public static final int a;
}

Test t1  = new Test();
t1.a = 10;
Test t2  = new Test();
t1.a = 20;   // ERROR, CAN'T BE ALTERED AFTER THE FIRST INITIALIZATION.


Further reading

Object-oriented programming in Java with game example

http://gamecodeschool.com/java/understanding-oop-for-coding-java-games/

Java tutorial on w3schools

https://www.w3schools.com/java/