Implement Interface using Abstract Class in Java
Last Updated :
06 Feb, 2023
Interface contains only abstract methods that can't be instantiated and it is declared by keyword interface. A class that is declared with the abstract keyword is known as an abstract class in Java. This is a class that usually contains at least one abstract method which can't be instantiated and It is also possible for the class to have no methods at all. The instance of an abstract class can't be created.
Now as all methods in an interface are abstract methods therefore we can implement it using Abstract Class.
1. Let's create an Interface at first:
Java
// creating an interface named GFG
interface GFG {
void learnCoding();
void learnProgrammingLanguage();
void contribute();
}
Here the three non-implemented methods are the abstract methods
2. Now let's implement the interface in an Abstract class named Student:
Java
// creating an abstract class named Student which is
// implementing the interface,GFG
abstract class Student implements GFG {
// Overriding two methods of the interfacem,GFG
@Override public void learnCoding()
{
System.out.println(
"Let's make coding a habit with GFG");
}
@Override public void learnProgrammingLanguage()
{
System.out.println(
"Let's master all fundamentals of java with the help of GFG");
}
}
Here we have overridden two abstract methods of the interface GFG.
3. Now let's create a class GEEK which extends the abstract class, Student:
As previously mentioned, we can't create an instance of our abstract class therefore we need to make a non-abstract class.
Java
// creating an non-abstract class
// GEEK which is extending Student
class GEEK extends Student {
// overriding the remaining method of the interface,GFG
@Override public void contribute()
{
System.out.println(
"Now let's help others by contributing in GFG");
}
}
Here we have overridden the remaining method of the interface GFG.
Below is the overall implementation of the problem statement:
Java
// Implementation of Interface using Abstract Class in Java
// Interface GFG
interface GFG {
void learnCoding();
void learnProgrammingLanguage();
void contribute();
}
// Abstract class Student implementing from GFG interface
abstract class Student implements GFG {
// Overriding the methods
@Override public void learnCoding()
{
System.out.println(
"Let's make coding a habit with GFG");
}
@Override public void learnProgrammingLanguage()
{
System.out.println(
"Let's master all fundamentals of java with the help of GFG");
}
}
// Extend the GEEK class by Student abstract class
class GEEK extends Student {
@Override public void contribute()
{
System.out.println(
"Now let's help others by contributing in GFG");
}
}
// Driver code
public class Main {
public static void main(String[] args)
{
// New GEEK object is created
GEEK gfgStudent = new GEEK();
// Calls to the multiple functions
gfgStudent.learnCoding();
gfgStudent.learnProgrammingLanguage();
gfgStudent.contribute();
}
}
Output:
Let's make coding a habit with GFG
Let's master all fundamentals of java with the help of GFG
Now let's help others by contributing in GFG
Similar Reads
How to implement an Interface using an Enum in Java Enumerations serve the purpose of representing a group of named constants in a programming language. For example, the 4 suits in a deck of playing cards may be 4 enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named Suit. We have already discussed the basics of enu
3 min read
Difference Between Abstract Class and Interface in Java In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use
9 min read
Are All Methods in a Java Interface are Abstract? In Java, the interface is called a blueprint of a class and it is used to achieve abstraction in java. By using interfaces only we can achieve multiple inheritances in java. Let's understand the concept of blueprint through an example like a blueprint of the building will consist of properties and b
8 min read
Can We Instantiate an Abstract Class in Java? Abstract class, we have heard that abstract class are classes which can have abstract methods and it can't be instantiated. We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.Example 1Java// Java program to demonstrate abstract class /
3 min read
Difference between abstract class and interface in Python In this article, we are going to see the difference between abstract classes and interface in Python, Below are the points that are discussed in this article: What is an abstract class in Python?What is an interface in Python?Difference between abstract class and interface in PythonWhat is an Abstra
4 min read
How to Implement Multiple Inheritance by Using Interfaces in Java? Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when methods with the same signature exist in both the superclasses and subclass. On calling the method, the compiler cannot determine which class m
2 min read