Tuesday, April 16, 2024
HomeJavaOOPS Concepts in Java

OOPS Concepts in Java

-

In software development, we provide solutions to real life problems of people. Whereas, object oriented programming(OOP) is a programming technique which is inspired by real life objects, their properties and behaviors. Hence, arised this object oriented approach. Java is one of the programming languages, which uses this object oriented paradigm. This article gives a brief overview of OOPS concepts in Java.

Object oriented programming or OOPS Concepts in java are basic and very important concept to understand how the java works.

List of OOPS Concepts in Java

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance

Related Links:

Java Interview Questions

Let’s have a look over these concepts one-by-one.oops concepts in java logo

1. Abstraction

Abstraction is a process of hiding the implementation details and displaying only functionality to the user. In other words, it only displays essential things to the user and inner details are hidden. For Example, whenever we are about to send an email, we just type the content to be sent and click upon send button to send the mail. But, we are not supposed to see the internal processing about the email delivery, that’s its beauty. Therefore, Abstraction lets you focus on what the object does instead of how it does it.

Ways to achieve Abstraction:

There are two ways to achieve abstraction in java. They are:

  • Abstract Class
  • Interface

Let’s consider it with an example:

Using Abstract Class:

//oops concepts in java example #1 - abstract class
abstract class Vehicle
{
   public abstract void engine();  
}
public class Car extends Vehicle {
    
    public void engine()
    {
        System.out.println("Car engine"); //car engine implementation        
    }
    
    public static void main(String[] args)
    {
        Vehicle v = new Car();
        v.engine();
    }
}
Output:
Car Engine

Using Interface:

//oops concepts in java example #2 - Interface Eg
interface MyInterface
{
   public void method1();
   public void method2();
}
class InterfaceDemo implements MyInterface
{
  public void method1()
  {
      System.out.println("method1 implementation");
  }
  public void method2()
  {
      System.out.println("method2 implementation");
  }
  public static void main(String arg[])
  {
      MyInterface obj = new InterfaceDemo();
      obj. method2();
  }
}
Output:
method2 implementation

 

2. Encapsulation

Encapsulation is subset of Abstraction. It is a technique used to implement abstraction concept. Encapsulation means hiding the code and data into a single unit to protect the data from outside world. It is a way of keeping some part of data private by allowing no access to it from outside. It is also known as Data Binding.

Ways to Achieve Encapsulation:

Encapsulation in java achieved using:

  • Access specifiers such as private, protected and public keywords.
  • setter and getter methods

Let’s take an example:

// oops concepts in java Example #3 Encapsulation Eg
public class EncapsulationDemo
	   {
		
private String name;		
		private int age;
		
		
public String getName()
		{
		 
return name;
		
}
		public int getAge()
		{
		 
return age;
		
}	
		
public void setName(String newName)
		{
		 
name = newName;

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

	   
public static void main(String args[])
		{
		 
EncapsulationDemo obj = new EncapsulationDemo();
		 
obj.setName("Coderefer");
		 
obj.setAge(3);		 
		 System.out.print("Name : " + obj.getName()+'\n' + " Age : " + obj.getAge());

		}
	   
}
Output:
Name : Coderefer
 Age : 3
3. Polymorphism

Polymorphism word itself mean that ‘capability of representing one form in multiple forms’. Any object in Java that passes more than one IS-A tests is said to be polymorphic in nature. Generally in Java, all objects are polymorphic in nature. Since, any object will pass minimum of two tests: one for itself and one for Object class.

An important usage of polymorphism occurs in oops is how a parent class refers to a child class object.

Example: 

Suppose if a person is at home, he behaves like a son to his parents. If he is in class, behaves like a student. So, here single person behaving in different forms.

Polymorphism in java has two types. They are:

  • Static Binding(Compile Time Polymorphism)
  • Dynamic Binding(Run Time Polymorphism)

Static Binding:

Static Binding also known as Method overloading/Compile-Time Polymorphism. Same method name is overloaded with different number of parameters in same class with different signature. In this mechanism overloaded method calls get resolved at compile time by the compiler.

Let’s us take a simple example:

//oops concepts in java example #4 - static polymorphism eg
class StaticPolymorphismDemo
{
    	void sum(int a, int b)
    
	{
        
		System.out.println(a+b);
   
        }
   
        void sum(int a, int b, int c)
  
        {
       
	        System.out.println(a+b+c);
    
	}


public static void main(String args[])
  {
    Addition add=new Addition();
    add.sum(10,20);   
    add.sum(10,20,30);
  }
}
Output:
30
60

Dynamic Binding:

Dynamic Binding also known as Method overriding/Run-Time Polymorphism. Same method is overridden with same signature in different classes.  In this Mechanism overridden method call get resolved at Run-Time.

Let’s look it with an example:

//oops concepts in java example #5 - dynamic polymorphism eg
public class DynamicPolymorphismDemo
 {

   
	 public static void main(String args[])
	 
    {
        
		Vehicle veh = new Car(); //here vehicle Reference but object will be Car
  
	        veh.start();       //Car's start called because start() is overridden 
	     }
 
}


class Vehicle
 
{

   
	 public void start()
    
	     {
       
		 System.out.println("Start method of a Vehicle");
   
	     }
 
}


class Car extends Vehicle

 {

   
 	 @Override
   
 	 public void start()
   
 	    {
        
		System.out.println("Start method of a Car");
   
  	    }

 }
Output:
Start method of a Car
4. Inheritance

Inheritance is one of the features of object oriented programming. It is a concept which allows to use properties and methods of one class in another. In other words, properties and behaviors from the base class is inherited into derived class. The class from which properties are inherited can be called as Super class or base class and class which inherits properties of base class can be called as Subclass or derived class.

Additional attributes and methods can be added by derived class. A super class can have any number of sub classes, but not vice-versa.

In order to inherit properties of base class by derived class, java offers extend keyword as shown below:

//oops concepts in java example #6 - inheritance eg
Class A
{
 //Members and methods declaration
}
Class B extends A
{
 //Members and methods are inherited from class A
 //Members and methods declaration of B
} 

Types of Inheritance:

  • Single Inheritance
  • Multi Level Inheritance
  • Hierarchical Inheritance

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

Packages in Java

Hello World! Today's blog is about packages in java. Here, we will learn about what are packages and how we can use packages along with...

House Robber Problem | Maximum Sum of non-adjacent elements

In this blog post, we will discuss about how to find maximum sum of Non-Adjacent elements. Since House Robber Problem is a typical DP Problem...

Java SimpleDateFormat Example

In Java SimpleDateFormat article we'll see how to use java.text.SimpleDateFormat class, which provides methods to format date to string or parse string to date. The SimpleDateFormat is a...

Exception Handling in Java

While coding, we may face few compile time errors because of incorrect syntax and those errors can be removed by re-correcting our syntax. Similarly, we...

Follow us

1,358FansLike
10FollowersFollow
401SubscribersSubscribe

Most Popular