1.

Abstract class and Interfaces

Answer»

Abstract Class:-

In C++, a class becomes abstract if it has at least one pure virtual function. In Java, unlike C++, an abstract class is created using a separate keyword abstract. The following are some key points to remember about Java abstract classes.


  • An abstract class instance cannot be created.

  • The use of constructors is permitted.

  • There is no need for an abstract method in an abstract class.

  • Final methods aren't allowed in abstract classes since they can't be overridden, but abstract methods are designed to be overridden.

  • We are prohibited from creating objects for any abstract class.

  • In an abstract class, we can define static methods.

Example -

abstract class Sample1 {
abstract void fun();
}

class Sample2 extends Sample2 {
void fun()
{
System.out.println("Inside Sample2's fun function.");
}
}

class Test {

public static void main(String args[])
{

Base b = new Derived(); // We can have references of Base type.
b.fun();
}
}

Output -

Inside Sample2's fun function.

Explanation - In the above code, we have defined an abstract class Sample1. We declare the function definition in this class. We define another class Sample2 and extend it from Sample1. Here, we provide the implementation for the method of the abstract class inherited. Now we create an instance of the Sample2 class and invoke the fun() function.

Java Interfaces:-


  • An interface, like a class, can include methods and variables, but the methods declared in an interface are abstract by default (only method signature, no body).

  • Interfaces define what a class must do, not how it must do it.

  • An interface is about capabilities; for example, a Player may be an interface, and any class that implements it must be able to (or must implement) movement (). As a result, it provides a set of methods that must be implemented by the class.

  • If a class implements an interface but does not offer method bodies for all of the interface's functionalities, the class must be abstracted.

Syntax -

interface INTERFACE_NAME {
// constant fields
// methods 
}

The interface keyword is used to declare an interface. Its purpose is to provide complete abstraction. All methods in an interface are declared with an empty body and are public by default, and all fields are public, static, and final. A class that implements an interface is required to implement all of the interface's functions. The keyword implements is used to implement an interface.

Reasons for introducing interfaces in Java:


  • It's a technique for achieving complete abstraction.

  • Because java does not provide multiple inheritances in the case of classes, multiple inheritances can be achieved by using interfaces.

  • It can also be used for loose coupling.

  • Abstraction is implemented through interfaces. So one question that comes to mind is why we should utilise interfaces when we have abstract classes. The reason for this is because abstract classes can have non-final variables, whereas interface variables are final, public, and static.

Example -

interface Sample
{

final int a = 1; // public, static and final
void display(); // public and abstract
}

// A class that implements the interface.
class Test implements Sample
{
// Implementing the capabilities of the interface.
public void display()
{
System.out.println("InterviewBit");
}

public static void main (String[] args)
{
Test t = new Test();
t.display();
System.out.println(a);
}
}

Output -

InterviewBit
1

Explanation - In the above code, we created an interface Sample having a public, static and final data member ‘a’ and a function signature with name ‘display’. We create a class Test that implements this interface and provides the definition for the display function. We also access the data member ‘a’ defined in the interface.

Abstract Class vs Interface:

The following table depicts the differences between abstract classes and interfaces in Java:

Abstract ClassInterface
Abstract and non-abstract methods can both be found in an abstract class. It can also contain default and static methods starting with Java 8.An interface can only contain abstract methods.
Non-final variables can be present in abstract classes.Variables declared in a Java interface are by default final variables.
Variables in an abstract class can be final, non-final, static, or non-static.Only static and final variables are used in the interface.
The interface can be implemented using an abstract class.An abstract class cannot be implemented using an interface.
The keyword "extends" can be used to extend an abstract class.The keyword "implements" can be used to implement a Java interface.



Discussion

No Comment Found