OOPs concepts in Java

OOPs concepts in Java

OOPs concepts in Java define the main Object-Oriented Programming methods in java. The main motive of object-oriented programming is to work on the principles of real-world entities for example objects, classes etc. An object is said to be something that has its own form. That’s why we call it the instance of the class. It has its own address and occupies its own memory.

As a result, objects communicate with each other based on their code or data. For example, we take leopard as an object as it has its own color, his own characteristics as well as features. However, a collection of various objects is known as a class occupying no memory.

OOPs Concepts in JAVA

OOPs concepts in Java

Abstraction

It is the property by which only useful details are shown to the user. It hides the unimportant parts of the user by identifying only the required things. We can have the process of abstraction using an interface and an abstract class.

OOPs concepts in Java

Polymorphism

Polymorphism is a process in java by which we can use a method in multiple ways. Or we make the method take different forms. The child class takes the parent class as its reference. This process is performed either by overloading in java or overriding in java. We use polymorphism when we aim at building an interface with different objects but the same form. When we create a new object in the program, it takes the form of previous or inherited objects of the parent class via polymorphism.

Hence we can perform many functions in various ways. Just like an animal can have various characteristics at a time. A leopard can be orange in colour, ferocious in nature and striped. He is so many things at the same time which is an example of polymorphism.

You can have better visions for polymorphism in Java.

Inheritance in Java

It is the key feature of OOP programming. This process allows one class to inherit another class including its objects and methods. It has:

Super Class: Super class is the parent class whose methods can be inherited.

Sub Class: The child class which inherits the parent class is the sub class. This class can have its own features as well in addition to the parent class.

Reusability: This concept says that if we want to form a class, we can reuse an existing class whose features we wish to use currently. As a result, we inherit the features and methods of the existing class.

Below is the synatx:

class derived-class extends child-class
{
//All methods and features
}

Encapsulation

The process wraps all the data under a single unit by binding the whole code together. It is protective for the data from any outer threat by declaring all the variables as private but methods remain public in the class. It hides the data from the other classes and shows only the relevant data, so also known as data hiding.    

class Classroom 
{ 
    private String Name; 
    private int RollNumber; 
    private int Age; 
  
    public int getAge(){ 
      return Age; 
    } 
 
    public String getName()  
    { 
      return Name; 
    } 
      
    public int getRollNumber()  
    { 
       return RollNumber; 
    } 
   
    public void setAge(int newAge) 
    { 
      Age = newAge; 
    } 
   
    public void setName(String newName) 
    { 
      Name = newName; 
    } 
       
    public void setRoll(int newRollNumber)  
    { 
      RollNumber = newRollNumber; 
    } 
} 

public class ClassroomMain
{     
public static void main (String[] args)  
{ 
Classroom obj = new Classroom();
obj.setName("My name is Megha"); 
obj.setAge(25); 
obj.setRoll(29); 
          
System.out.println("Name of the student: " + obj.getName()); 
System.out.println("Age of the student: " + obj.getAge()); 
System.out.println("Roll Number of the student: " + obj.getRollNumber());
} 
} 

The output of the program will be:

Name of the student: Megha
Age of the student: 25
Roll Number of the student: 29

Leave a comment

Your email address will not be published. Required fields are marked *