📜  在Java中使用抽象类实现接口

📅  最后修改于: 2022-05-13 01:55:09.474000             🧑  作者: Mango

在Java中使用抽象类实现接口

接口仅包含无法实例化的抽象方法,并由关键字interface声明。使用abstract关键字声明的类在Java中称为抽象类。这是一个通常包含至少一个无法实例化的抽象方法的类,并且该类也可能根本没有任何方法。无法创建抽象类的实例。

现在因为接口中的所有方法都是抽象方法,所以我们可以使用抽象类来实现它。

1. 我们首先创建一个接口:

Java
// creating an interface named GFG
interface GFG {
    void learnCoding();
    void learnProgrammingLanguage();
    void contribute();
}


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");
    }
}


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");
    }
}


Java
// Implemention 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();
    }
}


这里三个未实现的方法是抽象方法

2. 现在让我们在名为 Student 的 Abstract 类中实现接口:

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");
    }
}

这里我们重写了接口 GFG 的两个抽象方法。

3. 现在让我们创建一个类 GEEK,它扩展了抽象类 Student:

如前所述,我们无法创建抽象类的实例,因此我们需要创建一个非抽象类。

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");
    }
}

这里我们重写了接口 GFG 的其余方法。

下面是问题陈述的整体实现:

Java

// Implemention 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();
    }
}

输出:

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