JAVA is an object oriented language. It has four main object oriented concepts. They are as follows.
- Abstraction:- In an Object oriented language the inner details are kept hidden from the outside world, only essential features are visible to the user. This is called Abstraction.
- Encapsulation :- To bind the data and functions together in a unit called class is the second concept of OOPS concept called Encapsulation. The purpose of encapsulation is to keep the data safe from an unauthorized access.
- Inheritance:- When one class inherits the properties of another class it is called Inheritance(We can relate this concept with the real life word called “Heredity”, We all acquire physical attributes, intellectual features, financial properties and our DNA from our parents). The class that gives it properties is called a parent class and the class that acquires the properties is called a child class.
For example class Rectangle, Triangle, Circle acquire the properties of class shape. So in this case Shape is called super/Base/Parent class and Rectangle, Triangle and circle are called sub/Derived/Child classes.
In java there are different types of inheritance involved.
- Single Inheritance.
- Multiple Inheritance (Through Interface)
- Multilevel Inheritance.
- Hierarchical Inheritance.
- Hybrid Inheritance (Through Interface)
4. Polymorphism:- This is a feature which literally means “Many”+”Forms”. When a function can behave in more than one different form depending upon the object that has called it, this is called polymorphism. Lets take an example of polymorphism. A draw() function that is defined in the classes shape , triangle, rectangle and circle will behave differently when called with objects of different classes.
In java polymorphism can be achieved with two different concepts, function overloading and function overriding.
Function Overloading is a technique to define more than one function with the same name but different parameter list in a class.
Function overriding is a technique where one function with the same name and the same parameter list is defined in a super class and all the sub classes. For achieving function overriding , all the classes must implement the concept of inheritance. The function that will be called depends on the object of which class that is calling the function. Here basically we can say that the function of the super class is overridden by the functions of the sub classes.