ALL JAVA OOPS CONCEPT



The java programming language is built on the concept of object-oriented programming language. Though java is an object-oriented programming language, it is not 100% object-oriented because of the eight primitive data types available in java. But still, the main oop's concept of java are as follows

Let's understand all the above-mentioned concepts one by one.

Class: 

When we are talking about a java program or java application, it contains a lot of java "classes". A java class is basically a blueprint for objects. A class contains member variables which are also known as instance (non - static fields) variables, class variables (static fields), methods. A class is declared with the "class" keyword. A class must have an access specifier.

Below is the syntax of a class:- 

public class Person
{
   //Instance variables
   String Name;
   int Age;

   //Constructor for object creation
   public Person(String Name, int Age)
   {
     this.Name = Name;
     this.Age = Age;
   } 

   // Methods
   public void Display()
   {
     System.out.print(" This is a method ");
   }
}


Object:

The next important topic of java is "Object". As stated earlier class is a blueprint of an object, whereas objects are real-world physical entities. This object has a state and behavior. Suppose Animal is the class then Dog, Bird, Horse are the objects. The number of legs, color, eat, run is the state and behavior of these objects. In Java, we can create an object in five different ways.

  • Using new keywords: 
           
Person personObject = new Person("Tapas",10);
 
  • Using newInstance() method of Class class:    

This newInstance() present inside Class class. This Class class is present under java.lang package. 
Person personObject = Person.class.newInstace();
  • Using newInstance() method of Constructor class:
  • Using clone() method: 
 
Person personSecondObject = (Person).personObject.clone();
  • Using Deserialization:


Inheritance:

Inheritance is an important OOP(Object Oriented programming) feature of java programming language by which child class can access the parent class's properties based on their(properties) accessibility. 
Let assume an animal class has a method or function to eat. Now we have a few other classes that need that eat method in it. We can write the same eat method in all the classes. But this is not a good programming practice and this will increase code complexity. 

To overcome this problem we can inherit the eat method of an animal class in other classes where we need it. The animal class will be called Parent class or Superclass or Base class and the other class says the dog class which will use the eat method will be known as Child class. We can achieve inheritance with the extends keyword. see the below example for better understanding.

class Animal{
   void eat(){
	System.out.println("Eating");
   }
}

public class Dog extends Animal{
   public static void main(String args[]){
	Dog obj = new Dog();
	obj.eat();
   }


Polymorphism:

Polymorphism means "many forms" i.e. we can perform the same task in many different ways. This polymorphism feature of Java is achieved by inheritance that we learned earlier. To understand the polymorphism let us talk about the sound of animals. As we know that making sound is a generic task for all animals but all animals don't make the same sound. 

Suppose in Animal class we have one generic method say soundOfAnimal() and the dog and cat class extends the animal class and overrides that method to use it. Which method will be executed that will be decided at Runtime. So this scenario is known as Runtime Polymorphism or Dynamic Polymorphism

class Animal {
  public void soundOfAnimal() {
    System.out.println("The animal makes a sound");
  }
}

class Dog extends Animal {
  public void soundOfAnimal() {
    System.out.println("The dog barks: bow bow");
  }
}

class Cat extends Animal {
  public void soundOfAnimal() {
    System.out.println("The cat sounds: meww meww");
  }
}

class MainClass {
  public static void main(String[] args) {
    Animal animalObj = new Animal();  // Create a Animal object
    Animal dogObj = new Dog();  // Create a Dog object
    Animal catObj = new Cat();  // Create a Cat object
    
    animalObj.soundOfAnimal();
    dogObj.soundOfAnimal();
    catObj.soundOfAnimal();
  }
}
 
The above example is of Runtime Polymorphism or Dynamic Polymorphism. 

There is another way we can achieve polymorphism in java. That method is known as Static Polymorphism or Compiletime Polymorphism. Static or Compiletime polymorphism is achieved when we overload a method in a single class. Suppose we have a class called staticPolymorphismExample, inside this class, we have a display() method. Now we overloaded this method three times for three different tasks. See the example code below for a better understanding.

class staticPolymorphismExample
{
    void display (int a)
    {
       System.out.println ("a: " + a);
    }
    void display (int a, int b)
    {
       System.out.println ("a and b: " + a + "," + b);
    }
    double display(double a) {
       System.out.println("double a: " + a);
       return a*a;
    }
}
class MethodOverloading
{
    public static void main (String args [])
    {
        staticPolymorphismExample Obj = new staticPolymorphismExample();
        double result;
        Obj .display(10); 	//O/P a: 10
        Obj .display(10, 20);  //O/P a and b: 10,20
        result = Obj .display(4.4);
        System.out.println("O/P : " + result);  //O/P double a: 19.36
    }
}

Abstraction:

Abstraction means hiding the implementation from the user. That means abstraction hides the how part from the user and focuses on what part. e.g. instead of bothering how the machine work focuses on what the machine does. In java, abstraction can be achieved in two ways 1. Abstract class (0% to 100% abstraction), 2. Interface (100% abstraction)

In java, a class declared with the "abstract" keyword will be known as an abstract class. 
Main points to remember about the abstract class:
  1. An abstract class may or may not contain concrete methods.
  2. An abstract class cannot be instantiated i.e we are not allowed to create objects of an abstract class. otherwise, it will create a compilation error: ExampleAbstractClass is abstract; cannot be instantiated.
  3. An abstract class can be extended by other concrete or abstract class.
  4. An abstract class can implement an interface.
The interface is the other way by which we can achieve abstraction. Let's assume a  case where Class A is the parent class of class B and class C also wants to extend class A i.e class A will be a parent for both B and C. This situation is known as multiple inheritances. But Java doesn't support multiple inheritance in this way. To overcome this problem we can make A an interface instead of class. Inside interface, we cannot have concrete methods (as of java 7). java 8 Onwards we can have static and default methods inside the interface. The interface represents IS-A relationship. abstract classes are slightly faster than interface because interface involves a search before calling any overridden method in Java.

Encapsulation:

Encapsulation ensures data hiding from other classes. To achieve encapsulation one class must declare all the variables as private and write getter and setter methods to access the variables from outside the class. In this way, the variables will be read-only or wire only and the class will have control over these variables.

class EncaptulationTest {
   private String name;
   private String idNum;
   private int age;

   public int getAge() {
      return age;
   }

   public String getName() {
      return name;
   }

   public String getIdNum() {
      return idNum;
   }

   public void setAge( int newAge) {
      age = newAge;
   }

   public void setName(String newName) {
      name = newName;
   }

   public void setIdNum( String newId) {
      idNum = newId;
   }
}


public class RunEncaptulation {

   public static void main(String args[]) {
      EncaptulationTest encap = new EncaptulationTest();
      encap.setName("Tapas Mishra");
      encap.setAge(20);
      encap.setIdNum("12343");

      System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
   }
}


Association:

In Java, objects of various classes talk to each other to perform a specific task. This relationship between two separate classes established through the objects is called association in Java. Association can be of four types that is -  one to one, many ones, one to many, and many to many.
Association is categorized into two versions that are aggregation and composition.

Aggregation:

An aggregation is a special form of association. It represents a has-a relationship between objects of separate classes, unidirectional association. For example, suppose a department can have students but vice versa is not possible. The important thing about aggregation is that both entities mean objects can survive individually.

Composition :

Composition on the other hand is the most stricter version of aggregation. Two objects are strictly dependent on each other. Suppose one of the objects in the relationship is destroyed then the other object will be destroyed automatically.