Now that we have discussed the basics of OOP in Java , it's time to discuss some more important concepts (related to interviews in JAVA).
The Diamond Problem occurs in multiple inheritance scenarios where a class inherits from two classes that both inherit from a single base class. This creates ambiguity about which path to follow to inherit from the base class.
Imagine a situation where you have two supervisors at work, and both report to the same manager. If you receive conflicting instructions from both supervisors, you might be confused about which instruction to follow, given that both have authority from the same higher-up.
Java avoids the Diamond Problem by not supporting multiple inheritance of classes. Instead, Java uses interfaces to achieve multiple inheritance.
interface A {
void display();
}
interface B extends A {
default void display() {
System.out.println("Display from B");
}
}
interface C extends A {
default void display() {
System.out.println("Display from C");
}
}
class D implements B, C {
public void display() {
B.super.display();
C.super.display();
}
}
public class Main {
public static void main(String[] args) {
D d = new D();
d.display();
}
}
The output from above code looks like:
Display from B
Display from CIn this example, class D implements both interfaces B and C, which both have a display method. D explicitly resolves which display method to call, thus avoiding the ambiguity.
Abstraction focuses on hiding the complexity by providing a simplified model of something. It involves creating classes that represent abstract concepts rather than specific instances.
Consider driving a car. You use the steering wheel, pedals, and buttons to control the car, without needing to understand the engine’s internal workings.
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts with a key.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car();
myCar.start();
}
}
Encapsulation is the technique of bundling the data (variables) and the code (methods) that operate on the data into a single unit, typically a class. It also involves restricting direct access to some of an object's components.
Think of a capsule pill. The medication is encapsulated within a shell that controls access to the drug inside.
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(100);
System.out.println("Balance: " + account.getBalance());
}
}
Also known as compile-time polymorphism, static polymorphism is achieved through method overloading. It allows multiple methods in the same class to have the same name but different parameters.
Consider a Swiss Army knife. It has multiple tools (methods) that can be used for different tasks, depending on what you need.
class MathOperations {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(5, 3)); // Outputs 8
System.out.println(math.add(5.5, 3.5)); // Outputs 9.0
}
}
Also known as run-time polymorphism, dynamic polymorphism is achieved through method overriding. It allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
Think of a parent and child relationship. The parent defines a general behavior (e.g., how to greet), but the child can have a specific way of greeting based on their own style.
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // Outputs "Dog barks"
}
}
Now we have discussed these important concepts , in the next article we are going to discuss about: