
Project Technical Lead
In this instalment of the series, we continue with the most frequently asked questions that may arise during the first round of interviews for the position of Junior Java Developer.
See also other articles in the Java Developer Interview series:
Inheritance in Java is a concept that allows you to create new classes based on existing classes. This feature allows you to share the properties and methods of one class with other classes. The class you inherit from is called the superclass and the class you inherit to is called the subclass.
The subclass inherits all the member variables and methods of the superclass. This allows us to reuse code and create a hierarchy of classes, where each class can have its own methods and variables, but also inherited properties from the superclass.
We use inheritance in Java with the extends keyword. When we declare a subclass, we must specify the superclass 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 subclass type can be used as an object of a superclass type. This allows you to simplify the code and increase 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.
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 through inheritance.
In Java, we can achieve polymorphism in two ways – by overriding methods and by overloading methods.
Overriding methods:
Overriding refers to methods that have the same name, parameters and return type in both the superclass and the subclass. When a method in a subclass is overridden, it replaces the method in the superclass and calls this new implementation. This is known as compile-time polymorphism.
Overloading methods:
This type of polymorphism is achieved through inheritance and method overriding. If we have a class that inherits from another class, we can override the superclass method in the subclass. An object of the type of the subclass can be used as an object of the type of the superclass. Java chooses the correct method based on the current object type, not the variable type. This is known as 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.
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. This provides greater security and control over the classes.
This is achieved using access modifiers and getter and setter methods. Access modifiers determine which parts of the class are visible from other parts of the program. Typical access modifiers are:
public – visible from anywhere in the program,
private – visible only within the given class,
protected – visible within the given class and its subclasses,
default (no access modifier specified): – visible within the class package.
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 allows us to control access to attributes and prevent invalid values.
Encapsulation is an important concept in object-oriented programming because it provides greater control, security and flexibility when working with classes and their attributes.
In Java, a constructor is a special method in a class that is called when a new object of that class is created. The constructor is used to initialize the object and assign initial values to its attributes.
The main features of the constructor are:
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.
Abstraction in Java is a concept that allows you to hide implementation details and focuses only on the basic properties and behaviour of an object. In this way, the complexity of the programming can be simplified and its readability improved.
In Java, we can achieve abstraction by using abstract classes and interfaces.
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 subclasses.
An interface defines a set of methods that must be implemented by the classes that implement it. 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 leads to 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!