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.
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
- Class Student{
- String id;
- int age;
- String course;
- void enroll(){
- //Logic for enrolling student
- System.out.println("Student enrolled");
- }
- }
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.
- Student s1 = new Student();
- Student s2 = new Student();
- 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
- s1.id=123;
- s1.age=18;
- 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
- 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:
- Abstract class Phone{
- Abstract void receiveCall();
- Abstract void sendMessage();
- }
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
- Interface Car{
- public void changeGear( int gearNumber);
- public void applyBrakes();
- }
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.
- 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 ");
- }
- }
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
- 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 ");
- }
- }
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
- Car c = new Audi();
- c. changeGear(2);
- 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.
very clearly explained.
great content….made it very easy to understand.