Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, March 15, 2023

OOP concepts in Java

Java is an Object Oriented Programming Language. It uses objects, or the blueprints of a class, as the fundamental unit of programming. The Basic OOP concepts are,

Abstraction
Polymorphism
Inheritance
Encapsulation
(easily remembered as A-PIE).

Abstraction

What? Abstraction is the process of hiding certain details and showing only essential information to the user.
Why? It provides a template for creating classes, mandating its implementation.
How? There are two ways to achieve this.

1. Abstract Class
It has zero or more abstract methods.

abstract class Vehicle {
    abstract void drive();
}
  • Variables cannot be abstract.
  • We cannot create objects for abstract classes.
  • Concrete class should implement abstract method, or it should again be declared abstract.
  • An abstract class can have private methods, but the abstract method cannot be private.

2. Interface
An interface is different from abstract class in several ways, including:

interface Drivable {
    void drive();
}
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract except default and static methods.
  • An interface cannot contain instance variables. The only fields that can appear in an interface must be declared both static and final.
  • An interface is implicitly abstract. Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
  • Abstract Methods in an interface are implicitly public.

Polymorphism

What? Polymorphism means the different forms of something.
Why? It establishes different ways of doing the same thing, thereby reducing the code.
How? Two ways of achieving this are,

1. Overloading
Method overloading refers to using multiple methods with the same name, but different signatures.

drive(String key) { }
drive(String button){ }
  • A method can be overloaded in the same class or in a subclass
2. Overriding
Method overriding is rewriting a parent's function by a child.

drive(String x) { } in  the parent class
drive(String x) { } in the child class.
  • There is no change in the signature while overriding.
  • We cannot override a final or static method.
Inheritance

What? A parent-child relationship
Why? It puts common functionalities together and others separate.
How? Two ways of achieving this are,

1. Extend
A child class extends the parent and overrides its necessary methods. 

class Vehicle {
    drive() { }
}
class Car extends Vehicle {
    drive() { }
}
  • It is mandatory to override if the parent is abstract.
  • A class can extend only one parent.
2. Implement
A class can implement an interface and override all its non-default methods. 
    interface Drivable {
        drive() { }
    }
    class Car implements Drivable {
        drive() { }
    }
  • A class can implement multiple interfaces.
  • Each interface adds a behavior to the class. 
Encapsulation

What? Encapsulation is integrating attributes and methods into a single unit, called class.
Why? It provides security to the attributes of a class.
How? Attributes are declared private with public getter/setters to access them.

class Car {
    private String type;
    public String getType() {
        return this.type 
    }
    public void setType(String type) {
        this.type = type;
    }
}