Understanding OOP Concepts In Java With Program Examples

Java is a unique programming language, and by far, one of the populous ones. It is a class-based object-oriented programming language which is built around the concept of objects. The fundamental objective of OOP concepts in Java is to enhance code readability and reusability by defining how to structure a Java program efficiently. This programming paradigm is characterized as an object so it’s also known as the true object-oriented programming language.

The OOP concepts in Java Programming Language is based on Classes, Objects, Inheritance, Polymorphism, Encapsulation, and Abstraction. Let’s help you understand each of them in detail.

OOP Concepts In Java with Examples

1. Class: 

Class is one of the fundamental OOPS concepts in Java. This is a blueprint of the object which defines functions, attributes, and variables where the real functionality lies within. These attributes, variables, and methods are called members and these members can be accessed based on the access modifiers during the declaration of members.

Example of class OOPS concepts in Java:

Let’s say that you create a class called Animal. Then dog, cat, cow, goat would all be the objects.

The syntax below shows how to define a class in Java.


class className {

// variables

// methods


From our example of the student as a class, you should first use the datatype class and give the className as Student. It should look like below:

public class Student {

String name;
int rollno;

void exam() {

void playing() {


2. Object:

An Object is defined as an instance of a class. An object contains the address and takes up some space in memory. It represents the real-life entities which belong to a class. A Java program creates many objects, which are used by invoking methods (functions). An object has a specific state (properties of an object), behavior (functions), and identity.

Example of object OOPS concepts in Java:

The cow is an object and it would assume properties like color, name, breed, etc.as well as behaviors like wagging the tail, mooing, eating, etc. Similarly, other animals like dogs, cats, etc. can also be included as objects.


className object = new className();

To define the object, in the syntax above, you are making use of the constructor className(). For our example of defining the Cow, Dog, Cat as the objects, follow the steps below:

//Cow object

Animal Cow = new Animal();

//Dog object

Animal Dog = new Animal();

//Cat object

Animal Cat = new Animal();

As you see, with these steps the objects Cow, Dog and Cat have been created.

3. Method:

A method is a collection of statements that perform some specific task and return the result to the caller. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class. Methods are time savers and help us to reuse the code without retyping the code.

Example of method OOPS concepts in Java:

The process of a Cow, munching on grass is a method.


public class className {

static void myMethod() {

System.out.println(“Cow is munching on grass”);

As you see in the syntax, inside the class className, the method myMethod has been created. The usage of static is to show that it belongs only with the class and is not to be mistaken as part of the object of the class. From the example of a cow eating, or any other animal for that matter, the following is how the syntax would look like.

public class Animal{

static void Eating(){

System.out.println("Cow is eating");



4. Polymorphism:

Polymorphism is defined as the process of performing a single task in different possible ways. This is done with the help of the unique signature and declaration of these entities. In Java, Polymorphism can be achieved in two ways called method overloading and method overriding. Method overloading is also called Compile Time Polymorphism whereas Method Overriding is also called Run Time Polymorphism.

Method Overloading: It provides us the functionality to use more than one method with the same name but having different parameters in numbers, sequences, and data types.

Method Overriding: In this process, a child-class overrides a method within the parent class. The method which is overridden is resolved at runtime, that’s why it is called runtime polymorphism.

Example of polymorphism OOPS concepts in Java:

The process of eating among animals can be extended to a dog eating, a cow eating, and so on.


class A{}

class B extends A{}


//For our example of animals eating their food, take a look below:

class Animal{

void eat(){
System.out.println("Animal is Eating");


class Dog extends Animal{

void eat(){
System.out.println("Dog is Eating");


class BabyDog1 extends Dog{

public static void main(String args[]){

Animal a = new BabyDog1();




Dog is Eating

5. Inheritance:

This is the third step process in which the data will be inspected, cleaned, transformed, and visualized by reducing useless information and transforming into important parts of information which is used for obtaining some valuable information out of the existing data. The keyword used for implementing this OOP concept in java is ‘extends’.

Example of inheritance OOPS concepts in Java:

Puppies inherit the properties and functions of the mother dog.


class derived-class extends base-class


//methods and fields



class Book {
int price = 1000;

Class PhysicsBook extends Book{
int discount = 500;

public static void main(String args[]) {
PhysicsBook physics = new Science();

System.out.println("Book's market price is" +physics.price);
System.out.println("Discount for the book is" +physics.discount);



Book’s market price is 1000

Discount for the book is 500

6. Encapsulation: 

This is the process of encapsulating which means hiding or binding or wrapping the code into a single unit or module which is defined as Class in Java. The encapsulation feature of object-oriented programming can be achieved by using a class in Java.

A plain old java object or a Java Bean is said to be encapsulated as the members of the class are private (access modifier) those which can be accessed only by using getters and setters’ methods in the class.

Example of encapsulation OOPS concepts in Java:

public class RunEncapsulation {

public static void main(String args[]) {

EncapsulationTest encapsulation = new EncapsulationTest();




System.out.print("Name : " + encapsulation.getName() + " Age : " + encapsulation.getAge());




Name: Ella Age: 25

In this example, we are having a public class called RunEncapsulation within which we are encapsulating the IdNum and are making the name and age alone visible.

7. Abstraction: 

Abstraction can be defined as the process of hiding the implementation of the functionalities by exposing only the required interfaces or accessing methods to invoke the methods of the Implementation class.

The abstraction can be achieved in Java programming language by using Interface and Abstract Class.  We can achieve 100% abstraction using interfaces.

The keyword “abstract” is used for creating abstract classes and methods.

Abstract Class: An abstract class is declared by using the ‘abstract’ keyword before the ‘class’ keyword. It is a restricted class which can contain abstract methods. The class must be declared ‘abstract’ if it contains at least one ‘abstract method’.

Abstract Method: An abstract method does not have a body and it is declared inside an abstract class using the ‘abstract’ keyword.


abstract class A{

abstract void myMethod();

void anotherMethod(){

//Does something



public abstract class Book {

public abstract void BookPrice()   // It doesn't have a body, so it is an abstract method
public void price() {
System.out.println("The Price Of The Book Is : 500");   // It is a regular method.

// Subclass Inherited from its parent class 
class Physics extends Book {
public void BookpPice() {
System.out.println("The Price Of Physics Book Is : 600");

class PrimaryClass {
public static void main(String[] args) {
Physics myPhysics = new Physics();


The Price Of The Book Is : 500

The Price Of Physics Book Is : 600

In Closing

The implementation of OOPS concepts in Java makes it a whole lot easier for troubleshooting, re-usage of codes, flexibility, and effective problem-solving. To leverage, these benefits of OOPS concepts in Java make sure to set forth for hands-on tasking of the above mentioned OOPS concepts in Java with program examples.

See More: Top 20 Most Frequently Asked C++ Interview Questions And Answers

Leave a Comment