Maybe the true sense of studying Java is to understand the various ‘Object oriented programming’ concepts coming into play. Apart from the fundamental definitions of object, class and variable which were already discussed in an earlier post, let us see the various Object oriented programming or OOPs concepts in this post. We may have seen them but we may not have understood that it is one of OOPs concept.
The four major object oriented programming concepts are:
- Encapsulation
- Polymorphism
- Inheritance
- Abstraction
Abstraction:
The primary aim of the OOPs concept is to hide the implementation from the user. Abstraction is the way in which this is achieved. We have already seen the concept of ‘abstraction’ in our ‘abstract modifier‘ and interfaces post.
To recap, an abstract method is one where implementation is not provided. When there are one or more abstract methods defined in a class, it is termed as an abstract class. When a user extends the abstract class, he/she provides implementation for the same.
An interface also contains the skeletal system of the Class. The entire implementation of the interface is done by the class that implements the interface.
These two concepts reinforce the concept of ‘Abstraction’ which is one of the important OOPs concept.
Inheritance:
Inheritance is again another OOPs concept that we have already seen. We all know that Java only supports single inheritance ie. a class can only extend from one super class. Inheritance ensures that a class gets the additional functionality of a super class.
Inheritance is established by using the ‘extends’ keyword in Java like the following:
class first{ //super class
void add(){
System.out.println("Hello world");
}
}
public class hello extends first{ //sub-class
public static void main(String args[]){
}
}
The class ‘hello’ gets all the methods and variables of class first. This is the concept of inheritance.
3. Encapsulation
Encapsulation is hiding the data and making sure that it is not accessible easily. We also ensure that the hidden variables and the methods to access them are wrapped together.
We have again briefly skimmed through this concept in Java modifiers post. Can you guess which access modifier ensures that variables are totally hidden? It is the ‘private’ access modifier. There are other ‘getter’ and ‘setter’ methods to set the values of the private variables and retrieve the private variables. This bring out the OOPs concept of ‘encapsulation’.
4. Polymorphism
Polymorphism in Java is yet another OOPs concept. Polymorphism is the ability of an object to take many forms.
There are different types of polymorphism such as static polymorphism and dynamic polymorphism in Java.
Static polymorphism is implemented using overloading methods in Java.
Dynamic polymorphism is implemented by overriding methods in Java.
We have already seen these two concepts in an earlier post.
Do come back tomorrow for a personal post! 🙂
I’m participating in #BlogchatterA2Z by BlogchatterÂ