Master Basic OOPs Concepts in Java – Interview Questions & FAQs
1.What is Object-Oriented Programming (OOP) and why is it important in Java?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects" which contain both data (fields) and methods (functions or procedures). In Java, OOP is central to how the language operates as it allows for creating modular, reusable and scalable code. The importance of OOP in Java would be modeling reality into real entities hence making applications easier to grasp, understand and extend/maintain. Actually, OOP languages are about code reuse or inheritance. Their ability to define complex systems clearly with encapsulation makes things go smooth while creating flexible code from polymorphism perspectives. That will make it great for applications especially for the Internet, for high-performance and extensive enterprise such as a basis for other sorts of software designs.
2. What are the four main principles of Object-Oriented Programming in Java?
Four main principles of Object-Oriented Programming in Java are
•Encapsulation: This is the bundling of data and methods that operate on the data into a single unit, called a class with some of the object's components being restricted to access. This hides the internal state of an object from the outside world and ensures that data can only be modified through well-defined methods.
•Inheritance: Inheritance allows a new class to inherit the properties and behaviors of an existing class. In Java, it helps in reducing redundancy by allowing developers to write new classes based on existing ones, promoting code reuse.
•Polymorphism: Objects of different classes can be represented as instances of the same class. Java ensures this by methods overloading compilation time as well as overriding at runtime to provide multiple different implementations of one method with one name.
•Abstraction: Abstraction hides the complex implementation details and provides an overview of only the essential features of an object. In Java, abstraction is implemented with the help of abstract classes and interfaces and allows developers to focus on the high-level operations while the implementation to be handled by the subclasses.
3.What is a class in Java and how is it different from an object?
A class in Java is a blueprint or template of creating an object. It specifies the properties (fields) and methods that the object instantiated from the class will have. A class does not take up space in memory on its own, but it forms the template to create several objects. An object is an instance of a class. When a class is instantiated, memory is allocated to store the object's data. An object is a real entity occupying memory and being an instance of the class with its own state and behavior.
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.
Previous Topic==> Methods in Java FAQ. || Next Topics==> Advanced Java OOPS Concepts FAQ
Other Topic==>
Banking Account Management Case Study in SQL
Top SQL Interview Questions
Employee Salary Management SQL FAQ!. C FAQ
Top 25 PL/SQL Interview Questions
Joins With Group by Having
Equi Join
Joins with Subqueries
Self Join
Outer Join