Object Oriented Programming

Object Oriented Programming(OOP) is a programming paradigm centered around the concept of “objects”, which are instances of “classes”. A class defines the blueprint or structure of an object, including the data it holds (called attributes or properties) and the actions it can perform (called methods or functions).

Key Principles of Object Oriented Programming

1. Class

  • A class is a blueprint for creating objects.
  • It defines the attributes (fields) and behaviors (methods). 
class Car {

  String color;

  void drive() {

    System.out.println("Driving...");

  }

}

2. Object

  • An object is an instance of a class.
  • It represents a real-world entity.
  • Created using the new keyword.
  • Can be tangible (car, animal etc) or intangible (banking system)
Car myCar = new Car();

myCar.drive();

3. Inheritance

  • One class inherits properties of another.
  • Promotes code reusability.
  • Reduces redundancy.
  • Use the extends keyword.
  • Represents Java IS-A type of Relationship i.e parent child relationship
class Animal {
  void sound() {
    System.out.println("Animal sound");
  }
}
class Dog extends Animal {
  void bark() {
    System.out.println("Dog barks");
  }
}

Types of Inheritance

There are five types of inheritance.

a) Single Inheritance

  • One child class inherits from one parent class.
  • Simplest form of inheritance.
  • Promotes code reusability and clarity.
  • Example: Dog inherits from Animal.
Object Oriented Programming
Fig: Single Inheritance

b) Multi-level Inheritance

  • A class inherits from a child class, which in turn inherits from another parent class.
  • Forms a chain of inheritance.
  • Animal → Mammal → Dog
multilevel inheritance

c) Hierarchical Inheritance

  • Multiple child classes inherit from a single parent class.
  • Common features of the parent are shared with all child classes.
  • Example: Dog, Cat, and Cow all inherit from Animal.
Hierarchical Inheritance

d) Multiple Inheritance

  • A single class inherits from two or more parent classes.
  • Can lead to ambiguity if parents have methods with the same name (handled differently in each language).
  • Example: FlyingCar inherits from both Car and Aeroplane.
Multiple Inheritance

e) Hybrid Inheritance

  • A combination of two or more types of inheritance.
  • It can involve multiple and hierarchical inheritance together.
  • Needs careful design to avoid complexity and ambiguity.
  • In Java, we can achieve hybrid inheritance only through Interfaces

Advantages of Inheritance

  • Code Reusability: Inheritance allows for code reuse and reduces the amount of code that needs to be written. The subclass can reuse the properties and methods of the superclass, reducing code duplication.
  • Abstraction: Inheritance allows for the creation of abstract classes that define a common interface for a group of related classes. This promotes abstraction and encapsulation, making the code easier to maintain and extend.
  • Class Hierarchy: Inheritance allows for the creation of a class hierarchy, which can be used to model real-world objects and their relationships.
    Polymorphism: Inheritance allows for polymorphism, which is the ability of an object to take on multiple forms. Subclasses can override the methods of the superclass, which allows them to change their behavior in different ways.

4. Polymorphism

  • Ability to take many forms.
  • Improves flexibility and reusability.
  • Enables dynamic method binding.

There are two types of polymorphism.

  • Method Overloading (Compile-Time Polymorphism)
  • Method Overriding (Run-Time Polymorphism)

Method Overloading

  • Same method name, different parameters.
  • Resolved at compile-time.
  • Java determines which method to call based on the arguments passed to it.
class Calculator {
    public void add(int a, int b) {
        System.out.println("Sum Int" + (a+b));
    }
    public void add(double a, double b) {
        System.out.println("Sum doubles: " + (a+b));
    }  
}
public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        calc.add(5, 10);
       
            }
}

Method Overriding

  • Occurs when a subclass has a method with the same name and signature as a method in its superclass.
  • Subclass can override the method to provide its own implementation.
  • Method that gets called is determined at runtime based on the object that the method is called on.
class Animal {  
    public void makeSound() {
        System.out.println("Animal Sound");
    }
}
 class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Bark");
    }
class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Meow");
    }
}

public class Main {
    public static void main(String[] args) {
    	  Animal animal=new Animal();
        Animal animal1 = new Dog();
        Animal animal2 = new Cat();

        animal.makeSound();
        animal1.makeSound(); 
        animal2.makeSound(); 
    }
}

Abstract Class

  • A class that cannot be instantiated.
  • Can contain both abstract and concrete methods.
  • Used to provide common base functionality.
  • Use an abstract keyword.
abstract class Shape {
  abstract void draw();
  void display() {
    System.out.println("Shape");
  }
}
class Circle extends Shape {
  void draw() {
    System.out.println("Drawing Circle");
  }
}

Class Methods

  • Belongs to the class, not objects.
  • Declared using the static keyword.
  • Can be called without creating an object.
  • Useful for utility functions.
class MathUtils {

  static int square(int x) {

    return x * x;

  }

}

//can be accessed as 
MathUtils.square(5); // 25

Instance Methods

  • Belongs to an object.
  • Can access instance variables and methods.
  • Requires an object to call.
class Person {
  String name;
  void sayHello() {
    System.out.println("Hello " + name);
  }
}
// Access Person As
Person p = new Person();
p.name = "Alice";
p.sayHello();

Let’s get connected

We can be friends. Find on FacebookLinkedinGithubYouTube

BuyMeACoffee, and Instagram.

Contribute: BuyMeACoffee

ContactContact Us