Project Technical Lead
Java developer Junior job interview FAQ: part 3
In this installment of the series, we continue with the frequently asked questions that may arise in the 1st round of job interviews for the position of Junior Java developer.
See also other articles from the Java developer interview series:
- Junior Java developer interview 1.part
- Junior Java developer interview part 2
- Junior Java developer interview part 4
Inheritance is an important concept in Java. Explain the principle of inheritance.
Inheritance in Java is a concept that allows the creation of new classes based on existing classes. This feature allows you to share properties and methods of one class with other classes. The one from which it is inherited is called the superclass and the one that inherits is called the subclass.
The child class inherits all member variables and methods of the parent class. This way we can reuse the code and create a hierarchy of classes, where each class can have its own methods and variables, but also inherited properties from the parent class.
We apply inheritance in Java using the extends keyword. When declaring a child class, we must specify the parent class from which it inherits. For example:
public class PodradenaTrieda extends NadradenaTrieda {
// kód podradenej triedy
Inheritance allows polymorphism, which means that an object of a child class type can be used as an object of a parent class type. This allows you to simplify the code and improve the flexibility of the program.
Inheritance allows the creation of complex class hierarchies, which are the basis for creating efficient and modular code in Java.
What is polymorphism? List the two forms and explain the difference between them.
Polymorphism in Java is a concept that allows the use of a single name for different types of data or classes. This concept allows us to work with different classes that can be linked together by inheritance.
In Java, we can achieve polymorphism in two ways – by overriding and by overloading methods.
Overriding methods:
Overridding refers to methods that have the same name, parameters, and return type in the parent class and the child class. When a method in a child class is overridden, it will replace the method from the parent class and call this new implementation. This is called compile-time polymorphism.
Overloading methods:
This type of polymorphism is achieved through inheritance and method overriding. When we have a class that inherits from another class, we can override the method of the parent class in the child class. An object of the child class type can be used as an object of the parent class type. Java selects the correct method based on the current object type, not the variable type. This is the so-called runtime polymorphism.
Polymorphism is a useful concept because it allows us to write universal code that can work with different types of objects, which is important in object-oriented programming in Java.
What is the meaning of encapsulation of classes in Java?
Encapsulation in Java is a concept that allows you to hide the internal implementation of a class and provide access to its attributes and methods only through a defined interface. In this way, greater security and control over the classes can be achieved.
It is achieved using access modifiers and get-set methods (getters and setters). Access modifiers determine which parts of the class are visible from other parts of the program. Typical access modifiers are:
public – visible from any place in the program,
private – visible only within the given class,
protected – visible within the given class and its subclasses,
default (without specifying an access modifier): – visible within the package of the class.
To access class attributes that are declared private, we use get-set methods. Get-set methods are used to get the value of an attribute (get) and set the value of an attribute (set). This way we can control access to attributes and prevent invalid values.
Encapsulation is an important concept in object-oriented programming because it allows for better control, security, and flexibility when working with classes and their attributes.
What is the role of the constructor in Java?
In Java, a constructor is a special method in a class that is called when a new object is created from that class. The constructor is used to initialize the object and assign initial values to its attributes.
The main features of the constructor are:
- the name of the constructor must be the same as the name of the class it is in,
- the constructor has no return type or void type,
- is called automatically when a new object of the class is created using the new operator,
- we can define multiple constructors with different parameters (called constructor overloading).
The use of constructors is important when initializing objects and provides a way to ensure that an object has valid values for its attributes as soon as it is created.
What is the purpose of abstraction in Java?
Abstraction in Java is a concept that allows you to hide implementation details and focuses only on the basic properties and behavior of an object. In this way, the complexity of the programming can be simplified and its readability improved.
In Java, we can achieve abstraction using abstract classes and interfaces.
Abstract class:
An abstract class is a class that cannot be directly created as an object, but can contain abstract methods and normal methods. An abstract method is declared but has no implementation, that is, it does not contain a method body. Abstract methods must be implemented in child classes.
Interface:
An interface defines a set of methods that must be implemented by the classes that implement the interface. The interface is essentially abstract and does not implement any methods. Classes can implement multiple interfaces at once.
Abstraction allows us to hide the complexity of the implementation and focus on what the object does and what methods it provides. This achieves better code organization, reuse, and separation of responsibilities between different parts of the program.
If you’re a Java developer looking for a job, read more about Java developer salary and check out our job offers.