Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java OOPs Concepts Tutorial

  1. What is Object-Oriented Programming (OOP)?
  2. Classes and Objects
  3. Inheritance
  4. Polymorphism
  5. Encapsulation
  6. Abstract Classes and Methods
  7. Interfaces
  8. Composition
  9. Enums
  10. Abstraction

1. What is Object-Oriented Programming (OOP)?

Object-Oriented Programming is a programming paradigm that uses objects to organize code. An object is an instance of a class, and a class is a blueprint for creating objects.

Java Code Example:

                    
public class MyClass {
    private int myVariable;

    public void setMyVariable(int value) {
        myVariable = value;
    }

    public int getMyVariable() {
        return myVariable;
    }
}
                    
                

2. What are Classes and Objects in Java?

In Java, a class is a blueprint for creating objects, and objects are instances of classes. Classes define the properties and behaviors that objects of the class will have.

Java Code Example:

            
public class Car {
    private String model;
    private int year;

    public Car(String model, int year) {
        this.model = model;
        this.year = year;
    }

    public String getModel() {
        return model;
    }

    public int getYear() {
        return year;
    }
}
            
        

3. What is Inheritance in Java OOP?

Inheritance is a mechanism in Java OOP that allows a class to inherit properties and behaviors from another class. The class that is inherited from is called the superclass, and the class that inherits is called the subclass.

Java Code Example:

            
public class Animal {
    protected String species;

    public Animal(String species) {
        this.species = species;
    }

    public void makeSound() {
        System.out.println("Animal sound");
    }
}

public class Dog extends Animal {
    public Dog() {
        super("Dog");
    }

    @Override
    public void makeSound() {
        System.out.println("Woof!");
    }
}
            
        

4. What is Polymorphism in Java?

Polymorphism allows objects to be treated as instances of their parent class rather than their actual class type. In Java, it can be achieved through method overriding and interfaces.

Java Code Example:

            
interface Shape {
    void draw();
}

class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing Square");
    }
}
            
        

5. What is Encapsulation in Java OOP?

Encapsulation is the bundling of data (attributes) and methods that operate on the data into a single unit known as a class. It helps in hiding the internal implementation details of an object.

Java Code Example:

            
public class BankAccount {
    private double balance;

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
        }
    }

    public double getBalance() {
        return balance;
    }
}
            
        

6. What are Abstract Classes and Methods in Java?

An abstract class in Java is a class that cannot be instantiated. It can have abstract methods, which are methods without a body. Abstract classes are meant to be extended by subclasses, and the subclasses must implement the abstract methods.

Java Code Example:

            
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing Square");
    }
}
            
        

7. What are Interfaces in Java OOP?

An interface in Java is a collection of abstract methods. Classes implement interfaces to provide concrete implementations for those methods. Interfaces allow for multiple inheritance in Java.

Java Code Example:

            
interface Printable {
    void print();
}

class Printer implements Printable {
    @Override
    public void print() {
        System.out.println("Printing...");
    }
}
            
        

8. What is Composition in Java OOP?

Composition is a design principle in Java OOP where a class contains objects of other classes, helping to create complex structures by combining simpler objects. It promotes code reuse and maintainability.

Java Code Example:

            
class Engine {
    void start() {
        System.out.println("Engine started");
    }
}

class Car {
    private Engine engine;

    Car(Engine engine) {
        this.engine = engine;
    }

    void start() {
        engine.start();
        System.out.println("Car started");
    }
}
            
        

9. What are Enums in Java?

Enums in Java are a special data type that represents a group of constants. Enumerations are often used to define a fixed set of values that are easily readable and maintainable in the code.

Java Code Example:

            
public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
            
        

10. What is abstraction, and how does Java support it in the context of Object-Oriented Programming?

Abstraction is the process of hiding complex implementation details and exposing only the necessary features of an object. In Java, abstraction is achieved through abstract classes and interfaces.

Java Code Example:

            
// Abstract class example
abstract class Shape {
    abstract void draw(); // Abstract method with no implementation

    void display() {
        System.out.println("This is a shape.");
    }
}

// Concrete class implementing the abstract class
class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle.");
    }
}
        
        

In this example, the abstract class "Shape" declares an abstract method "draw()" that must be implemented by its concrete subclasses. The "display()" method provides a default implementation. The "Circle" class extends "Shape" and provides a specific implementation for the "draw()" method.

©2024 WithoutBook