OOPS concepts Overview


Let us understand OOPS Concepts and its importance in this article


There are many popular programming languages like Java,c++,c# etc follow an Object Oriented Programming paradigm.

Object Oriented Programming system(OOPS) is designed based on the concept of “Object”.

“Object” contains both variables (used for holding the data) and methods(used for defining the behaviors)

OOPS concept brings this data and behavior in a single place called “class” and we can create any number of objects to represent different state for each object.

OOPS Concepts are very important mainly because without having idea of OOPS concepts, we will not be able to design systems in object oriented programming model.


oops_overview


Class and objects

Class represents a real world entity which acts as a blueprint for all the objects.

We can create as many objects as we need using Class.

Example:

We create a class for “ Student ” entity as below

Student.java

  1. Class Student{
  2. String id;
  3. int age;
  4. String course;
  5. void  enroll(){
  6.       //Logic for enrolling student
  7.    System.out.println("Student enrolled");
  8.  }
  9. }
Class Student{
String id;
int age;
String course;
void  enroll(){
      //Logic for enrolling student
   System.out.println("Student enrolled");
 }
}


Above definition of class contains 3 fields id,age and course and also it contains 1 behavior or a method called “ enroll ”

Now we can create any number of objects using this class and all those objects will get the same fields and behavior.

  1. Student s1 = new Student();
  2.  
  3. Student s2 = new Student();
  4.  
  5. Student s3 = new Student();
Student s1 = new Student();

Student s2 = new Student();

Student s3 = new Student();


Now we have created 3 objects s1,s2 and s3 for the same class “ Student ”.

We can create as many objects as required in the same way.

We can set the value for each field of an object as below

  1. s1.id=123;
  2. s1.age=18;
  3. s1.course=”computers”;
s1.id=123;
s1.age=18;
s1.course=”computers”;


We can call the method inside class “ Student ” using object “ s1 ” as below

  1. s1.enroll();
s1.enroll();


Similarly we can provide different values for each field for “s2” and “s3” and we call this as a “ state ” of an object.

Check class-and-objects-in-java article to know more about Class and Objects


Abstraction

Abstraction is the concept of hiding the internal details of a functionality and providing a simple representation for the same.


So complex functionality can be made available to the outside world in a simple way.

Example:
We use mobile phone everyday but we don’t know how the functionalities are designed inside it so that we receive the calls and send messages etc.

These functionalities have been kept inside and we are just accessing them using the options provided in the mobile.

Similarly, we drive a car and change the gears as and when its required to go high speed and come back to low speed but We don’t know how the gear box works internally.

So, gearbox working is abstracted and provided with simple mechanism of changing gears to the outside world.

Similary, we apply brakes whenever we want to stop the car but we are not sure how the braking system works internally.

Similarly, in java we can write a method to perform some functionality inside a class and we can expose it to outside world just by providing an option to call this method.

So, anyone who calls this method will not be knowing the internal complexity of the method but will be knowing the functionality of the method and hence he calls it and uses it.

So, this way, we hide the internal implementation and abstract it inside a method.

We can achieve “ abstraction ” in Java using 2 ways

1. Abstract class

2. Interface


1. Abstract Class

Abstract class in Java can be created using “ abstract ” keyword.

If we make any class as abstract then it can't be instantiated which means we are not able to create the object of abstract class.


Inside Abstract class, we can declare abstract methods as well as concrete methods

So using abstract class, we can achieve 0 to 100 % abstraction.

Example:

  1. Abstract class Phone{
  2. Abstract void receiveCall();
  3. Abstract void sendMessage();
  4. }
Abstract class Phone{
Abstract void receiveCall();
Abstract void sendMessage();
}


Now any concrete class which extends the above abstract class will provide the definition of these abstract methods.

This definition is the actual internal details of how this functionality has been implemented.

Anyone who needs to access this functionality has to call the method using the Phone object pointing to its subclass.

Check Abstract Class in Java article for more details on Abstract class and methods


2. Interface

Interface is used to achieve pure or complete abstraction.


We will have all the methods declared inside Interface as abstract only.

So, we call interface as 100% abstraction.

Example:
We can define interface for Car functionality abstraction as below

  1. Interface Car{
  2. public void changeGear( int gearNumber);
  3. public void applyBrakes();
  4. }
Interface Car{
public void changeGear( int gearNumber);
public void applyBrakes();
}


Now these functionalities like changing a gear and applying brake are abstracted using this interface.

Actual implementation will be provided by the sub-classes which implements the above the interface.

  1. Class Lamborghini implements car {
  2. public void changeGear( int gearNumber){
  3. //Actual logic to change the gear
  4. System.out.println("Gear changed in Lamborghini car to "+gearNumber);
  5.  
  6. }
  7.  
  8. public void applyBrakes(){
  9. //Actual logic to apply the brake
  10. System.out.println("Break applied in Lamborghini car ");
  11.  
  12. }
  13. }
Class Lamborghini implements car {
public void changeGear( int gearNumber){
//Actual logic to change the gear
System.out.println("Gear changed in Lamborghini car to "+gearNumber);

}

public void applyBrakes(){
//Actual logic to apply the brake
System.out.println("Break applied in Lamborghini car ");

}
}


Similarly , we can implement this interface for Audi car

  1. Class Audi implements car {
  2. public void changeGear( int gearNumber){
  3. //Actual logic to change the gear
  4. System.out.println("Gear changed in Audi car to "+gearNumber);
  5.  
  6. }
  7.  
  8. public void applyBrakes(){
  9. //Actual logic to apply the brake
  10. System.out.println("Break applied in Audi car ");
  11.  
  12. }
  13. }
Class Audi implements car {
public void changeGear( int gearNumber){
//Actual logic to change the gear
System.out.println("Gear changed in Audi car to "+gearNumber);

}

public void applyBrakes(){
//Actual logic to apply the brake
System.out.println("Break applied in Audi car ");

}
}


So, we can see that, internal working of gear box and the brake system is implemented in the subclass and its abstracted using the interface.

Anyone who would like to use this functionality can call the appropriate method using interface variable pointing to the required implementation as below

  1. Car c = new Audi();
  2. c. changeGear(2);
  3. c. applyBrakes();
Car c = new Audi();
c. changeGear(2);
c. applyBrakes();


Since variable “c” is pointing to Audi class object , we can see below output


Check Interfaces in java article for more details


Encapsulation

Encapsulation is the process of binding object state(fields) and behaviors(methods) together in a single entity called “Class”.


Since encapsulation wraps both fields and methods in a class, it will be secured from the outside access.

We can restrict the access to the members of a class using access modifiers such as private,protected and public keywords

When we create a class in Java, It means we are doing encapsulation.

Encapsulation helps us to achieve the re-usability of code without compromising the security.

Check Encapsulation in Java article for more details


Inheritance

One class inherits or acquires the properties of another class.


The class from which the properties are inherited is called Super class or Parent class

The class which inherit the properties is called Subclass or Child class.

We use “ extends ” keyword to achieve the same.

The whole idea behind inheritance is “ Re-usability ”.

We can create more specific classes by inheriting fields and methods from the Generic class.

And we can add additional fields and methods in the Subclass.

There are several types of Inheritance supported in Java

Check Inheritance in Java article for more details on Inheritance


Polymorphism

Polymorphism is the concept where an object behaves differently in different situations

Since the object takes multiple forms, it is called Polymorphism.


In java, we can achieve polymorphism using method overloading and method overriding

There are 2 types of Polymorphism available in Java

Method overloading

In this case, which method to call will be decided at the compile time itself based on number or type of the parameters


Static/Compile Time polymorphism is an example for method overloading.

Method overriding

In this case, which method to call will be decided at the run time time based on what object is actually pointed by the reference variable.


Run Time Polymorphism is an example for method overriding.

Check Polymorphism in Java article for more details on Polymorphism.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on