An in-depth guide to mastering basic Object-Oriented Programming (OOP) concepts in Java with FAQs and interview questions.

4.How would you define a class in Java? Give an example.
In Java, we define a class using the keyword class followed by the class name and then the block of code inside curly braces {} containing fields and methods as follows:
public class Car {
String color;
String model;
// Constructor
public Car(String color, String model) {
this.color = color;
this.model = model;
}
// Method
public void startEngine() {
System.out.println("The " + model + " engine is starting.");
}
}
In this above example, the Car class consists of two attributes (color and model), a constructor to initialize them and a method startEngine() that simulates starting the car's engine.


5.What is an object in Java and how is it created?
An object in Java is an instance of a class. By defining a class, it provides a kind of blueprint for objects. An object, however is created from that blueprint and occupies memory. Objects are created by using the new keyword followed by the constructor of a class.
For example,
Car myCar = new Car ("Red", "Tesla");
Here, myCar is an object of the class Car. The new keyword makes a new instance of the Car class and the constructor Car("Red", "Tesla") initializes the attributes of the object (color and model).


6.What is the constructor in Java and how is it different from a method?
A constructor in Java is a special method used to initialize objects when they are created. It has the same name as a class but does not have a return type. This means constructors are called automatically when a new object is instantiated, on the other hand a method refers to a code block within a class that defines a behavior or function and can return a value or void if no value is returned. Constructors are only used to create the initial state of an object whereas methods are used for operations and behaviors on that object.


7.What is the new keyword in Java used for when creating objects?
The new keyword in Java is used to allocate memory for a new object and invoke the class constructor to initialize it. When new is used the JVM allocates memory for the object, initializes its fields and returns a reference to that memory. This reference can be used to interact with the object. Without the new keyword objects cannot be created in Java.
Example:
Car myCar = new Car("Blue", "BMW");
Here,
new Car("Blue", "BMW") creates a new Car object and initializes it.


8.How do you instantiate an object of a class in Java?
To instantiate an object in Java, you make use of the new keyword prefixed with the class constructor. This is where you initialize the object and assign some memory space.
Here's how you instantiate an object:
Car myCar = new Car("Black", "Honda");
In this example, new Car("Black", "Honda") creates a new instance of the Car class with the specified color and model.


9. What is the purpose of the this keyword in Java?
In Java, the this keyword refers to the current instance of the class inside its methods or constructors. It is used to refer to the instance variables and methods of the current object. When the parameters of the methods or local variables are of the same name as the instance variables, the this keyword is particularly useful in the differentiation of the two. This can further be applied to call other constructors of the same class.
Example:
class Person {
String name;
public Person(String name) {
this.name = name; /* 'this' refers to the instance variable*/
}
}
In this example, this.name is referring to an instance variable and the parameter of the method is also called name.


10. What's the difference between == and .equals() when comparing objects in Java?
In Java == and .equals() are used to compare objects but they do so in different ways:
•== compares the reference (memory address) of two objects, meaning it checks if both references point to the exact same object in memory.
•.equals() compares the content of the objects, meaning it checks if the objects have the same state or values.
For example:
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1 == str2); /* false (references comparison)*/
System.out.println(str1.equals(str2)); /* true (content comparison)*/
The expression == would be false as str1 and str2 occupy different memory location but.equals() would give a true since the contents are identical.