---
title: "Introduction to Abstraction in java"  
description: "Abstraction means show functionality and hide complexity. In object oriented programming functionality should be shown to user but complexity (code of"  
author: "Hemant Patel"  
published: 2017-01-19  
updated: 2018-03-17  
canonical: https://www.mindstick.com/blog/11269/introduction-to-abstraction-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Introduction to Abstraction in java

**Abstraction:-** \

Abstraction means show [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) and hide complexity. In object [oriented programming](https://www.mindstick.com/forum/162/object-oriented-programming-oop-s) functionality should be shown to user but complexity (code of the function) should be hidden for the user. It can be achieved by following two ways:-\

1.[Abstract class](https://www.mindstick.com/articles/1430/abstract-class)

2.Interface\

## Abstract class:-

Abstract class can not be instantiated. It means object of an Abstract class can not be created. Abstract modifier should be use to define an abstract class.

**e.g.:-**

```
   abstract class Shape         {            }           class Text                       {                         main method                             {                                 new Shape(); //Error because abstract class never create object                               }                        }
```

## \

**[Abstract method](https://www.mindstick.com/articles/23305/abstract-methods-and-class-in-c-sharp):-** A method which does mot have body is known as abstract method. Abstract method should be defined by using abstract modifiers.\

**Rule:**- Abstract method can only be defined in abstract class.

It means if a class has any abstract method then that class must be an abstract class.

## e.g:-

```
abstract class Shape         {         abstract void findArea();         }
```

**Rule:-** **1.** If parent of a class is abstract then all the abstract method of parent class must be overridden to this class(child class) otherwise you will have to make this class an abstract class.

**2.** Abstract class should be define to provide prototype of a method to all its type so that methods prototype in each child class should be same.

**e.g.:-**

```
    abstract class Calculator       {          int num1,num2,res;          void accept Number(int n1,int n2)          {          Num1=n1;          Num2=n2;          }          abstract void cal();          void showResult()          {          System.out.println(res);          }      }
```

**Interface:-** Interface also structure of class is should be define by using interface keyword.

**e.g:-**

```
 interface Automobile {         }And //       Class Test      {          main method          {           new Automobile();          }      }And//interface Automobile{methods();}
```

Method of a interface by default public and can only be public.

**[Implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) of Interface:-**

An Interface contains prototype of methods and thes methods will be define by classes.This is called Implementation of interface the class that implement in that Interface is called Implementation of class.

**e.g:-**

```
class Bus Implements Autimobile{  public void method of parent class()  {  System.out.println(“method of parent class executed”);  }}
```

All the method of interface must be overridden (Implemented) by child class.

All the overriding rules must be follow here

*Method of Interface can not be static.

**e.g:-**

**Type of a reference variable can be:-**

**1.** The class which is instantiated

**2.** Parent class

**3.** Interface that implement

```
 class TestBus          {          Main
method          {          Automobile
auto=new Bus();          Auto.start();          Auto.stop();}          }
```

***e.g:-***

```
 class A             {             }             inteface B              {              }            class C extend A implements B               {               main method                 {                 C obj1=new C();                 A obj2=new C();                 B obj3=new C();                 }              }
```

**You can also visit these related helpful link**\

**[What is the difference between abstraction and encapsulation](https://www.mindstick.com/Interview/2393/what-is-the-difference-between-abstraction-and-encapsulation)**

[**Can you use abstract and final both with a method in java**](https://www.mindstick.com/Interview/2396/can-you-use-abstract-and-final-both-with-a-method-in-java)

**[Difference between Abstraction and Encapsulation in Java](https://www.mindstick.com/Articles/1712/difference-between-abstraction-and-encapsulation-in-java)**

[**Abstraction in java**](https://www.mindstick.com/Blog/10994/abstraction-in-java)

---

Original Source: https://www.mindstick.com/blog/11269/introduction-to-abstraction-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
