Basic OOP Concepts

Data Abstraction:
  • Abstraction is a method of hiding the implementation details and showing only functionality.
  • Abstraction is used to hiding the unwanted data and represent only useful information.
    • Example: Mobile interface.
  • Abstraction solves the problem at the design level.
  • Data abstraction can be used to provide security for the data from the unauthorized methods.
  • In Java, data abstraction can be implemented using class.
  • In Java,  it is implemented by Abstract class and Interface.
    • Example: You know what is the functionality of accelerator in the car but you don't know how accelerator internal mechanism.
    • Example: You know what is the use of switch no 1 in remote control of the TV to change the channel but you don't know how to do design that IC of Remote Control.
Encapsulation:

  • Encapsulation is most important mechanism in Java.
  • It is used to wrapping the Variables and Methods.
  • Variables also called as Data.
  • The code acting on data is called Methods.
  • Variables & Methods consider as a single unit (called class).
  • By default, declare the variables of a class as private. 
    • In encapsulation, the variables of a class will be hidden from other classes.
  • The private variables can be accessed only through the methods of their current class.
    • We can provide public setter (write) and getter (read) methods to modify and view the values of the variables.
  • Therefore, it is also known as data hiding.
  • Benefits of Encapsulation
    • The fields of a class can be made read-only or write-only.
    • A class can have total control over what is stored in its fields.
    • The user will have no idea about the inner implementation of the class. 
    • Encapsulation also improves the re-usability and easy to change with new requirements.
    • Encapsulated code is easy to test for unit testing.

Inheritance
  • Inheritance means one class is allowed to inherit the features (variables and methods) of another class.
  • Inheritance is a java mechanism to acquires all the properties from superclass to subclass.
    • The superclass also called Base class, Parent class.
    • The subclass also called Derived class, Child class.
  • The idea behind inheritance in Java is that you can create new classes that are built upon existing classes (Reusability).
Types of Inheritance in Java
Single Inheritance:
 
  • In single inheritance, the subclass inherits the features of one superclass. 
  • In the image below, class A serves as a base class for the derived class B.

Multilevel Inheritance : 
  • In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. 
  • In the image below, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members.
Hierarchical Inheritance : 
  • In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass.
  • In the below image, the class A serves as a base class for the derived classes B, C, and D.

Multiple Inheritance: 
  • In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes. 
  • Note: Java does not support multiple inheritance with classes. 
  • Note: In java, we can achieve multiple inheritance only through Interfaces. 
  • In the image below, Class C is derived from interface A and B.

Hybrid Inheritance: 
  • It is a mix of two or more of the above types of inheritance. 
  • Note: Java doesn’t support multiple inheritance with classes.
  • Note: Hybrid inheritance support only through Interfaces.
Example:
Polymorphism:
  • Polymorphism is derived from 2 greek words: poly and morphs.
    • The word "poly" means many and "morphs" means forms.
  • So, Polymorphism means single name and multiple forms.
  • Example: A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee, a student. So the same person posses different behavior in different situations. This is called polymorphism.
  • There are two types of polymorphism in Java: 
    • Compile-time polymorphism and Runtime polymorphism. 
    • Early Binding and Late Binding
  • Compile-time polymorphism: 
    • It is also known as static polymorphism. 
    • This type of polymorphism is achieved by function or method overloading or operator overloading.
    • Operator Overloading: Java also provide option to overload operators. 
      • Example: Operator overloading, + between two numbers considered as addition and + between two strings consider as join string.
    • Method or function Overloading: When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in the number of arguments or/and change in type of arguments.
      • Example: Function overloading, New_Year_Celebration(), Jan 1 celebrate for new year as per English Calendar and Kartak 1 celebrate for new year as per Indian Calendar.
  • Runtime polymorphism: 
    • It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.
      • Method overriding: When a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
Dynamic Binding:
  • Connecting a method call to the method body is known as binding.
  • There are two types of binding
    • A. Static Binding (also known as Early Binding).
    • B. Dynamic Binding (also known as Late Binding).
  • A. Static Binding that happens at compile time.
    • It is also called early binding.
    • The binding which can be resolved at compile time by the compiler is known as static or early binding. 
    • The binding of static, private and final methods is compile-time. Why? 
    • The reason is that these methods cannot be overridden and the type of the class is determined at the compile time.
  • B. Dynamic Binding that happens at runtime. 
    • It is also called late binding.
    • When the compiler is not able to resolve the call/binding at compile-time, such binding is known as Dynamic or late Binding. 
    • Method Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have the same method and in this case, the type of the object determines which method is to be executed. 
    • The type of object is determined at the run time so this is known as dynamic binding.
  • Difference between Static Binding vs Dynamic Binding
    • 1. Static binding happens at compile-time while dynamic binding happens at runtime.
    • 2. Binding of private, static and final methods always happen at compile time since these methods cannot be overridden. When the method overriding is actually happening and the reference of parent type is assigned to the object of child class type then such binding is resolved during runtime.
    • 3. The binding of overloaded methods is static and the binding of overridden methods is dynamic.


Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form