---
title: "When we use Interface and abstract class in Java?"  
description: "When we use Interface and abstract class in Java?"  
author: "Hemant Patel"  
published: 2017-03-02  
updated: 2020-09-23  
canonical: https://www.mindstick.com/interview/23213/when-we-use-interface-and-abstract-class-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# When we use Interface and abstract class in Java?

First we come to interface and abstract class, both are just only a thought that’s can use for achieving multiple inheritance. When we know about some property of defined method, then we create an abstract class. Suppose that we create a class Bus and Car, then both have the property of clutch, brake and gear. Then if we know some property of brake, so we create abstract class and defined a function brake and give some functionality of brake, means in abstract class we declare and define the method. And this abstract class implemented by Bus and Car Classes. And if we want to change the algorithm of the parent class (abstract class) method, then we change in our child class.

\

```
abstract class Bike    {          Bike()        {         System.out.println("bike is created");        }          void changeGear()        {         System.out.println("gear changed");         }      }     class Honda extends Bike    {      void changeGear()        {         System.out.println("Gear change..");        }      }   class Hello{   public static void main(String args[]) {    Bike obj = new
  Honda();    obj.changeGear();   }  }
```

But if we don't know about the functionality of method, then we create interfaces and just define a method and extend in child class. Suppose if we not know about Clutch and Gear functionality, then we define a method in an interface and extend in class Bus and Car. And change the algorithm of the parent class method (interface) in child class.

\

```
interface Bike{   public void clutch();   public void gear();}class Bus implements Bike{  public void clutch ()  {   System.out.println("implementation of clutch ");  }  public void gear ()  {   System.out.println("implementation of gear ");  }  public static void main(String args[])  {      Bike obj = new Bus ();       obj. clutch ();  }}
```

## Answers

### Answer by Hemant Patel

First we come to interface and abstract class, both are just only a thought that’s can use for achieving multiple inheritance. When we know about some property of defined method, then we create an abstract class. Suppose that we create a class Bus and Car, then both have the property of clutch, brake and gear. Then if we know some property of brake, so we create abstract class and defined a function brake and give some functionality of brake, means in abstract class we declare and define the method. And this abstract class implemented by Bus and Car Classes. And if we want to change the algorithm of the parent class (abstract class) method, then we change in our child class.

\

```
abstract class Bike    {          Bike()        {         System.out.println("bike is created");        }          void changeGear()        {         System.out.println("gear changed");         }      }     class Honda extends Bike    {      void changeGear()        {         System.out.println("Gear change..");        }      }   class Hello{   public static void main(String args[]) {    Bike obj = new
  Honda();    obj.changeGear();   }  }
```

But if we don't know about the functionality of method, then we create interfaces and just define a method and extend in child class. Suppose if we not know about Clutch and Gear functionality, then we define a method in an interface and extend in class Bus and Car. And change the algorithm of the parent class method (interface) in child class.

\

```
interface Bike{   public void clutch();   public void gear();}class Bus implements Bike{  public void clutch ()  {   System.out.println("implementation of clutch ");  }  public void gear ()  {   System.out.println("implementation of gear ");  }  public static void main(String args[])  {      Bike obj = new Bus ();       obj. clutch ();  }}
```


---

Original Source: https://www.mindstick.com/interview/23213/when-we-use-interface-and-abstract-class-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
