Java OOPS Concepts

September 1, 2018 Off By ravi24kant

Java OOPS Concepts

Java OOPS concepts area unit the rear bone of Java, wherever Object oriented programming is aprogramming Technic that is predicated on OOPs concepts like Inheritance, Encapsulation, Abstraction, Polymorphism.
There are several standard programming languages follow an object oriented programming like Java, C++, C# etc.
In this article, we’ll point out object oriented programming ideas in Java. Any object-based application in Java is predicated on declaring categories, making objects from them and interacting between these objects.
we will perceive the below core ideas of Object oriented Programming. thus here we have a tendency to go and see some attention-grabbing Java OOPS concepts in Java World

Abstraction.
Encapsulation.
Polymorphism.
Inheritance.
Ohhh hold on hold on…., before we have a tendency to discuss the OOPS ideas, Lets see, what’s Object directed Programming.

Object oriented program

Object orientating Program that additionally known as Object oriented Programming is a programming concept that works on the principle of  objects that are the foremost necessary a part of your program. currently you need to be thinking what’s Object means in Java. thus you’ll suppose like that as we’ve got objects in real world, equally we’ve got objects in Java world.

Object oriented Programming permits users to create the objects that they require so they produce ways to handle those objects. Manipulating these objects to urge results is that the goal of Object oriented Programming.

Java OOPS Concepts

Now Lets discuss Java OOPS Concepts

  1. Abstraction

    :- An abstraction is an act of representing essential options without including background details. it’s a wayof making a new knowledge kind that’s fitted to a particular application. for instance, whereas driving a automobile, you do not need to be troubled with its internal operating. Here you only have to be compelled to concern about components like wheel, Gears, accelerator, etc. There area unit many ways to attain abstraction in object oriented programming, like encapsulation and inheritance.

    You can deliver the goods abstraction in 2 ways:

    a) Abstract class

    b) Interface

    Let’s perceive these concepts in additional detail.

    Abstract category: Abstract class in Java contains the ‘abstract’ keyword. now what will the abstract keyword mean? If a category is declared abstract, it can’t be instantiated, which implies you can’t produce an object of an abstract class. Also, an abstract class will contain abstract in addition as concrete methods.

    To use an abstract category, you’ve got to inherit it from another category wherever you’ve got to produceimplementations for the abstract ways there itself, else it’ll conjointly become an abstract class.

    Interface: Interface in Java is a blueprint of a class or you can say it’s a group of abstract methods and static constants. In an interface, every method is public and abstract however it doesn’t contain any constructor. together with abstraction, interface conjointly helps to attain multiple inheritance in Java.
    Note: you’ll deliver the goods 100 percent abstraction using interfaces.

    So an interface primarily is a cluster of connected methods with empty bodies.

  2. Encapsulation

    :-Encapsulation is the technique used to implement abstraction in object oriented programming by wrapping the data and code. In this OOPS concept, the variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class. A very simple example – a student cannot exist without a class in school.

    As you can see in the above Diagram, when we keep class, method and object together that becomes Encapsulation. It is also known as data hiding.To achieve encapsulation in Java −

    • Declare the variables of a class as private.
    • Provide public setter and getter methods to modify and view the variables values.
    • public class Student{
         private String name;
         private int age;
         public int getAge() {
            return age;
         }
         public String getName() {
            return name;
         }
      }
  3. Polymorphism

    :-Polymorphism is the concept where an object behaves differently in different situations. There are two types of polymorphism – compile time polymorphism and runtime polymorphism (Lets Park it for now). For example, in English, the verb “run” has a different meaning if you use it with “a Software Program,” “a Marathon, and ”business. Here, we understand the meaning of “run” based on the other words used along with it.The same also applied to Polymorphism.

    As an example in above picture, we can see Triangle,Rectangle and Circle are Shape. That means Triangle, Rectangle and Circle are polymorphic effect of Shape. Ideally all three are shapes.

    Let have another example in JAVA , where Animal is the Parent Class and Horse and Cat Class which are extending the Animal Class have their own Sound Methods respectively.

    public class Animal{
       ...
       public void sound(){
          System.out.println("Animal is making a sound");   
       }
    }
    public class Horse extends Animal{
    ...
        @Override
        public void sound(){
            System.out.println("Neigh");
        }
    }
    public class Cat extends Animal{
    ...
        @Override
        public void sound(){
            System.out.println("Meow");
        }
    }
  4. Inheritance

:- It is the mechanism in java by which one class is allow to inherit the features like fields and methods of  another class. Inheritance is the Pillar of any Object Oriented program. computer programs are designed in such a way where everything is an object that interact with one another.Inheritance is one such concept where the properties of one class can be inherited by the other. It helps to reuse the code and establish a relationship between different classes.

for Example here in the snap below, Students, Employee and GrandFather inherit are actully some Person and has all the properties of Person