When a class inherits an abstract class and it does not implement all the abstract methods?

  • In Interface, a class can implement multiple interfaces, whereas the class can inherit only one Abstract Class.
  • In Interface does not have access modifiers. Everything defined inside the Interface is assumed to have a public modifier, whereas Abstract Class can have an access modifier.
  • The Interface cannot contain data fields, whereas the abstract class can have data fields.
  • Interfaces help define a class’s peripheral abilities, whereas an abstract class defines the identity of a class.

When a class inherits an abstract class and it does not implement all the abstract methods?

A class which has the abstract keyword in its declaration is called abstract class. Abstract classes should have zero or more abstract methods. i.e., methods without a body. It can have multiple concrete methods.

Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method.

Abstract classes cannot be instantiated.

Important Reasons For Using Interfaces

  • Interfaces are used to achieve abstraction.
  • Designed to support dynamic method resolution at run time
  • It helps you to achieve loose coupling.
  • Allows you to separate the definition of a method from the inheritance hierarchy

Important Reasons For Using Abstract Class

  • Abstract classes offer default functionality for the subclasses.
  • Provides a template for future specific classes
  • Helps you to define a common interface for its subclasses
  • Abstract class allows code reusability.

The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods (methods that have code). All the methods of an interface are abstract methods.

An interface cannot be instantiated. However, classes that implement interfaces can be instantiated. Interfaces never contain instance variables but, they can contain public static final variables (i.e., constant class variables)

Difference between Interface and Abstract Class in Java

An abstract class permits you to make functionality that subclasses can implement or override whereas an interface only permits you to state functionality but not to implement it. A class can extend only one abstract class while a class can implement multiple interfaces.

Parameters Interface Abstract class
Speed Slow Fast
Multiple Inheritances Implement several Interfaces Only one abstract class
Structure Abstract methods Abstract & concrete methods
When to use Future enhancement To avoid independence
Inheritance/ Implementation A Class can implement multiple interfaces The class can inherit only one Abstract Class
Default Implementation While adding new stuff to the interface, it is a nightmare to find all the implementors and implement newly defined stuff. In case of Abstract Class, you can take advantage of the default implementation.
Access Modifiers The interface does not have access modifiers. Everything defined inside the interface is assumed public modifier. Abstract Class can have an access modifier.
When to use It is better to use interface when various implementations share only method signature. Polymorphic hierarchy of value types. It should be used when various implementations of the same kind share a common behavior.
Data fields the interface cannot contain data fields. the class can have data fields.
Multiple Inheritance Default A class may implement numerous interfaces. A class inherits only one abstract class.
Implementation An interface is abstract so that it can’t provide any code. An abstract class can give complete, default code which should be overridden.
Use of Access modifiers You cannot use access modifiers for the method, properties, etc. You can use an abstract class which contains access modifiers.
Usage Interfaces help to define the peripheral abilities of a class. An abstract class defines the identity of a class.
Defined fields No fields can be defined An abstract class allows you to define both fields and constants
Inheritance An interface can inherit multiple interfaces but cannot inherit a class. An abstract class can inherit a class and multiple interfaces.
Constructor or destructors An interface cannot declare constructors or destructors. An abstract class can declare constructors and destructors.
Limit of Extensions It can extend any number of interfaces. It can extend only one class or one abstract class at a time.
Abstract keyword In an abstract interface keyword, is optional for declaring a method as an abstract. In an abstract class, the abstract keyword is compulsory for declaring a method as an abstract.
Class type An interface can have only public abstract methods. An abstract class has protected and public abstract methods.

Sample code for Interface and Abstract Class in Java

Following is sample code to create an interface and abstract class in Java

Interface Syntax

interface name{ //methods }

Java Interface Example:

interface Pet { public void test(); } class Dog implements Pet { public void test() { System.out.println("Interface Method Implemented"); } public static void main(String args[]) { Pet p = new Dog(); p.test(); } }

Abstract Class Syntax

abstract class name{ // code }

Abstract class example:

abstract class Shape { int b = 20; abstract public void calculateArea(); } public class Rectangle extends Shape { public static void main(String args[]) { Rectangle obj = new Rectangle(); obj.b = 200; obj.calculateArea(); } public void calculateArea() { System.out.println("Area is " + (b * b)); } }

Abstract class in Java is similar to interface except that it can contain default method implementation. An abstract class can have an abstract method without body and it can have methods with implementation also. abstract keyword is used to create a abstract class and method. Abstract class in java can’t be instantiated. An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class.

When a class inherits an abstract class and it does not implement all the abstract methods?
Here is a simple example of an Abstract Class in Java.

package com.journaldev.design; //abstract class public abstract class Person { private String name; private String gender; public Person(String nm, String gen){ this.name=nm; this.gender=gen; } //abstract method public abstract void work(); @Override public String toString(){ return "Name="+this.name+"::Gender="+this.gender; } public void changeName(String newName) { this.name = newName; } }

Notice that work() is an abstract method and it has no-body. Here is a concrete class example extending an abstract class in java.

package com.journaldev.design; public class Employee extends Person { private int empId; public Employee(String nm, String gen, int id) { super(nm, gen); this.empId=id; } @Override public void work() { if(empId == 0){ System.out.println("Not working"); }else{ System.out.println("Working as employee!!"); } } public static void main(String args[]){ //coding in terms of abstract classes Person student = new Employee("Dove","Female",0); Person employee = new Employee("Pankaj","Male",123); student.work(); employee.work(); //using method implemented in abstract class - inheritance employee.changeName("Pankaj Kumar"); System.out.println(employee.toString()); } }

Note that subclass Employee inherits the properties and methods of superclass Person using inheritance in java. Also notice the use of Override annotation in Employee class. Read more for why we should always use Override annotation when overriding a method.

Abstract class in Java Important Points

  1. abstract keyword is used to create an abstract class in java.
  2. Abstract class in java can’t be instantiated.
  3. We can use abstract keyword to create an abstract method, an abstract method doesn’t have body.
  4. If a class have abstract methods, then the class should also be abstract using abstract keyword, else it will not compile.
  5. It’s not necessary for an abstract class to have abstract method. We can mark a class as abstract even if it doesn’t declare any abstract methods.
  6. If abstract class doesn’t have any method implementation, its better to use interface because java doesn’t support multiple class inheritance.
  7. The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.
  8. All the methods in an interface are implicitly abstract unless the interface methods are static or default. Static methods and default methods in interfaces are added in Java 8, for more details read Java 8 interface changes.
  9. Java Abstract class can implement interfaces without even providing the implementation of interface methods.
  10. Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.
  11. We can run abstract class in java like any other class if it has main() method.

That’s all for an abstract class in Java. If I missed anything important, please let us know through comments.