Object-Oriented Programming in Java
Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”,So, let’s first know about objects -
Object — Objects are key to understanding object oriented technology. Anything in the real world that can be perceived is object. For example — human, dog, desk etc.
These real world object share two characteristic : state and behavior.
Things an object know about itself are called state (instance variable).
Things an object can do are called behavior (methods).
Class — A class is a blueprint for an object. It tells the virtual machine how to make an object of that particular type. Each object made from that class have its own values for the instance variable of that class.
For example, you might use the button class to make the dozen of different buttons, and each button have its own size, color, shape, label, and so on.
In OOPs concepts, we will learn four major principles — abstraction, encapsulation, inheritance, and polymorphism. They are also known as four pillars of the object oriented programming paradigm.
Abstraction :
Hide internal implementation and just highlight the set of services, is called abstraction. By using abstract classes and interfaces we can implement abstraction.
Example :
By using ATM GUI screen bank people are highlighting the set of services what they are offering without highlighting internal implementation.
The main advantages of Abstraction are:
1. We can achieve security as we are not highlighting our internal implementation.(i.e., outside person doesn’t aware our internal implementation.)
2. Enhancement will become very easy because without effecting end user we can able to perform any type of changes in our internal system.
3. It provides more flexibility to the end user to use system very easily.
4. It improves maintainability of the application.
5. It improves modularity of the application.
6. It improves easyness to use our system.
Encapsulation
Most of the instance variable are coded within certain assumption about the boundries. Like think of all thing that would break if negative number allowed. So, we are not going to expose our data.
In the encapsulation technique, we declare fields as private in the class to prevent other classes from accessing them directly. The required encapsulated data can be accessed by using public Java getter and setter method.
The process of binding data and corresponding methods (behavior) together into a single unit is called encapsulation in Java.
If any java class follows data hiding and abstraction such type of class is said to be encapsulated class.
Encapsulation=Data hiding+Abstraction
For example-
Let us consider a car. A car consist of various component like engine, chassis etc to protect it from the outer world, it is covered through the body in a single unit.
The main advantages of encapsulation are :
1. We can achieve security.
2. Enhancement will become very easy.
3. It improves maintainability and modularity of the application.
4. It provides flexibility to the user to use system very easily.
Polymorphism:
Polymorphism is derived from two Greek words: poly and morphs. The word “poly” means many and “morphs” means forms. So polymorphism means many forms.
Lets take an example of shape class, shape can be any of type it may be circle, rectangle, or may be triangle etc this shows the different form of the shape class.
In java we can achieve polymorphism in two ways -
1.Compile time polymorphism
2. Run time polymorphism
Compile time polymorphism :
A polymorphism that is resolved during compile time based on the reference type is known as static polymorphism. Method overloading is an example of compile time polymorphism.
Run time polymorphism :
Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, that’s why it is called runtime polymorphism.
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.
Inheritance :
When you design with the inheritance, you put common code in a class and tell other more specific classes that the common(more abstract) class is their class is superclass. When one class inherits from another, the subclass inherits from the superclass.
In java we say that the subclass extends the superclass.An inheritance relationship means that the subclass inherits the members of the superclass. When we say “members of a class” we mean the instance variables and methods.
The relationship between two classes is called is-a relationship.
For example, if PantherMan is a subclass of SuperHero, the PantherMan class automatically inherits the instance variables and methods common to all superheroes including suit, tights, specialPower, useSpecialPower() so on. But the PantherMan subclass can add new methods and instance variables of its own, and it can override the methods it inherits from the superclass SuperHero. FriedEggMan doesn’t need any behavior that’s unique, so he does not override any methods.
Java supports three types of inheritance : single, multilevel and hierarchical inheritance.
Single inheritance :
When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.
Multilevel inheritance :
When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.
Hierarchical inheritance :
When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.