---
title: "Abstract Class in Java"  
description: "If you are declared with abstract keyword of a class is called abstract class. It can have abstract and non-abstract method (method with body)."  
author: "Anupam Mishra"  
published: 2015-12-11  
updated: 2017-12-14  
canonical: https://www.mindstick.com/articles/11946/abstract-class-in-java  
category: "java"  
tags: ["java", "j2ee"]  
reading_time: 2 minutes  

---

# Abstract Class in Java

If you are declared with abstract [keyword](https://yourviews.mindstick.com/view/87345/explained-the-importance-of-keyword-traffic) of a class is called [abstract class](https://www.mindstick.com/articles/1430/abstract-class). It can

have abstract and non-[abstract method](https://www.mindstick.com/articles/23305/abstract-methods-and-class-in-c-sharp) (method with body).

It need to be extended and its method implemented to another class.it can’t be instantiated.

abstract class Abc{}

##### abstract method :

A method i.e. declared abstract & does not have [implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) is [known as](https://answers.mindstick.com/qa/38482/name-the-world-s-lightest-material-which-was-manufactured-by-isro-scientists-at-the-vikram-sarabhai-space-centre-thiruvananthapuram-and-is-also-known-as-blue-air) abstract method within a Abstract class.

abstract void getMilkPrice ();

\

**Note:**

1. If any class are declared any abstract method then that class must be abstract

2. If you are extending any abstract class that have abstract method then you must either provide the implementation of the method or make this class abstract.

##### *Real Scenario of abstract class:*

For take example, Milk is the abstract class,its implementation is provided by the Amul,Prag and NamsteIndia classes. Mostly, we don’t know about the implementation class (i.e. [hidden](https://yourviews.mindstick.com/view/87368/the-mitrokhin-files-the-kgb-s-hidden-hand-in-world-affairs-part-1-infiltration) to the end user) and object of the implementation class is provided by the [factory method](https://www.mindstick.com/forum/34032/factory-method-design-pattern-using-c-sharp).A factory method is the method that returns the [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) of the class

In this example, if you create the instance of Amul class, getMilkPrice() method of Amul class will be invoked.Similarily,other two classes does.

> ```
> abstract class Milk{    abstract int getMilkPrice();    }        class Amul extends Milk{    int getMilkPrice(){return 20;}    }    class Prag extends Milk{    int getMilkPrice(){return 19;}    }    class NamsteIndia extends Milk{    int getMilkPrice(){return 18;}    }        class MilkPriceTest{    public static void main(String args[]){    Amul b=new Amul();//if object is Amulint Price=b. getMilkPrice();    System.out.println("Price of Amul Milk is : "+Price+" Rs"); Prag b1=new Prag();//if object is Prag Price=b1. getMilkPrice();    System.out.println("Price of Prag Milk is : "+ Price
> +" Rs"); NamsteIndia b2=new NamsteIndia();//if object is
> Namste Indiaint Price=b1. getMilkPrice();    System.out.println("Price of NamsteIndia Milk is : "+Price+" Rs");    }}
> ```

---

Original Source: https://www.mindstick.com/articles/11946/abstract-class-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
