Introduction to OOPs Concept in Java with Example

Mahesh Sharma
4 min readDec 18, 2018

In this article, we will learn about Java OOPs concept. An Object-Oriented Programming system is an approach that contains many ideas such as Object and class, Inheritance, Abstraction, Polymorphism, Encapsulation etc.

Most popular languages like C, C++, Java follow an object-oriented programming language. Smalltalk is the first language which is a pure object-oriented programming language.

The primary purpose of the OOPs concept is to perform real-world things.

Concepts of OOPs(Object-Oriented Programming System):

Java OOPs Concept

1. Object and class

2. Abstraction

3. Inheritance

4. Polymorphism

5. Encapsulation

1. Object and Class:

Object: An object is a real-world entity like a pen, table, chair, car, etc. Objects are the mixture of attributes and methods. Attributes define the behavior of the object. The object is run time entity. To understand this better, let’s take an example of Dog.

A dog has many states such as Color, name, breed as well as behaviors — Wagging the Tell, Barking, Eating, etc.

Way to Create an Object:-‘new’ keyword is used to create new objects.

Example :-

public class Test

{

public test(String name)//This constructor has one parameter.

{

System.out.println(“Hello :” +name);

}

public static void main(string args[])

{

// following statement would create an object mytest.

Test mytest = new Test(“Anjali”);

}

}

OUTPUT:-

Hello: Anjali

Class: A class can be defined as blueprint/template that describes the behavior/states that the object of its type support. The class is a collection of similar type of object. Once you create a class, so you can define a No. of objects.

A class can contain the following variable types.

a. Local Variable:- Local Variables are defined in methods, constructors or blocks. within the method, the variable will be declared and initialized, and the variable will be destroyed when the method has completed.

b. Instance variables:- Instance variables are declared within a class and initialized when the class is instantiated.

c. Class variables:- Class variables are declared within a class and outside any method with the static keyword.

2. Inheritance:

When properties of one class inherited by other class, this concept is known as Inheritance. The concept of inheritance provides code reusability feature. When a class inherits the properties is known as a child class whereas a class whose properties are inherited is known as Parent class.

Here is 4 type of Inheritance in Java. These are

a). Single Inheritance: In this, one class properties inherited by other.

Syntax:

Class A

{

}

Class B extends Class A{

}

b). MultilevelInheritance: When a class is extending from a class which also extends from another class, or a class having more than one parent class but at different stages, this type of inheritance is called Multilevel Inheritance.

Syntax:

Class A

{

}

Class B extends Class A{

}

Class C extends Class B{

}

c). HierarchyInheritance: When a parent class having one or more child class, this is known as Hierarchy inheritance.

Syntax:

Class A

{

}

Class B extends Class A{

}

Class C extends Class A{

}

d). HybridInheritance: This type of inheritance is only achieved through Interfaces. Because this is the combination of multiple and multilevel inheritances and multiple is not supported in java Because of Ambiguity.

3. Encapsulation:

The Process of wrapping data members and abstraction together in a unit, it is known as Encapsulation. In Java Encapsulation, every class is an example of Encapsulation. In other words, it’s just like capsule medicine.

Encapsulation = Data Hiding + Abstraction

It simply means that any component is following abstraction and data hiding then it is also following Encapsulation.

How to achieve Encapsulation?

1. To achieve encapsulation, provide getter( ) and setter( ) method in the class to set the variables.

2. Declaring private to the instance variables of the class.

4. Abstraction:

Abstraction is the mechanism to hiding the unnecessary things from the users and provide only functionality.

There is a two way to achieve Abstraction:

1. By Abstract Class — Through “abstract” keyword, we can declare a class is abstract. The abstract class cannot be instantiated.

2. By Interface — In an interface, each method is public and abstract, but it does not contain any constructor.

ABSTRACT METHODS:-

if you want the actual implementation of that method which is determined by the child class, you must declare a class is abstract in a parent class.

5. Polymorphism:

Polymorphism is the concept which has the ability of an object to take on many forms. polymorphism a concept by which we can perform a single action by with different behaviors.

Here are two types of Polymorphism in Java :

1. Compile time polymorphism: Method Overloading is the way java supports Compile-time Polymorphism.

2. Runtime polymorphism: Method Overriding is the way java supports Runtime Polymorphism.

we can achieve polymorphism in java by method overloading and method overriding.

--

--

Mahesh Sharma

Hey, I'm Mahesh Sharma, a passionate digital marketer with 10+ years of experience in the field. I'll be sharing topics such as SEO, SMO, PPC/ SEM.