Challenge

Project

Object-Oriented Programming (OOP) is a popular programming paradigm that is widely used in Java programming. OOP focuses on the use of classes and objects, and it has several concepts that are referred to as "Oops Concepts" in Java. Some of the main Oops Concepts in Java with examples are:

  1. Encapsulation - This is the concept of wrapping data and methods into a single unit, which is known as a class. This helps in hiding the implementation details of the class from the user. For example:
public class BankAccount {
private int accountNumber;
private double balance;
public int getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
}

In the above example, the data members accountNumber and balance are private, which means they can only be accessed within the class. The methods getAccountNumber(), getBalance(), deposit(), and withdraw() are public, which means they can be accessed by other classes.

  1. Inheritance - This is the concept of creating a new class from an existing class. The new class, also known as the subclass, inherits the properties and methods of the existing class, which is known as the superclass. For example:
    public class Animal {
    public void eat() {
    System.out.println("Animal is eating");
    }
    }
    public class Dog extends Animal {
    public void bark() {
    System.out.println("Dog is barking");
    }
    }
    public class Main {
    public static void main(String[] args) {
    Dog dog = new Dog();
    dog.eat();
    dog.bark();
    }
    }

    In the above example, the Dog class extends the Animal class and inherits its eat() method. The Dog class also has a new method bark() that is not present in the Animal class.

  1. Polymorphism - This is the concept of having many forms. It allows objects of different classes to be treated as if they belong to the same class, which is achieved through method overloading and method overriding. For example:
    public class Shape {
    public void draw() {
    System.out.println("Drawing a shape");
    }
    }
    public class Circle extends Shape {
    @Override
    public void draw() {
    System.out.println("Drawing a circle");
    }
    }
    public class Rectangle extends Shape {
    @Override
    public void draw() {
    System.out.println("Drawing a rectangle");
    }
    }
    public class Main {
    public static void main(String[] args) {
    Shape shape1 = new Circle();
    Shape shape2 = new Rectangle();
    shape1.draw();
    shape2.draw();
    }
    }

    In the above example, the Circle and Rectangle classes override the draw() method of the Shape class, which allows them to draw their own specific shape. The Main class creates objects of the Circle and Rectangle classes and assigns them to variables of type Shape. This allows the draw() method to be called on each of these objects using the same variable, demonstrating polymorphism

  1. Abstraction - This is the concept of hiding the implementation details of a class and only exposing its interface. This is achieved through abstract classes and interfaces. For example:
    public abstract class Vehicle {
    public abstract void start();
    public abstract void stop();
    }
    public class Car extends Vehicle {
    @Override
    public void start() {
    System.out.println("
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.