Skip to main content

Object Oriented Programming (OOP) concept in Java | Programming blog

OOPs Concept in java programming

What is object oriented programing (OOP) in Java

OOP :- OOP refers as Object Oriented Programming. OOP is a concept that is based on objects. As a name suggests it is Object oriented. Java is Object oriented programming language. 

There are mainly four concepts in java :-

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Let's discuss all of above one by one.

1. Abstraction :-

In simple term, Abstraction means hiding complexity and showing essential details to the user. Means using the things without knowing background details about it. 

Abstraction is the technique of hiding implementation.

Like, we use smartphone very efficiently and easy way, we don't need background details how it works. we simply use.

Why should we use Abstraction?

  • Reduce complexity
  • Force users to extend implementation rather than modify
  • Support cross platform by changing the implementation per platform

In Java, we can use abstraction in two ways :-

  1. Using Abstract class
  2. Using Interface
Learn more about Abstract class and Interface
 

2. Encapsulation

Encapsulation means wraps variables or functions into single unit. means we can create private fields in class so any other class can not access its data. 

In encapsulation, data in a class is hidden for another classes, so it is also known as data-hiding.

For use private variables we can use public getter and setter method in that class. so we can access private variables.

Example of encapsulation

Encapsulation in java

Why we should use Getter and Setter methods?

This question confuse every new programmer who learn Encapsulation in oops. 

Why we should use getter and setter methods if we can directly access and use variable using defining public? Why we use getters and setters?

So, simple answer is we set limit in variable access using getter and setter methods. Like if anyone wants to get or set above name variable then there is only one option using getName() and setName() method.

Another main use of getter and setter, that we can put any validation in these methods. like in above example if we want to set name that is not have greater than 10 length. so we can easily add these type of validation in above method. so, anyone who want to set name then they must have to set name in less than 10 length.

3. Inheritance 

Inheritance means use the properties of one class to another class. Inheritance provides ides of reusability of code. 

extends keyword is used for access base or parent class properties in child or sub class.

Subclass defines own unique properties as well as extends another class ( parent class or base class ) and uses its properties. this is done by inheritance.

There is two class Student and Teacher that have common properties like name, DOB, department, etc. Student have some unique properties like student id, college fee, scholarship, etc. and Teacher have salary, teacher id, etc. so we can define another class that have common properties and extends that class into Student and Teacher class.

So, lets see simple example of inheritance.

public class OOPLanguage {

    public void OOP() {
        System.out.println("Object Oriented Programming");
    }  

public class Java extends OOPLanguage {

    public static void main(String[] args) {
       
        Java javaObj = new Java();
        javaObj.OOP();
       
    }
}

Output :-
Object Oriented Programming

4. Polymorphism

Polymorphism means there is two or more method whose name are same but its parameters are different.

Polymorphism means perform more than one action using single unit.

In java, we can perform polymorphism in two ways.

  1. Compile time polymorphism or static binding
  2. Run time polymorphism or dynamic binding

1. Compile time polymorphism

It is also knows as Method Overloading in java. Method overloading means there is same method name but different parameters in same class. 

In compile time polymorphism, which method is call decides at compile time.

public class ClassOne{
       
        public void print() {
            System.out.println("Class one");
        }

        public void print(String str) {
            System.out.println("Class one" + str);
        }

 }
 

2. Run time polymorphism

It is also known as Method Overriding in java. Method overriding means same method name and different parameters in same class.

public class ClassOne{
    
    public void print() {
        System.out.println("Class one");
    }

}

public class ClassTwo {
    
    public void print() {
        System.out.println("Class two");
    }

}

In run time polymorphism, which method is call decide at run time.

Other articles you may like :-

Comments

Popular posts from this blog

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.st