Published November 14, 2023 by

Cheat Sheet for OOPs concept in Java

 

  1. Class and Object:

    • Class: Blueprint or template for creating objects.
    • Object: Instance of a class; represents a real-world entity.

    java
    // Class definition
    class Car {
        // Fields (attributes)
        String model; int year;
        // Methods (behavior)
        void start() {
          // implementation
        }
    }
    // Creating an object
    Car myCar = new Car();



  2. Encapsulation:

    • Bundling of data (attributes) and methods that operate on the data into a single unit (class).
    • Access modifiers (public, private, protected) control access to class members.
    java
    class BankAccount {
        private double balance;
        public void deposit(double amount){
              // implementation
        }
        public double getBalance() {
              return balance;
        }
    }



  3. Inheritance:

    • A mechanism where a new class inherits properties and behaviors of an existing class.
    • extends is used to implement inheritance.
    java
    class Animal {
          void eat() {
              // implementation
           }
    }
    class Dog extends Animal {
          void bark() {
              // implementation
         }
     }



  4. Polymorphism:

    • Ability of a method to do different things based on the object it is acting upon.
    • Achieved through method overloading and overriding.
    java
    class Shape {
          void draw() {// implementation}
    }
    class Circle extends Shape {
          void draw() { // overridden implementation }
    }
    // Polymorphic behavior
    Shape myShape = new Circle();
    myShape.draw();



  5. Abstraction:

    • Concept of hiding the complex implementation details and showing only essential features of an object.
    • Abstract classes and interfaces are used to achieve abstraction.
    java
    abstract class Shape {
          abstract void draw();
    }
    class Circle extends Shape {
          void draw() { // implementation }
    }



  6. Interface:

    • A collection of abstract methods.
    • Classes implement interfaces to provide specific implementations for those methods.
    java
    interface Printable {
          void print();
    }
    class Document implements Printable {
          public void print() { // implementation }
    }



  7. Constructor:

    • Special method used for initializing objects.
    • It has the same name as the class and no return type.
    java
    class Person {
       String name;
       // Constructor
       public Person(String n) { name = n; }
    }
    // Creating an object with a constructor
    Person person1 = new Person("John");



  8. Getter and Setter:

    • Methods used to retrieve and update the values of private fields.
    java
    class Student {
          private String name;
          // Getter
          public String getName() { return name; }
          // Setter
          public void setName(String n) { name = n; }
    }