This article, written in a ‘questions and answers’ form, is intended for end-of-course students, junior developers as well as enthusiasts who plan on undergoing technical Java interview in the near future.
The questions being dealt with are those most often asked in interviews. This will help you in avoiding minor errors or making mistakes during your next technical interview. Furthermore, the explanations provided will allow you to deepen your previously acquired Java knowledge as well as being able to explain it to others.
I. The concept of object-oriented programming
In contrast to procedural programming, which is based on instruction and logic sets, object-oriented programming is a programming style that revolves around objects, which contain data and mechanisms.
- Translates a complex universe into small, simple and autonomous sets (Objects),
- Allows us to secure our data by defining access and modification rules,
- Allows several people to work on the same project composed of several bricks.
II. Class & Object
1. What is a class in Java?
A “class” is a set of attributes and methods (functions). In addition, it represents all the characteristics and mechanisms of a real world concept.
A. what is use of a constructor?
A constructor is a method used in creating a new instance of a class. It always bears the name of the class in which it is defined and is used to initialize attributes either with values passed as parameters, or with default values.
Its definition is not mandatory and we can define it, in the same class, as much as we want with different signatures.
B. What differences a constructor from a method?
A method (or function) in OOP is a feature attached to a real-world entity concept or simply a routine defined in a utility class.
Literally, a constructor is a method but it must be specified that a constructor:
- Must take the name of the class in which it is defined,
- Does not have a “return type” and returns “no value”,
- Always exists in a class. In the case of constructor absence, there is the default one,
- Cannot be called directly. They are implicitly called while instantiating an object with the keyword ‘new’.
2. What does and object represent in Java?
An object is a container that contains a set of data with built-in mechanisms to manipulate it.
3. What is the difference (or relation) between a class and an object?
It is important to distinguish the relationship and the differences between a class and an object:
- An object is an instance of a class. On the other hand, a class is defined as a ‘Template’ model matching an element in the real world.
- Normally, we should talk about objects only at programs execution.
- An object is created using the keyword ‘new’ using a constructor while a class is defined with the keyword ‘class’.
- During the execution, we can have several instances (objects) of a class, whereas the class concerned is declared only once.
- The creation of an object requires memory allocation, however it is not the case for a class.
III. Encapsulation and legacy
1. What is encapsultion?
Encapsulation consists of defining the visibility and accessibility of the properties and methods of a class to better control their use.
2. Levels of access in Java
In Java, there are four levels of access: public, protected private and package (undefined / default access).
3. What does inheritance in oop mean and what is its use?
Inheritance consists in defining a class by extension of the mechanisms and attributes of another class. In addition, for a given set of real world entities, this consists of grouping their common characteristics into a parent class (superclass) from which classes (subclass) corresponding to these entities will be derived.
Keep in mind:
- In Java, multiple inheritance is not supported. t is impossible to inherit two or more classes. If such a need arises, interfaces could be used.
- The inherance is made thanks to the keyword ‘extends’.
- To call a superclass constructor from a subclass, we use the keyword “super“.
- Inheritance makes it possible to avoid repeating a code several times and avoids modifying a code already existing in a base class:
- For example: Class Point → NamedPoint: a named point in addition to its coordinates.
- It is strongly recommended to make “protected” the attributes of the superclass, which can be used in the subclasses,
- There are non-inheritable classes in Java, for example, the String class.