OOPs concepts in Java

OOPs concepts in Java

In this Tutorial, we will learn about object oriented programming concept aka oops concepts in Java.

Object-Oriented Programming System (OOPs) is a programming concept that works on the principles of abstraction, encapsulation, inheritance, and polymorphism. It allows users to create objects they want and create methods to handle those objects. The basic concept of OOPs is to create objects, re-use them throughout the program, and manipulate these objects to get results.

OOP meaning β€œObject Oriented Programming” is a popularly known and widely used concept in modern programming languages like 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

Class

The class is one of the Basic concepts of OOPs which is a group of similar entities. It is only a logical component and not the physical entity. Lets understand this one of the OOPs Concepts with example, if you had a class called β€œExpensive Cars” it could have objects like Mercedes, BMW, Toyota, etc. Its properties(data) can be price or speed of these cars. While the methods may be performed with these cars are driving, reverse, braking etc.

Syntax :

class classname {
type instance variable 1;
type instance variable 2;
.
.
.
type instance variable n;
type methodname 1 (parameter list) {
// body od method 
}
type methodname 2 (parameter list) {
// body od method 
}
type methodnamen (parameter list) {
// body od method 
}
 }

Object

An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An Object is one of the Java OOPs concepts which contains both the data and the function, which operates on the data. For example – chair, bike, marker, pen, table, car, etc.

Abstraction

Abstraction is a process which displays only the information needed and hides the unnecessary information. We can say that the main purpose of abstraction is data hiding. Abstraction means selecting data from a large number of data to show the information needed, which helps in reducing programming complexity and efforts.  

There are also abstract classes and abstract methods. An abstract class is a type of class that declares one or more abstract methods. An abstract method is a method that has a method definition but not implementation. Once we have modelled our object using data abstraction, the same sets of data can also be used in different applicationsβ€”abstract classes, generic types of behaviours and object-oriented programming hierarchy. Abstract methods are used when two or more subclasses do the same task in different ways and through different implementations. An abstract class can have both methods, i.e., abstract methods and regular methods.

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

Example :

// Abstract parent class
abstract class Animal {
    // Abstract method
    public abstract void sound();
}

public class Lion extends Animal {
    public void sound() {
        System.out.println("roar");
    }

    public static void main(String[] args) {
        Animal obj = new Lion();
        obj.sound();
    }
}


Output :

Roar

Polymorphism

Polymorphism refers to one of the OOPs concepts in Java which is the ability of a variable, object or function to take on multiple forms. For example, in English, the verb run has a different meaning if you use it with a laptopa foot race, and business. Here, we understand the meaning of run based on the other words used along with it. The same also applied to 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

Inheritance is a mechanism of deriving one class from another. It allows the derived class to inherit features from a parent class, the features include (fields and methods). The parent class is also known as the superclass or base whereas the derived class is known as a subclass or child class.

  • Inheritance is a property of OOPs in which member functions and data members of one class can be used by another class.
  • Reusability of code can be achieved because of inheritance. If we want to create a class that already has some common methods which we want to define within another class, we can use the already existing class as a base class or a parent class and then inherit from it.
  • Hence the code can be reused as we don’t need to write the same piece of code again and can use it in the derived class or child class.
  • Base class- a class from which member functions and data members are used by another class.
  • Derived class- a class that uses the properties of the base class and can also add its properties.

In our example, we created a class “Plane” which contained attributes like plane_number, total_seats, airline_class, and pilot_name. All the planes are of the sub-type of class “Plane” and they have common attributes mentioned above. So in addition planes have their distinct attributes like luxury_arrangements, food, and air hostess. So instead of defining the common properties again, we can only define the distinct properties in the new class and inherit the common properties from the base class.

Types of Inheritance :

  • Single
  • Multilevel
  • Hybrid
  • Hierarchical

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

Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.

https://www.facebook.com/developerhelps

Leave a comment

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

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!