<p class="p1">In this tutorial, we will learn about Java abstract classes and methods and how to use them in our program with the help of examples.</p>
<h3 class="p1"><strong><span style="color: #000080;">Abstract Class</span></strong></h3>
<p class="p1">An <strong><span style="color: #008000;">abstract class</span></strong> is a class that cannot be instantiated (we cannot create objects of an abstract class).</p>
<h5 class="p1"><strong><span style="color: #0000ff;">Syntax</span></strong></h5>
<p>We use the <span style="color: #0000ff;"><strong>abstract</strong></span> keyword to declare an abstract class.</p>
<pre><span style="color: #0000ff;"><strong>abstract</strong></span> class class_name {
<strong><span style="color: #008000;">//fields and methods</span></strong>
 }</pre>
<p class="p1">If we try to create objects of an abstract class, we will get a compilation error. For example,</p>
<pre><span style="color: #008000;"><strong>//abstract class</strong></span>
abstract class Person {
<span style="color: #008000;"><strong> //class members</strong></span>
 private String name;

 public void setName(String name) {
 this.name = name;
 }
}

class Main {
 public static void main(String args[]) {
 <strong><span style="color: #008000;">//creating Person instance</span></strong>
 <span style="color: #008000;"><strong>//error: 'Person' is abstract; cannot be instantiated</strong></span>
 <strong><span style="color: #0000ff;">Person person = new Person();
</span></strong>
 }
}</pre>
<p>We can access the members of a class by creating its object or by creating an object of its subclass.</p>
<p class="p1">Abstract classes cannot be instantiated but we can create subclasses from it. We can create objects of subclasses to access members of the abstract class.</p>
<p class="p2">Abstract class<span class="Apple-converted-space"> </span>is used to define common characteristics of subclasses and the only purpose of the abstract class is to let other sub classes inherit from it.</p>
<p class="p1">Before we learn about it in detail, we need to understand <span style="color: #0000ff;"><strong>abstract methods</strong></span>.</p>
<h3 class="p1"><strong><span style="color: #000080;">Abstract Method</span></strong></h3>
<p>Abstract class can contain methods that contain no implementation (without any body) called <strong><span style="color: #0000ff;">abstract</span></strong> method.</p>
<h5 class="p1"><strong><span style="color: #0000ff;">Syntax</span></strong></h5>
<p>We use the same keyword <span style="color: #008000;"><strong>abstract</strong></span> to create abstract methods.</p>
<pre><strong><span style="color: #0000ff;">abstract</span></strong> return_type function_name(); <span style="color: #008000;"><strong>//No Implementation</strong></span></pre>
<p class="p1">An abstract class contains fields and methods similar to other classes and addition to this, it can also contain <span style="color: #008000;"><strong>abstract</strong></span> methods.</p>
<p>If a class has any <span style="color: #008000;"><strong>abstract</strong></span> method then the entire class must be declared as <span style="color: #008000;"><strong>abstract</strong></span>.</p>
<p><strong><span style="color: #0000ff;">When to use abstract methods?</span></strong></p>
<p class="p1">Abstract methods are usually declared where two or more subclasses are expected to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract methods.</p>
<p class="p1"><span style="color: #0000ff;"><b>Why can’t we create the object of an abstract class?</b></span></p>
<p class="p1">Abstract classes are incomplete, they have abstract methods that have no body so if java allows you to create object of this class then if someone calls the abstract method using that object then there would be no actual implementation of the method to invoke.</p>
<h3><span style="color: #000080;"><strong>Overriding abstract methods</strong></span></h3>
<p>An abstract class cannot be instantiated; therefore we must inherit it so that we can access the members of an abstract class.</p>
<p class="p1">When an abstract class is subclassed,</p>
<ul>
<li class="p1">To create an object of subclass, we must have to override (provide implementation to a method already defined in superclass) all the <span style="color: #008000;"><strong>abstract methods </strong><span style="color: #333300;">of super class</span></span> in our subclass.</li>
<li class="p1">However, if subclass does not implement abstract method, then the subclass must also be declared <span style="color: #008000;"><strong>abstract </strong><span style="color: #000000;">(which cannot be instantiated).</span></span></li>
</ul>
<h5 class="p1"><strong><span style="color: #0000ff;">Example</span></strong></h5>
<pre><span style="color: #0000ff;"><strong><span style="color: #008000;">//abstract class Person</span>
abstract</strong></span> class Person {
 String name;
<span style="color: #008000;"><strong> //abstract method</strong></span>
 <strong><span style="color: #0000ff;">abstract</span></strong> void show();
 <strong><span style="color: #008000;"> //non-abstract method</span></strong>
 public void setName(String name) {
 this.name = name;
 }
}
<span style="color: #008000;"><strong>//subclass Student inherit Person class</strong></span>
class <span style="color: #0000ff;"><strong>Student</strong></span> extends Person {

 private int rollNo;

 public void setRollNo(int rollNo) {
 this.rollNo = rollNo;
 }
<strong><span style="color: #008000;"> //Implementing the method of Person class in Student class</span></strong>
 public void show() {
 System.out.println("Student Detail");
 System.out.println(" Name: " + name + " Roll no: " + rollNo);
 }
}
<span style="color: #008000;"><strong>//subclass Teacher inherit Person class</strong></span>
class <strong><span style="color: #0000ff;">Teacher</span></strong> extends Person {

 private int salary;

 public void setSalary(int salary) {
 this.salary = salary;
 }
<strong><span style="color: #008000;"> //Implementing the method of Person class in Teacher class</span></strong>
 public void show() {
 System.out.println("Teacher Detail");
 System.out.println(" Name: " + name + " Salary: " + salary);
 }
}

class Main {
 public static void main(String[] args) {
 <span style="color: #008000;"><strong> //creating Student class instance</strong></span>
 Student s1 = new Student();
 s1.setName("Arun Verma");
 s1.setRollNo(101);
 s1.show();

 <strong><span style="color: #008000;">//creating Teacher class instance</span></strong>
 Teacher t1 = new Teacher();
 t1.setName("Ramesh Sahu");
 t1.setSalary(25000);
 t1.show();
 }
}</pre>
<h5 class="p1"><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>Student Detail
Name: Arun Verma Roll no: 101
Teacher Detail
Name: Ramesh Sahu Salary: 25000</pre>
<h5 class="p1"><strong><span style="color: #0000ff;">Example explained</span></strong></h5>
<ul>
<li class="p1">Here, we have created an abstract class <span style="color: #0000ff;"><strong>Person</strong></span>. This class contains an abstract method <span style="color: #008000;"><strong>show()</strong></span>, a non-abstract method <span style="color: #008000;"><strong>setName()</strong> </span>and a field <span style="color: #008000;"><strong>name</strong></span>.</li>
<li class="p1">We have inherited subclasses <span style="color: #0000ff;"><strong>Student</strong></span> and <span style="color: #0000ff;"><strong>Teacher </strong></span>from the super class <span style="color: #0000ff;"><strong>Person</strong></span>. The subclasses <strong><span style="color: #008000;">(Student </span></strong><span style="color: #008000;"><span style="color: #000000;">and </span></span><strong><span style="color: #008000;">Teacher) </span></strong>overrides the abstract method <span style="color: #008000;"><strong>show()</strong></span>.</li>
<li class="p1">Now creating an instance of subclasses we cannot only access its members but we can also access its superclass <span style="color: #008000;"><strong>(abstract class Person)</strong></span> members.</li>
</ul>
<h3><span style="color: #000080;"><b>Creating abstract class reference variable</b></span></h3>
<p class="p1">We cannot create instance of abstract class but we can create reference variable of abstract class.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<h5 class="p1"><strong><span style="color: #0000ff;">Example</span></strong></h5>
<pre><span style="color: #0000ff;"><strong>abstract</strong></span> class Base {

 public void funA() {
 System.out.println("Base funA() called");
 }
<span style="color: #008000;"><strong> //abstract method</strong></span>
 <strong><span style="color: #0000ff;">abstract</span></strong> void funC();

}
class Derived extends Base {

 public void funB() {
 System.out.println("Derived funB() called");
 }

<strong><span style="color: #008000;"> //Implementing funC() of Base class in Derived class</span></strong>
 void funC() {
 System.out.println("Derived funC() called");
 }
}

class Main {
 public static void main(String args[]) {

 <span style="color: #008000;"><strong>//error:'Base' is abstract; cannot be instantiated
 //Base b = new Base();</strong>

 <strong>// We can have references of Base type.</strong></span>
 <strong><span style="color: #0000ff;">Base</span></strong> b = new Derived();
 b.funA();
 b.funC();
 <strong><span style="color: #008000;">//error: using base class reference variable we cannot access derived class members
 //b.funB();</span></strong>
 }
}</pre>
<h5 class="p1"><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>Base funA() called
Derived funC() called</pre>
<p class="p1"><strong><span style="color: #0000ff;">Note:</span></strong> <strong>Using Base class reference variable we cannot access derived class members.</strong></p>
<h3></h3>
<h3><span style="color: #000080;"><strong>Abstract class constructor</strong></span></h3>
<p class="p1">An abstract class can contain constructors and it is called when an instance of inherited class is created.</p>
<h5 class="p1"><strong><span style="color: #0000ff;">Example</span></strong></h5>
<pre><span style="color: #008000;"><strong>// An abstract class with constructor</strong></span>
<span style="color: #0000ff;"><strong>abstract</strong></span> class Base {

<strong><span style="color: #008000;"> //Base class default constructor</span></strong>
 Base() {
 System.out.println("Base Constructor Called");
 }
<span style="color: #008000;"><strong> //abstract method</strong></span>
 <strong><span style="color: #0000ff;">abstract</span></strong> void fun();
}

class Derived extends Base {

<strong><span style="color: #008000;"> //Derived class default constructor</span></strong>
 Derived() {
 System.out.println("Derived Constructor Called");
 }

 void fun() {
 System.out.println("Derived fun() called");
 }
}

class Main {
 public static void main(String args[]) {
 <span style="color: #008000;"><strong>//creating Derived class instance</strong></span>
 Derived d = new Derived();
 }
}</pre>
<h5 class="p1"><strong><span style="color: #0000ff;">Output</span></strong></h5>
<pre>Base Constructor Called
Derived Constructor Called</pre>
<p>Visit <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://c1ctech.com/java-constructor-in-inheritance/">Java Constructor in Inheritance</a></strong></span> to learn more about Constructor behavior in Inheritance.</p>
<h4 class="p1"><span style="color: #000080;"><b>Points to Remember:</b></span></h4>
<ul class="ul1">
<li>To create abstract classes and methods we use the <span style="color: #008000;"><strong>abstract</strong></span> keyword.</li>
<li>An abstract method doesn&#8217;t have any implementation (method body).</li>
<li>A class containing abstract method must be declared as <span style="color: #008000;"><strong>abstract.</strong></span></li>
<li><span style="color: #008000;"><span style="color: #000000;">An abstract class can contain abstract and non-abstract methods.</span></span></li>
<li>Abstract classes cannot be instantiated but we can create subclasses from it.</li>
<li>Abstract class can have <strong><span class="s2" style="color: #0000ff;"><a style="color: #0000ff;" href="https://c1ctech.com/java-constructors/">constructors</a></span></strong> and final methods (which will force the subclass not to change the body of the method).</li>
<li>
<p class="p2">An abstract class may have <strong><span style="color: #008000;">static fields and static methods</span></strong>. You can use these static members with a class reference (for example, <strong>AbstractClass.staticMethod()</strong>)</p>
</li>
<li class="li2">The class inheriting the abstract class must either be declared <span style="color: #008000;"><strong>abstract</strong></span> or implement abstract method.</li>
<li>To create an object of a class that extends abstract class we must have to implement its abstract methods in our class.</li>
<li>If the class inheriting the abstract class is declared <span style="color: #008000;"><strong>abstract</strong></span>, it&#8217;s not mandatory to override abstract methods but now we cannot create an instance of subclass.</li>
</ul>


