Outlining The Difference Between Abstract Class And Interface In Java

Outlining The Difference Between Abstract Class And Interface In Java

Introduction

The definition of data abstraction can simply be related to declaring only the information that is essential for methods (blocks of code) to function. This can also be defined as a system of hiding certain attributes and using syntax to contain or relay the essential functions.

Abstractions can be effectively used by employing both abstract classes and interfaces. This allows users to build functions without extensively declaring bodies during the implementation of methods.

Despite their name, abstract classes generally only achieve partial abstraction (but can be 100% in situations) while interfaces achieve complete abstraction all the time. The major difference between abstract class and interface in Java is that interfaces only support abstract methods while abstract classes support both abstract and non-abstract methods.

Both use their respective keywords unlike virtual keywords in polymorphism. Though abstract classes and interfaces are generally not used together due to both projecting abstraction methods, they can however be integrated together to pass separate functions in the same program. 

Here is an example of an interface being used alongside abstract classes to create a function:

interface Example{  
void x();
void y();  
void z();  
void o();  
}  
  
//Creating abstract class that provides the implementation of one method of A interface  
abstract class Sample implements Example{  
public void z(){System.out.println("Java is fun.");}  
}  
  
class Demo extends Sample{  
public void x(){System.out.println("X");}  
public void y(){System.out.println("Y");}  
public void o(){System.out.println("Z");}  
}  
  
class Test1{  
public static void main(String args[]){  
Example x=new Demo();  
x.x();  
x.y();  
x.z();  
x.o();  
}}  

Output:

Now, let’s learn more about what interfaces and abstract classes are, separately, how they can be used individually and what is the difference between abstract class and interface in Java.

Interface In Java

Interfaces can be defined as blueprints of classes. Interfaces have abstract methods and static constants that work together to achieve true abstraction. Interfaces can only support abstract methods and variables in Java and not the method bodies. Interfaces are especially helpful when working with multiple interfaces.

Interface in Java can also help when trying to represent IS-A relationships that are based on inheritance. The inheritance here can also be divided into two subtypes, interface inheritances and class inheritances. On more modern compilers, we can use both static, private, and default methods when applying interfaces. Interfaces can also be used for achieving loose coupling.

Declaring interfaces is easy and can be done by simply using the ‘interface’ keyword. This allows the declaring of every method in the interface with empty bodies, thus providing complete abstraction. Classes that implement interfaces must be created so that every method declared is implemented.

Here is the syntax for implementing interfaces:

interface <example>{    
    // declaration of constant fields  
    // declaration of methods that abstract  
}  

Here is an example of using an interface in Java:

interface Assets{  
float approximatedepreciation();  
}  
class Car implements Assets{  
public float approximatedepreciation(){return 8.75f;}  
}  
class Machine implements Assets{  
public float approximatedepreciation(){return 12.6f;}  
}  
class TestInterface2{  
public static void main(String[] args){  
Assets b=new Car();  
System.out.println("Depreciation: "+b.approximatedepreciation());  
}}  

Output:

Here is another example of user interfaces in Java that will help you understand the concept of interfaces better:

interface Area {
	// abstract method
	void draw();
	double area();
}

class Rectangle implements Area {
	int length, width;

	// constructor
	Rectangle(int length, int width)
	{
		this.length = length;
		this.width = width;
	}

	@Override public void draw()
	{
		System.out.println("Rectangle has been drawn ");
	}

	@Override public double area()
	{
		return (double)(length * width);
	}
}

class Circle implements Area {

	double pi = 3.14;
	int radius;

	// constructor
	Circle(int radius) { this.radius = radius; }

	@Override public void draw()
	{
		System.out.println(" Created circle. ");
	}

	@Override public double area()
	{

		return (double)((pi * radius * radius) / 2);
	}
}

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

		Area rect = new Rectangle(4, 6);
		System.out.println("Rectangle Area: "
						+ rect.area());

		// creating the Objects of circle class
		Area circle = new Circle(2);
		System.out.println("Circle Area: "
						+ circle.area());
	}
}

Output:

Abstract Classes In Java

Abstract classes are classes that are declared in an abstract manner or that promote abstraction. However, unlike interfaces, abstract classes contain both non-abstract methods and abstract methods.

They need their methods to be implemented and the classes to be extended. These classes cannot be instantiated and must be declared with the ‘abstract’ keyword.

For example:

abstract class Example{}

Abstract classes are restricted classes that cannot be utilized for creating objects and these classes must be inherited from other classes. So, the ‘abstract’ keyword is used for both methods and classes, functioning as non-access modifiers. Abstract classes can have both static methods and constructors as well.

Abstract classes can work with final methods that enforce subclasses into retaining the original body of the method. So, these classes do not have bodies and can inherit the bodies of the sub-classes through regular and abstract methods.

Here is a simple example of how abstract classes can be used:

abstract class Area{  
abstract void draw();  
}  
class Rectangle extends Area{  
void draw(){System.out.println("Rectangle Area");}  
}  
class Circle1 extends Area{  
void draw(){System.out.println("Circle Area");}  
}  
class TestAbstraction1{  
public static void main(String args[]){  
Area s=new Circle1();
s.draw();  
}  
}  

Output:

For accessing abstract classes, the bodies must be inherited from other classes. Let’s use the Bat class as an abstract class to better understand how these classes can be effectively utilised:

// Abstract class
abstract class Bat {
  // Abstract method (does not have a body)
  public abstract void BatSound();
  // Regular method
  public void sleep() {
    System.out.println("Kruuu");
  }
}

// Subclass (inherit from Bat)
class Woobat extends Bat {
  public void BatSound() {
    // The body of BatSound() is provided here
    System.out.println("This bat makes this sound:");
  }
}

class Main {
  public static void main(String[] args) {
    Woobat myWoobat = new Woobat(); // Create a Woobat object
    myWoobat.BatSound();
    myWoobat.sleep();
  }
}

Output:

Differences Between Abstract Classes And Interfaces In Java

Though both abstract classes and interfaces allow developers to use abstraction when developing functions or working with regular methods, there are many differences between the two. Let us check the fundamental differences between these two approaches when programming in Java.

  • Abstract classes support both non-abstract and abstract methods, while interfaces only support abstract methods.
  • Abstract classes do not support instances of multiple inheritances while interfaces are used extensively for working with multiple inheritances.
  • Interfaces only work with final and static variables; meanwhile, abstract classes can function with static, non-static, final, and non-final variables.
  • Interfaces are not capable of providing implementations of abstract classes while abstract classes have the ability to provide interface implementations.
  • Abstract classes extend other classes and implement multiple interfaces while interfaces themselves can only extend another interface.
  • Abstract classes can be extended by the ‘extends’ keyword while interfaces can be extended using the ‘implements’ keyword.
  • Members of interfaces in Java are public while abstract classes can possess protected, private and other class members in Java.

Frequently Asked Questions

When should we use the abstract class and interface in Java?

Abstract classes should be used when working with non-abstract methods and interfaces only to support abstract methods. Interfaces should be used when working with instances of multiple inheritances.

Why do we use the interface and abstract class?

We use interfaces and abstract classes to effectively use abstraction methods when working with programming languages such as Java.

What is the use of the interface?

Interfaces allow complete abstraction and allow users to build functions without overly specifying the bodies of the methods being used. Interfaces are also very useful for loose coupling.

What are the advantages of the interface?

Interfaces work well with multiple inheritances and empower total abstraction easily.

Can abstract classes have a constructor?

Yes, abstract classes can have both constructors and static methods.

What is the difference between class and interface?

The difference between the two can be easily reduced to classes being the target of the method or where the function will pass through. Meanwhile, the interface contains the abstract methods of effectively using these classes for passing functions while promoting abstraction.

How to declare an interface and an abstract class?

Interfaces and abstract classes can be declared using ‘interface’ and ‘abstract class’ keywords respectively.

Can interfaces and abstract classes be used together?

Yes, these two can easily work together when integrated into the code or functions while building Java programs.

Are members of an interface private?

No, members of an interface are all public by default; however, one can use private members in abstract classes.

Key Takeaways

Abstract classes are also not known for multiple inheritances while one of the primary utilities of interfaces is providing the ability to work with multiple inheritances. Both abstract and non-abstract methods are highly important for providing solutions to individual requirements. Interfaces and abstract classes work well together when used properly.

Exit mobile version