Java Interview Questions

30 Questions
Java

Java

Web DevelopmentBackend

Question 3

What are the main principles of Object-Oriented Programming?

Answer:

The main principles of Object-Oriented Programming (OOP) are encapsulation, inheritance, polymorphism, and abstraction. These principles help in designing software that is modular, scalable, and easy to maintain.

1. Encapsulation

  • Definition: Encapsulation is the practice of wrapping data (variables) and methods (functions) that operate on the data into a single unit or class. It restricts direct access to some of the object’s components, which can help prevent the accidental modification of data.
  • Benefits: Promotes data hiding, reduces complexity, and increases control over data.
  • Example:
    public class Person {
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            if (age > 0) {
                this.age = age;
            }
        }
    }

2. Inheritance

  • Definition: Inheritance is a mechanism where a new class (subclass) inherits the properties and behavior (fields and methods) of an existing class (superclass). It promotes code reuse and establishes a natural hierarchy.
  • Benefits: Facilitates code reuse, simplifies code maintenance, and establishes a relationship between classes.
  • Example:
    public class Animal {
        public void eat() {
            System.out.println("This animal eats food.");
        }
    }
    
    public class Dog extends Animal {
        public void bark() {
            System.out.println("The dog barks.");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Dog myDog = new Dog();
            myDog.eat();  // Output: This animal eats food.
            myDog.bark(); // Output: The dog barks.
        }
    }

3. Polymorphism

  • Definition: Polymorphism allows objects of different classes to be treated as objects of a common superclass. It can be achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).
  • Benefits: Enhances flexibility and scalability of the code, allows for the implementation of dynamic method binding.
  • Example:
    public class Animal {
        public void sound() {
            System.out.println("This animal makes a sound.");
        }
    }
    
    public class Cat extends Animal {
        @Override
        public void sound() {
            System.out.println("The cat meows.");
        }
    }
    
    public class Dog extends Animal {
        @Override
        public void sound() {
            System.out.println("The dog barks.");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Animal myAnimal = new Cat();
            myAnimal.sound();  // Output: The cat meows.
    
            myAnimal = new Dog();
            myAnimal.sound();  // Output: The dog barks.
        }
    }

4. Abstraction

  • Definition: Abstraction is the concept of hiding the complex implementation details and showing only the essential features of an object. It is achieved through abstract classes and interfaces.
  • Benefits: Reduces complexity, enhances code readability, and isolates impact of changes.
  • Example:
    abstract class Vehicle {
        abstract void startEngine();
    
        public void displayInfo() {
            System.out.println("This is a vehicle.");
        }
    }
    
    public class Car extends Vehicle {
        @Override
        void startEngine() {
            System.out.println("Car engine started.");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Vehicle myCar = new Car();
            myCar.displayInfo();  // Output: This is a vehicle.
            myCar.startEngine();  // Output: Car engine started.
        }
    }

Conclusion

These four principles of OOPβ€”encapsulation, inheritance, polymorphism, and abstractionβ€”provide a framework for building robust, maintainable, and scalable software. They enable developers to create modular programs that can be easily understood, modified, and extended.

Recent job openings