Java interview Questions
Java interview Questions
OOP helps keep code organized by grouping related data and functions together,
making it easier to manage and reuse. It also lets developers build on existing
code, making programs simpler to expand and maintain.
There are four key principles of OOP: Encapsulation, Abstraction, Inheritance, and
Polymorphism.
Encapsulation is like putting all the important details inside a box and only
showing the outside to others. It protects the inside details so they can’t be
changed or messed with directly.
Real-life example: Think of a TV remote. When you press a button, the remote sends
a signal to change the channel or volume, but you don’t see or control the internal
wires or circuits. All those internal parts are hidden (encapsulated), and you only
interact with the buttons.
Real-life example: Think of a car. As a driver, you can control the car by using
its interface: the steering wheel, pedals, and gear shift. You don’t need to know
how the engine, transmission, or fuel system works internally—these details are
encapsulated from you.
Example: Imagine a coffee machine. You can press a button to make coffee, but you
don’t see or change how the machine grinds the beans or heats the water. The coffee
machine’s internal parts are hidden (encapsulated), and you only interact with the
buttons.
Abstraction is hiding complex details to show only the essential parts, making
things easier to understand and use.
showing only the essential features and hiding the complex parts.
Real-life example: Using an ATM is a good example. When you interact with an ATM,
you only care about withdrawing money or checking your balance. You don't need to
know the internal workings of the machine or how the bank processes your
transaction.
Example: Think of driving a car. When you drive, you only care about the controls
like the steering wheel, pedals, and gear shift. You don’t need to know how the
engine works or how the car is built. The complex details are hidden, and you only
interact with the important functions (drive, brake, etc.).
---Polymorphism?
Polymorphism allows objects to be used in different ways through a shared
interface, enabling flexibility and simpler code management.
Real-life example: Think of shapes like circles, squares, and rectangles. They all
have a method to calculate area, but each shape calculates its area differently.
--Inheritance?
Inheritance allows a new class to use properties/characteristics from an existing
class, saving time by reusing code.
Java
C++
Python
C#
Ruby
JavaScript
Swift
PHP
---Method Overloading:
Definition: Method overloading is when multiple methods in the same class have the
same name but differ in the number or type of parameters. This is also known as
compile-time polymorphism or static polymorphism.
class ATM {
// Deposit with cash
void deposit(int cashAmount) {
System.out.println("Deposited cash: " + cashAmount);
}
Here, both methods are named deposit, but one accepts an integer (for cash
deposits) and the other accepts a string and a double (for check deposits).
Depending on how the user wants to deposit, the appropriate method will be called
at compile time.
---Method Overriding:
Method overriding occurs when a subclass provides a specific implementation of a
method that is already defined in its superclass. This is called runtime
polymorphism or dynamic polymorphism.
Real-Time Example (Employee Management System):, vehicle speed
class Employee {
// General salary calculation
void calculateSalary() {
System.out.println("Calculating employee salary");
}
}
---Method overloading allows for flexibility in how methods with the same name
handle different parameters within a class, while method overriding enables a
subclass to provide its own specific implementation of a method. Both are essential
for achieving polymorphism in object-oriented programming.
These modifiers are essential for encapsulation, which helps in controlling the
access to data and methods, ensuring better security and code organization in Java
programs.
---STATIC KEYWORD
Method is called directly using the class name without creating object.
Example:
class Counter {
// Static variable
static int count = 0;
// Static method
static void increment() {
count++; // Increase count by 1
}
// Instance method
void displayCount() {
System.out.println("Count: " + count);
}
}
--=FINAL KEYWORD
to declare constants or to prevent method overriding and inheritance.
Example :
final int max_sum = 100; //constant
---ABSTRACT CLASS
1. we cannot create object directly.
2. It contains both regular methods(with code implementation) and abstract
methods(no code implementation).
3. Implementation of abstract method will be done in subclasses by inheriting and
overriding.
4. It is used as blueprint for other classes.
5. extends keyword
6. will have variables and constructors. (single inheritance)
// Abstract class
abstract class Animal {
// Abstract method (no implementation)
abstract void makeSound();
// Another subclass
class Cat extends Animal {
// Implementing the abstract method
@Override
void makeSound() {
System.out.println("Meow");
}
}
---Interface :
1. java doesnot support multiple inheritance, but using interface it is done.
(implements multiple interfaces)
2. Implements keyword is used
3. class will have, declaration of method without implementation and default
implementation about behaviour since java 8.
4. No instance variables, only constant variables (public,static,final).
5. what methods must be present, without telling how they should be done, allowing
any class to implement those methods.
6. Loose coupling
interface Flyable {
void fly();
}
@Override
public void fly() {
System.out.println("Bird is flying.");
}
}