Java OOPS Concepts

What is java program?

Java is a pure object oriented programming language. OOPS concepts are Class, Object, Inheritance, Polymorphism, Abstraction, and Encapsulation.

What is class?

  • Class is a blueprint or prototype that defines the fields and the methods (also called functions).
  • This is the template or pattern for object to be created. Every objects should follow class definition.
  • class keyword is used to define the new data type. once defined, this new type can be used to create the objects of that type.
  • A class is a template for an object, and an object is an instance of a class.
General structure of a class:
class classname {
 type instance-variable1;
 type instance-variable2;

 type method1(parameter1, parameter2, ...){

 type method2(parameter1, parameter2, ...){

 //more methods....
class Box{                                                                      
 double width;                                                                  
 double height;                                                                 
 double depth;    

 double calculateVolume(double width, double height, double depth);                                                              

What is object?

Program around its data is called object. object refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures.
// Java Class Example
class Box{                                                                      
 double width;                                                                  
 double height;                                                                 
 double depth;                                                                  
class BoxDemo {                                                                 
 public static void main(String args[]) {     
  //Object creation for class Box. here 'b'is the object name                                  
  Box b= new Box();                                                             
  double volume;                                                                
  b.width = 20;                                                                 
  b.height = 15;                                                                
  b.depth = 10;                                                                 
  volume = b.width * b.height * b.depth;                                        
  System.out.println("Box Volume is " + volume);                                
Box Volume is 3000.0

What is inheritance in java?

  • Inheritance is the process by which one object acquires the properties of another object.
  • Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of parent object.
  • It allows to create the hierarchical classifications.
  • Using inheritance, create a general class (is also called superclass or baseclass) that defines common set of related items. This class can then be inherited by other class(subclass).
  • A subclass is a specialized version of superclass.
  • extends keyword is used for inheriting one class into another class.
// Java inheritance example
class Parent {                                                                  
 int a, b;                                                                      
 void showab() {                                                                
 System.out.println("a and b: "+  a + " "  + b);                                
class Child extends Parent {                                                    
int c;                                                                          
 void showc() {                                                                 
 System.out.println("c: "+ c);                                                  
 void sum() {                                                                   
  System.out.println("sum(a+b+c): " + (a +b+c));                                
class SimpleInheritance {                                                       
 public static void main(String args[]) {                                       
  Parent superObj = new Parent();                                               
  Child subObj = new Child();                                                   
  superObj.a = 20;                                                              
  superObj.b = 30;                                                              
  subObj.a = 11;                                                                
  subObj.b = 12;                                                                
  subObj.c = 13;                                                                

a and b: 20 30

a and b: 11 12
c: 13

sum(a+b+c): 36

What is polymorphism in java?

Polimorphism (from the Greek, meaning "many forms") is a feature that allows one interface to be used for a general class of actions.
In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
There are two different types of polymorphism.
  • Method Overloading or Compile time polymorphism
  • Method Overriding or Run time polymorphism.

What is Method Overloading?

Method Overloading is a feature that allows a class can have two or more methods having same method name if number of arguments or data type of arguments are different.
Compiler throws error if two methods having same method name and arguments(same signature).
Method overloading is done in the same class or subclass(derived class).
// Java polymorphism example
class Number                                                                    
    void show(int a)                                                            
       System.out.println ("a: " + a);                                          
    void show(int a, int b)                                                     
       System.out.println ("a and b: " + a + "," + b);                          
    double show(double a) {                                                     
       return a;                                                                
class OverloadTest                                                              
    public static void main (String args [])                                    
        double result;                                                          
        Number obj = new Number();                                             ;                                                          , 12);                                                       
        result =;                                                
        System.out.println("double a: " + result);                              
a: 15
a and b: 11,12
double a: 59.5

What is Method Overriding?

Sub class method overrides the base class method if base class and sub class have same method name with arguments.
Applies only in inherited methods.
Object type assigned to reference variable type determines which overridden method will be used at runtime.
Static and final methods cannot be overridden.
Constructors cannot be overridden
class Parent                                                                    
 public void show(int a) //Base class method                                    
  System.out.println ("Parent a:" + a);                                         
class Child extends Parent                                                      
 public void show(int a) //Derived Class method                                 
  System.out.println ("Child a:" + a);                                          
class OverrideTest                                                              
 public static void main (String args []) {                                     
 // Parent class reference and object                                           
 Parent obj1 = new Parent();                                                    
 // Parent class reference but Child class object                               
 Child obj2 = new Child();                                                      
 // Calling method from Parent class                                    ;                                                                 
 //Calling method from Child class                                      ;                                                                 
Parent a:15
Child a:20

What is abstraction in java?

Abstraction is a process in java used to hide certain details and only show the essential features of the object.
  • When designing your classes, Abstraction helps to hide internal implementation from others as far as possible.
  • In OOPS, abstraction is a process of hiding the implementation details, only the functionality will be provided to the user.
  • User will have the information on what the object does instead of how it does it.
  • This deals with the outside view of an object.
  • Abstraction is used to hide the implementation complexity.
  • Abstract class and interfaces are used to implement abstractions in java code.
  • Abstract keyword is used to implement abstract class or methods.
Abstract Class:
  • Abstract class cannot be instantiated.
  • Abstract class may or may not have abstract methods.
  • Abstract method is declared or defined in a class, then class must be defined as abstract.
  • Abstract class can be used in java code as inherit the abstract class and provide implementation for all abstract methods in sub class.
//abstract class                                                                
abstract class AbstractClass {                                                  
 //abstract method                                                              
 abstract void method1();                                                       
 //We can define concrete methods still in abstract class.                      
 void method2() {                                                               
  System.out.println("This is a concrete method in abstract class");            
class ChildClass extends AbstractClass {                                        
 void method1() {                                                               
  System.out.println("AbstractClass method1 implementation in sub class.");     
class AbstractTest {                                                            
 public static void main(String args[]) {                                       
  //throws compiler error for below commented code                              
  //AbstractClass obj = new AbstractClass();                                    
  ChildClass obj = new ChildClass();                                            
AbstractClass method1 implementation in sub class.
This is a concrete method in abstract class

What is encapsulation in java?

Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.
  • This is also known as data hiding.
  • Encapsulation is as a protective wrapper(class) that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper(class).
  • Access to the code and data inside wrapper is tightly controlled through a well defined interface.
  • Encapsulated class can be created in java by making all the data members of the class private, and setter and getter methods can be defined as public to set and get the data in the class.
  • Instance variable's of a class can be made read-only or write-only.
  • A class can have entire control over instance variables what is stored.
// Java Encapsulation example
class EncapsulationClass {                                                      
 private String field1;                                                         
 private int field2;                                                            
 private double field3;                                                         
 public String getField1() {                                                    
  return field1;                                                                
 public int getField2() {                                                       
  return field2;                                                                
 public double getField3() {                                                    
  return field3;                                                                
 public void setField1(String updatedField1) {                                  
  field1 = updatedField1;                                                       
 public void setField2(int updatedField2) {                                     
  field2 = updatedField2;                                                       
 public void setField3(double updatedField3) {                                  
  field3 = updatedField3;                                                       
class EncapsulationTest {                                                       
public static void main(String args[]) {                                        
 EncapsulationClass obj = new EncapsulationClass();                             
 System.out.println("Field1: " + obj.getField1());                              
 System.out.println("Field2: " + obj.getField2());                              
 System.out.println("Field3: " + obj.getField3());                              
Field1: TestUser
Field2: 15
Field3: 100.5

Java Examples

GCD and Rational Number Java Program

Java Queue Program using exception

Java Stack Program using exception

Addition of three integers java program

Biggest of three integers java program

Fibonacci numbers java program

Arithmetic Operations Menu java program

Second Smallest Element In Array Java Program

Transpose Of A Matrix Java Program

Java Program to Display triangle of stars (*)

Java Programming Prints Product Tables

Java Program to Display triangle of numbers

Java Program to Get Current Date

Java Program to Find Character Vowel or Consonent

Java Program to Compute HCF and LCM

Java Program to Sum the Command Line Integer Arguments

Java Programm to Multiply the Command Line Integer Arguments

Java Program to Find Multiplication of Command Line Floating Point Numeric Arguments

Java Program to Check String Contains or Not

Java Program to Get Grade Description using Switch

Java Program for CSV File Reader

Java Program to Find Character Frequency Count in String using for each

Java Program to Find Min from Integer Array by Passing Array to Method

Java Program Linear Search

Binary Search Java Program

Ternary Search Java Program

Java Program to Generate Range of Numbers

Java Program to Generate Range of Characters

Java Program to Compute Square Root Value

Java Program to Check Number is Positive or Negative

Java Program to Check Number is Odd or Even

Java Program to Compute Plot Area

Java Program to Convert Number of Days into Years

Java Program to Check a Year is Leap Year, Century Year or Not

Java Program to Check a Character is Digit, Letter or neither digit nor letter

Java Program to Check a Number is Palindrome or not

Java Program to Sum Two Matrix

Java Program to Compute Power

Java Program to Check Number is an Armstrong Number or not

Java Program for Temperature Unit Conversions

Java Program to Generate Random Numbers in Specified Range

Java Program to Compute Sum of Digits and Product of Digits

Java Program to Compute Reverse Number

Java Programming Computes Factorial Value

Java Programming Checks Prime Number or not

Java Program to Compute Harmonic Series

Java Program Generate Floyd's Triangle

Java Program to Reverse String

Java Program to Check Palindrome String or not

Java Program to Open Notepad

Java Program to Search String using RegEx Pattern

Java Program to Search Word in String

Java Program to Get System Environment Variables

Java Program to Get IP Address of Server Name

Java Program for Arrays Sorting and Reverse using sort method

Java Program for Bubble Sorting

Java Program for Selection Sorting

Java Program for Insertion Sorting

Java Program for Merge Sorting

Java Program for Quick Sorting

Java Program for Counting Sort

Java Program for Radix Sorting

Java Program for Sorting Array of Strings

Java Program for String Characters Sorting

Java Program to Sum First N Numbers

Java Program to Product First N Numbers

Java Program to get URL details

Java Program to get URL HTML Content

Java Program to get URL details

Java Program to get URL HTML Content

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter