Java interview questions

Java language is developed using object oriented programming (OOP) concept as its platform. This object oriented programming language concept organizes data into different classes, providing the data proper hierarchy through inheritance mode. There are four basic principles defined under the OOP concept in Java which is very popular among interviewers during technical recruitment for Java developer posts. These four basic concepts consist of major portions of Java interview questions in technical interviews as the recruiter is interested in knowing whether the candidate is familiar with the basic concepts of OOP language or not. So do read all the 4 basic OOP concepts described here to crack Java interview questions.

1. Abstraction Principle: This principle gives unique characteristics to all the objects present in a class so as to group similar objects in one class as well as defining the boundary of each class on the basis of objects distinct characteristics. The abstraction feature is used by classes to give distinct attributes to data such as weight, cost, size and methods or functions to work on these particular attributes. In the abstraction principle, a user can only utilize the methods provided because these methods work on some particular interface to complete the task properly and in a particular order.

2. Encapsulation Principle: This principle combines the member data as well as the member function into a single unit so that the basic property of data gets hidden, providing security to user data. The encapsulation principal prohibits the access of data by a third party or outside member which further secures the data. Java Tutorial The basic difference between the abstraction & encapsulation principles is that the former gives access to a third party for data viewing while latter does not have this facility due to security reasons.

3. Inheritance Principle: This principle provides the mechanism of sharing attributes of classes and various operations present along with them. Thus by using inheritance principle an object of one class can acquire the property of another class falling under the same hierarchy. While categorizing the classes using the inheritance principle, the old class is referred as the parent class, base class or super class, while the newly formed class is referred to as the child class, subclass or derived class.

4. Polymorphism Principle: This principle becomes handy if a user wants to use a single operation under different names for a similar kind of operation. For example, if you want to draw a triangle, a square and a rectangle then you can use the draw operation on these three objects by substituting the object name with the draw operation on all the three different objects thus saving both the data space as well as simplifying the program execution. This feature of Polymorphism helps in function as well as operation overloading while dealing with bulky and large data.

Java Strings – 

In this article we’ll look at a collection of Java String programming tips. The Java String class is one of the most commonly-used classes, so it’s well worth knowing how to perform some of the most common operations.

Testing String equality

The first important thing to know about working with Java strings is how to compare two strings to each other. Unlike comparing primitives in Java, you cannot compare two Java strings like this:

 

if (s1 == s2) { // do something here }

 

Actually, you can do that, but it would be wrong. The correct way to compare two Java strings is to use the String equals method, like this:

 

if (s1.equals(s2)) { // do something here } Finding one string in another string

 

A second common String operation is to search one string to see if it contains some other string. For instance, assume you had a string named toppings, and you wanted to see if that string contained the string “pepperoni”. A simple way to do that is to use the indexOf() method, like this:

 

if (toppings.indexOf(“pepperoni”) >= 0) { // your logic here }

 

As a precaution, if you don’t know the case of the string you’re comparing to, you’re always better off converting that string to upper- or lower-case, like this:

 

if (toppings.toLowerCase().indexOf(“pepperoni”) >= 0) { // your logic here }

 

One final way to do this, which is really very powerful, is to use the matches() method of the Java String class, like this:

 

if (toppings.matches(“.*pepperoni.*”)) System.out.println(“found a match”);

 

Note that with this method you actually need to pass in a regular expression, that’s why the string in between the parentheses looks like this:

 

“.*pepperoni.*”

 

instead of this, which would not work: