JAVA is an Object oriented programming language. OOPs concepts include abstraction, encapsulation, inheritance and polymorphism.
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. Data Abstraction is the property by which only the essential details are displayed to the user. The non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components
- 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. Encapsulation can be achieved by: Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. In encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared.
- 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. In this inheritance, a derived class is created from a single base class. For example, Class A is the parent class and Class B is the child class since Class B inherits the features and behavior of the parent class A.
- Multiple Inheritance (Through Interface) In this inheritance, a derived class is created from more than one base class. This inheritance is not supported by Java Language.
- Multilevel Inheritance. In this inheritance, a derived class is created from another derived class.
- Hierarchical Inheritance. In this inheritance, more than one derived classes are created from a single base class and further child classes act as parent classes for more than one child classes.
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.