Interviews can be tricky, there is a chance that one might get disqualified if he/she is not well prepared for the interview. If it is a Java interview, the Important Java Concepts to Prepare Before Giving Interview should be well prepared.
It is for a person giving interview to know that the interviewer tries to know that how much understanding the interviewee has about the Java concepts, so simply mugging up the definitions will get you into trouble. It is vital for you to convince the interviewer that you are well-versed with the concepts and that you understand how the Java concepts are related to real life.
Also Read: What to do after learning Basic Java?
Therefore, it is a good practice to give examples and explain the concepts in your own words instead of book written definition. If you prepare a Java code as an example of the important Java concepts, it will be easier for you to convince the interviewer. It will also help you have a complete control over the Java concepts.
The Java Concepts to Prepare Before Giving Interviews are given below and also the probable answers you can give regarding those Java concepts are given.
Inheritance
Inheritance is a Java concept of one object acquiring the properties of another object. This is important because it supports the concept of hierarchical classification.
Hierarchical classifications makes most of the knowledge manageable. A suitable example for this concept will be a Java class of Dog which inherits from a Java class Mammal which in turn is inherited from superclass Animal. Therefore, Animal is the superclass and Mammal is the subclass while Mammal is the superclass of Dog. Let’s dig deeper into the example.
The common properties of Mammal is that it can eat, sleep, etc. These are behavioral aspects. The abstract attributes are size, type of skeletal, intelligence, etc. These attributes and behavior classifies it as a single class Animal. Mammals can be defined as more specific class of Animals, mammals have specific set of attributes such as mammary glands and type of teeth.
This is called the subclass of Animals and superclass of Mammals will be Animals. This is the basic concept of Inheritance. Inheritance is one of the important concepts of Java to prepare before giving interviews.
Method Overriding
Java’s concept of Method Overriding is generally asked after the Java’s concept of inheritance. Start off with a good programming example in Java for the concept and then give the definition which is: In inheritance, when a method in a subclass has the same name and signature as the superclass, then the method in the subclassoverrides the method in superclass. Method Overriding is an important Java concept to prepare before giving interviews.
Polymorphism
Polymorphism is an important Java concept that defines the usage of one interface for a general class of actions. Start off with giving a suitable example for this Java concept, here the same Animal example has been continued but you are free to use another example for the interview. For the concept of Polymorphism, two classes have been added to the example. Those areCow and Cat who overrides eat().
Animal[] animals = newAnimal[3];animals[0] = newHuman();animals[1] = newCow();animals[2] = newCat();animals[0].eat(); //Human’s eat()is invokedanimals[1].eat(); //Cow’s eat()is invokedanimals[2].eat(); //Cat’s eat()is invoked |
In the concept of polymorphism, the superclass of the actual object type can be the reference type. This implies that the same Animal array behaves as Human, Cow and Cat. This example justifiesfor the concept of Polymorphism which is in turn an important Java concept to prepare before giving interviews.
Objects
Object is an identifiable entity with specific state and behavior. A real world example for this concept of Java would be, several objects are found around us such as birds, vehicles, human beings, etc. All of the mentioned objects consists unique state and behavior.
The state of a bird can be defined as its name, color, etc. while its behavior would be flying, chirping, etc. Objects is an important Java concept to be prepared before interviews because it gives an idea of Object-Oriented programming.
Classes
Classes are blueprints of Objects through which individual Objects are developed. A Java class contains three types of variables that are local variables, instance variables and class variables. Follow up with a Java program example i.e.
public class Bird {
String name;
intage;
String color;
void chirping() {
}
void flying() {
}
}
A Java class can contain several methods, here in this example, chirping( ) and flying( ) are methods. The interviewer needs to be convinced that you have knowledge about the programming part too.
Abstraction
Abstraction is an important Java concept of object-oriented programming. Abstraction is the concept of hiding the details and only showing the essential details. Abstraction has many real life examples that you can give in an interview.
A good example can be of a switch board where all the details or wirings are hidden only the essential thing that is a switch is shown which is enough to switch on or off a fan, tubelight, etc. another real life example can be that of a car.
If we see a car from outside, a car is a single object but from inside you have seat belts, brakes, steering wheel, etc. if you dig deeper, you find a car to be a very complex object with several thousand parts but the complexity is always ignored which is how the internal mechanisms work together.
Instead, the only concern is to how to handle the steering, brakes, clutch, etc. similarly several methods are used in a Java program which are already defined and are used without knowing their actual functions. This is a Java concept of Abstraction.
Encapsulation
Encapsulation is wrapping up of data into a single unit. Encapsulation can also be defined as a wrapper that protects the Java code and data from being arbitrarily accessed by a different Java code that is defined outside the wrapper.
An example that will be well suited for interviews will be again the car example. The car example explains encapsulation as the gear shift lever enables us to use transmission. It can be considered as an interface to transmission. Simply the shifting of gears affects the transmission. Also, only the transmission is affected by the act of shifting of gears.
Similarly Java programs also contain a class that is defined in such a way so that they are wrapped or encapsulated. The classes outside the class can only access the class through methods and therefore are unable to arbitrarily alter the values of the instance variables. This is the concept of Encapsulation. It is an important Java concept to prepare before giving interviews.
Garbage Collection
In Java, the newoperator is used to dynamically allocate memory space to Java Objects. After sometime the objects are destroyed when no longer in use. Java has an automatic mechanism for this purpose which is known as Garbage Collection. Java automatically deallocates the objects that are no longer in use.
When no reference exists for a particular object, then that particular object is considered to be no longer used and the memory occupied by that object can be reclaimed. Java’s Garbage collection occurs sporadically when a program is executed, it does not occur for simply a non-referenced object. For example:
Dog d = newDog(); //allocates memory space to a new Dog object (suppose Object 1)d = newDog(); //allocates memory to another Dog object (suppose Object 2) |
After the creation of Object 2, no reference is exists for Object 1. Therefore, Object 1 is ready for Garbage Collection. Thus, Java’s Garbage Collection runs and removes Object 1 when the system is low on memory.
Interfaces And Abstract Class
The concept of Interfaces and Abstract Classes in Java can be explained with the help of an example. Suppose there is an Animal class with two subclasses Bat( ) and Human( ). The methods defined in Animal class are sleep( ), eat( ), both the subclasses have the same methods (i.e. sleep( ) and eat( )) with a few others too.
Therefore, the two methods are inherited in the Bat and Human class but the way of sleeping is different for both bats and humans. In contrast to human, bat sleeps upside down. Hence, it is not required for the sleep( ) method to be defined in the Animal class itself. Thus, the sleep( ) method can be made abstract.
The methods with no body are the Abstract methods. A Java class containing an abstract method needs to be defined abstract too. Here, the Animal class is also be made abstract along with sleep( ). Instance of an abstract class cannot be created.
abstractpublicclassAnimal {publicabstractvoidsleep();publicvoideat() {//statements}} |
In interfaces, all the methods are abstract i.e. a class that is completely abstract is an interface.
publicinterfaceAnimal {
publicabstractvoidsleep();
publicabstractvoideat();
}
This is an example for explaining the Java concept of interface. The few differences between them is that interfaces can be instantiated while abstract classes cannot. The other difference is that an abstract class extends while an interface implements. Only one class be extended in Java while Java provides implementation of multiple interfaces.
Exception Handling
Exception handling mechanism of Java is the best. Majority of errors are caught in Java. Errors like division by zero cannot be handled by mother programming languages such as C/C++. A simple error like this makes the entire system down if C/C++ is used; this is easily handled in Java.Java uses the term‘exception’ for a problem while the try/catch block defines the way the exception is being handled. This is the basic concept ofException Handling in Java.
TRY block :
A code which has the probability of generating an exception is kept inside the try block. The rest is taken care of by Try.
CATCH block :
An exception that is thrown away is caught in the catch block. Generally, the error message is printed in the catch block so that user can be notified regarding the possible exception. For example, if the file that is required to be accessed and is missing. The error message printed is FileNotFoundException.
FINALLY block :
When an exception occurs, then the lines following in the try block are not executed. In some cases we might want to execute certain statements irrespective of the outcome, then those statements are kept in the finally block. The statements inside the finally block are executed irrespective of the outcome.
In an interview, a suitable example can be given with this. Therefore, this explains the basic understanding of Java’ exception handling mechanism which is an important Java concept to prepare before giving interviews.
These were a few Java concepts that are important for interviews. Hence. These are the Java concepts that you should prepare before giving interviews.