Tech Master Tutorials
Email Facebook Google LinkedIn Pinterest Twitter
Home Java Java 8 Java Interview Questions Java8 Interview Questions Object Oriented Programming in Java JVM Java Programming

Object Orient Programming (OOPs)

Introduction to OOPs

OOPs stands for Object Oriented Programming System.
Object oriented programming was the new big programming paradigm introduced after functional programming.
OOPs is based on the objects. OOPs is a programming methodology that aims to do programming in such a way that programming entity relates to the real world objects and their behaviours.
Classes and Objects in Java : Classes and Objects

Every system is made of certain components. OOPs is also made of certain components called as Pillars of OOPs.
Below are the four pillers of OOPs :
  • Encapsulation : Encapsulation is combining fields and methods together into a class. Or we can say that combining state and behavior into a class.
    In Java, fields and methods are encapsulated together and with that we can mark fields private and can provide access through only public methods. That way fields can not be updated accidentally like C/C++ where we have global variables.

    Encapsulation Details : Encapsulation Encapsulation

  • Abstraction : Keeping things abstract and providing only what is required at the moment. Like we have interfaces and abstract classes in Java.
    In Java we have interfaces and abstract classes where we can provide abstract methods(methods without definition) and ask the implementation classes to provide the definition.
    Also while sharing the APIs, we can just provide the Interface/Contract imstead of giving all the details. So that way also we hide the implementation details that are not required to call the APIs.

    In Java, we achieve abstraction through abstract classes or interfaces.

    Abstraction Details : Abstraction

  • Inheritance : Inheriting the properties of a class and adding some additional fields and methods.
    If there are multiple entities with some common fields/states as well as some specific fields/states, then we can define a common class for both of them and specific classes can be extended from the common class. Specfic fields/states can be added to the specific classes.

    Inheritance Details : Inheritance

  • Polymorphism : Polymorphism is having the same name and multiple forms. In java we have static and dynamic polymorphism.
    • Static Polymorphism(Method Overloading) : Static polymorphism is having multiple methods in a class with the same name, which method to call is decided on compile time on the basis of the parameters passed. That’s why method poverloading is called static polymorphism.
    • Dynamic Polymorphism(Method Overriding) : Overriding is when a child class overrides the parent class method.
      In case of overriding there are two classes involved, when parent class and child class both have the method with the same name and same signature, but when call the method with the child class object reference, child class method is called.

      Polymorphism Details : Polymorphism