Let's Learn Object Oriented Programming (Using Java)

Let's Learn Object Oriented Programming (Using Java)

Note - The programs I have written here are in java. but 95% of concepts are similar in any object-oriented programming language.

Object Oriented Programming is a methodology to design a program using classes and objects. It simplifies the maintenance and development of the program.

Class

Class is a user-defined data type that defines properties and functions or methods. Class is only a Logical Representation of data.

For Example -> Vehicle is a class. The Vehicle Parts are its properties and the action performed by Vehicle is known as functions/methods.

Object

The object is a run-time entity. it is an instance of the class. An Object can represent a person, place, or any other item. An object can operate on both data members and member functions.

class Vehicle{
   String name;
   int wheels;

   public void getInfo() {
       System.out.println("The name of this Vehicle is " + this.name);
       System.out.println("Wheels in Vehicle " + this.wheels);
   }
}

public class OOPS {
   public static void main(String args[]) {
       Vehicle v1 = new Vehicle();
       v1.name = "Bicycle";
       v1.wheels = 2;
       v1.getInfo();

       Vehicle v2 = new Vehicle();
       v2.name = "Car";
       v2.wheels = 4;
       v2.getInfo();
   }

this

'this' keyword in java refers to the current instance of the class. it is used to:

  • pass the current objects as a parameter to another method

  • refer to the current class instance variable

Constructor

Constructor is a special type of method which is invoked automatically at the time of object creation.

  • Constructors have the same name as class or structure.

  • Constructors don't have a return type.

  • Constructors are only called once.

There are 3 types of constructors in java.

  • Non-Parameterized constructor: A constructor which is has no argument is known as a non-parameterized constructor.
class Vehicle{
   String name;
   int wheels;

   Vehicle() {
       System.out.println("Constructor called");
   }
}
  • Parameterized constructor: A constructor which has parameters is called a parameterized constructor.
class Vehicle {
   String name;
   int wheels;

   Student(String name, int wheels) {
       this.name = name;
       this.wheels = wheels;
   }
  • Copy Constructor: A copy constructor is an overloaded constructor used to declare and initialize an object from another object.
class Vehicle {
   String name;
   int wheels;

   Vehicle(Vehicle v2) {
       this.name = v2.name;
       this.wheels = v2.wheels;
   }
}

Polymorphism

Poly means many and morphism means forms. So polymorphism is the technique to represent the same interface in many different forms. With polymorphism, each of these classes has different data.

Types of Polymorphism

  1. Compile time Polymorphism
  2. Runtime Polymorphism

Compile Time Polymorphism: As the name suggests it is implemented at the compile time is known as compile time polymorphism

Function Overloading: Function overloading is a technique that allows us to have more than one function with the same function name but different functionality.

class Vehicle {
   String name;
   int wheels;

   public void displayInfo(String name) {
       System.out.println(name);
   }

   public void displayInfo(int wheels) {
       System.out.println(wheels);
   }

   public void displayInfo(String name, int wheels) {
       System.out.println(name);
       System.out.println(wheels);
   }
}

Runtime Polymorphism: Function overriding is an example of runtime polymorphism. Function overriding means when the child class contains the method which is already present in the parent class. so the hide class overrides the function of the parent class.

class Shape {
   public void area() {
       System.out.println("Displays Area of Shape");
   }
}
class Triangle extends Shape {             // Inheritence
   public void area(int h, int b) {
       System.out.println((1/2)*b*h);
   }  
}
class Circle extends Shape {
   public void area(int r) {
       System.out.println((3.14)*r*r);
   }  
}

Inheritance

Inheritance is a technique in which one object can acquire all the properties and behaviors of its parent object automatically. This will increase our code reusability

The class which inherits the members of another class is called a derived class and the class whose members are inherited is called a base class.

Types of Inheritance

  1. Single-level Inheritance: When one class inherits another class, it is known as single-level inheritance.

  2. Multi-level Inheritance: Multilevel inheritance is a process of deriving a class from another derived class.

  3. Hierarchical inheritance: Hierarchical inheritance is a process of deriving more than one class from a base class.

  4. Hybrid inheritance: Hybrid inheritance is a combination of simple, multiple inheritance and hierarchical inheritance.

Encapsulation

Encapsulation is a process of combining data and functions into a single unit called a class. In Encapsulation we cannot be accessed directly; it is accessed through the functions in classes.

Abstraction

Abstraction is the process of hiding unnecessary details and Showing only the essential parts of the application to the user.

Did you find this article valuable?

Support Harsh's Insights by becoming a sponsor. Any amount is appreciated!