OOP’s, What are the important concepts of OOPS in Java?

Reading Time: 5 minutes

OOP’s is “Object Oriented Programming”,it consists objects and classes and therefore in this,programming is done with class and object.

OOP’s Concepts

WHAT IS “CLASS’ ?

OOP’s A class in java is a blueprint contains all the methods,functions,main in simple words the whole code is enclosed inside the class.Therefore class is a block contains everything.Like a capsule enclosed medicines inside it.

class class_name{

The code come here:

Methods/functions/main

}

WHAT IS AN “OBJECT”?

It is a real world entity like pen or chair which is used used to invoke classes.So any class can be accessible with the help of an object.

Class_Name Object_Name=new Class_Name();
This is the way to create an object of any class:
Object_Name.Method_Name();
This is how an object of a class access a method of that class:

INHERITANCE

A process in which a class acquires all the properties of other class. Hence the process is called as inheritance.

So class which inherit properties is known as child class and the other class whose methods get inherit is known as parent class.

Types of “Inheritance”

There are multiple types of inheritance in Java which are basically depends on various conditions like:

*Single_Inheritance

In this one class extends another class (one class only).

Single inheritance

*Multiple Inheritance-

In Multiple Inheritance, one class extending more than one class.But Java does not support multiple inheritance.

Multiple inheritance

*Multilevel Inheritance

In Multilevel Inheritance, one class can inherit from a derived class.Therefore, the derived class becomes the base class for the new class.

Multilevel Inheritance

*Hierarchical Inheritance

In this, one class is inherited by many sub classes.Therefore parent class is inherited by multi child class.

Hierarchical Inheritance

*Hybrid Inheritance

It is a combination of Single and Multiple inheritance because it contain one main parent class and sub multi parent class and multi child classes.

Hybrid Inheritance

Note: 

**Java (does not) support hybrid/Multiple inheritence

**EXAMPLE “CODE OF INHERITANCE

class Employee{  
 float salary=40000;  
}  
class Programmer extends Employee{  
 int bonus=10000;  
 public static void main(String args[]){  
   Programmer p=new Programmer();  
   System.out.println("Programmer salary is:"+p.salary);  
   System.out.println("Bonus of Programmer is:"+p.bonus);  
}  
}  

Polymorphism

It is a concept in which we can perform a single task ,in different ways so it’s name suggest its working and it is derived from 2 Greek words: poly and morphs.

Therefore the word “poly” means many and “morphs” means forms.

Image showing Polymorphism

Two types:

1.Runtime: is a process in which a call to an overridden method is resolved at runtime so not on compile-time.

So runtime is which contain method overridden process in which we inherit a method with same name in a child class.

2.Compile Time: is a process in which method a class can contain two method with same name having different arguments or no of arguments.

So compile_time does not require that runtime condition.

**EXAMPLE “CODE

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

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

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

class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object
    myAnimal.animalSound();
    myPig.animalSound();Here
    myDog.animalSound();
  }
}

ABSTRACTION

An Abstraction is a process of hiding the implementation details and showing only functionality so to the user.

So in simple words it is the process in which user only see the information in which he is interested in and not the whole detail.

So Ac is the best example of abstraction we just want to know how to on that and not how it’s work.

Image showing Abstraction

**EXAMPLE “CODE

// Abstract class
abstract class Animal {
  // Abstract method (does not have a body)
  public abstract void animalSound();
  // Regular method
  public void sleep() {
    System.out.println("Zzz");
  }
}

// Subclass (inherit from Animal)
class Pig extends Animal {
  public void animalSound() {
    // The body of animalSound() is provided here
    System.out.println("The pig says: wee wee");
  }
}

class Main {
  public static void main(String[] args) {
    Pig myPig = new Pig(); // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}

Encapsulation

This  is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines

Image showing Encapsulation

Therefore by providing only a setter or getter method, you can make the class read-only or write-only.

In other words, you can skip the getter or setter methods.It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method.

You can write the logic not to store the negative numbers in the setter methods.It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members

**EXAMPLE “CODE

class Account {  
//private data members  
private long acc_no;  
private String name,email;  
private float amount;  
//public getter and setter methods  
public long getAcc_no() {  
    return acc_no;  
}  
public void setAcc_no(long acc_no) {  
    this.acc_no = acc_no;  
}  
public String getName() {  
    return name;  
}  
public void setName(String name) {  
    this.name = name;  
}  
public String getEmail() {  
    return email;  
}  
public void setEmail(String email) {  
    this.email = email;  
}  
public float getAmount() {  
    return amount;  
}  
public void setAmount(float amount) {  
    this.amount = amount;  
} } 
public class TestEncapsulation {  
public static void main(String[] args) {  
    //creating instance of Account class  
    Account acc=new Account();  
    //setting values through setter methods  
    acc.setAcc_no(7560504000L);  
    acc.setName("Sonoo Jaiswal");  
    acc.setEmail("sonoojaiswal@javatpoint.com");  
    acc.setAmount(500000f);  
    //getting values through getter methods  
    System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+acc.getEmail()+" "+acc.getAmount());  
}  
}   

Reference-https://www.javatpoint.com/java-oops-concepts

Written by 

Nitin is a Software Consultant, with experience of more than 7 months. He works on Rust Programming Language and Embedded Development using Rust. He is also fond of Java Programming & Artificial Intelligence. Apart from that, his hobbies are Watching Netflix, Reading, Singing & Writing.