src :

1 Java Modifiers

1.1 Access Modifiers − default, public , protected, private

Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are −

  • Visible to the package, the default. No modifiers are needed.
  • Visible to the class only (private).
  • Visible to the world (public).
  • Visible to the package and all subclasses (protected).

The fields in an interface are implicitly public static final and the methods in an interface are by default public.

Access Control and Inheritance The following rules for inherited methods are enforced

  • Methods declared public in a superclass also must be public in all subclasses.
  • Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.
  • Methods declared private are not inherited at all, so there is no rule for them.

1.2 Non-access Modifiers − final, abstract, strict

1.2.1 Static

  • Static variable : The static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class
  • Static Method : The static keyword is used to create methods that will exist independently of any instances created for the class.

1.2.2 Final

  • A final variable can be explicitly initialized only once.
  • A final method cannot be overridden by any subclasses.
  • The main purpose of using a class being declared as final is to prevent the class from being subclassed

1.2.3 Abtract

  • An abstract class can never be instantiated. If a class is declared as abstract then the sole purpose is for the class to be extended.
  • An abstract method is a method declared without any implementation. The methods body (implementation) is provided by the subclass. Abstract methods can never be final or strict.
  • Any class that extends an abstract class must implement all the abstract methods of the super class, unless the subclass is also an abstract class.

1.2.4 The Synchronized Modifier

The synchronized keyword used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier can be applied with any of the four access level modifiers.

1.2.5 The Transient Modifier

An instance variable is marked transient to indicate the JVM to skip the particular variable when serializing the object containing it

1.2.6 The Volatile Modifier

The volatile modifier is used to let the JVM know that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory.

Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.

public class MyRunnable implements Runnable {
   private volatile boolean active;

   public void run() {
      active = true;
      while (active) {   // line 1
         // some code here

   public void stop() {
      active = false;   // line 2

Usually, run() is called in one thread (the one you start using the Runnable), and stop() is called from another thread. If in line 1, the cached value of active is used, the loop may not stop when you set active to false in line 2. That’s when you want to use volatile.

2. Java Variables Following are the types of variables in Java −

  • Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
  • Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. The instance variables are visible for all methods, constructors and block in the class. Normally, it is recommended to make these variables private (access level). However, visibility for subclasses can be given for these variables with the use of access modifiers.
  • Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.

3. Data Types

Java Arrays Arrays are objects that store multiple variables of the same type. However, an array itself is an object on the heap. We will look into how to declare, construct, and initialize in the upcoming chapters.

Java Enums Enums were introduced in Java 5.0. Enums restrict a variable to have one of only a few predefined values. The values in this enumerated list are called enums.

Interfaces In Java language, an interface can be defined as a contract between objects on how to communicate with each other. Interfaces play a vital role when it comes to the concept of inheritance.

An interface defines the methods, a deriving class (subclass) should use. But the implementation of the methods is totally up to the subclass.

4. Exceptions

4.1 Throws

In order to see an exception we need to specify just after the signature the Type of Exceptions we would like to see(ie throw)

public static void main(String args[]) throws FileNotFoundException{

Otherwise you will get the following message: error: unreported exception FileNotFoundException; must be caught or declared to be thrown

And in thos case, the return value of your application would be different than 0 and your application will stop running once it throws the error.

Exception in thread "main" input.txt (Aucun fichier ou dossier de ce type)
	at Method)
	at HelloWorld.main(

Process finished with exit code 1

4.2 Try/Catch

Instead of using throws, you may use the try/catch to manage what you would like to do in case an arror occurs. You may for instance do some actions or call one of the Exception methods (printStackTrace, getCause…)

        }catch(IOException e){

And in this case the return value of your method would be 0. input.txt (Aucun fichier ou dossier de ce type)
Process finished with exit code 0

4.3 Throws vs Try/Catch

You do not need to use both. In case your methos manages the Exception in its code with try/catch, you do not need to use throws

The most dangerous thing you can do is catch an exception and not do anything with it.

4.3 Throw

Java throw keyword is used to explicitly throw an exception. Whereas Java throws keyword is used to declare an exception.

5 Fundamental OOP concepts

5.1 Encapsulation

5.2 Inheritance

5.2.1 Definition

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.

The super keyword: The super keyword allows to call a super-class method inside the the sub-class definition.

With the use of the extends keyword, the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.

5.2.2 Override

In the previous chapter, we talked about superclasses and subclasses. If a class inherits a method from its superclass, then there is a chance to override the method provided that it is not marked final. The benefit of overriding is: ability to define a behavior that’s specific to the subclass type, which means a subclass can implement a parent class method based on its requirement. In object-oriented terms, overriding means to override the functionality of an existing method.

5.3 Polymorphism

5.4 Abstraction

  • If a class is declared abstract, it cannot be instantiated.
  • To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
  • If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

5.5 Interfaces

An interface is similar to a class in the following ways:

  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The byte code of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including:

  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.

Interfaces have the following properties −

  • An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
  • Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
  • Methods in an interface are implicitly public.

5.6 Abstract Classes vs Interfaces

A class may implement many interfaces but cannot inherit from many classes. This is the main difference between a fully abstract class with 100% of abstract methods and an interface.

More details about why Java doesn’t handle multi-heritence

comments powered by Disqus