Friday, October 14, 2022

Implement Interface using Abstract Class in Java

Java Interface, Abstract Class, Java Career, Java Prep, Java Preparation, Java Tutorial and Materials, Java Certification, Java Jobs

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:


// creating an interface named ORACLEJAVACERTIFIED 
interface ORACLEJAVACERTIFIED { 
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:


// creating an abstract class named Student which is 
// implementing the interface,ORACLEJAVACERTIFIED 
abstract class Student implements ORACLEJAVACERTIFIED { 
// Overriding two methods of the interfacem,ORACLEJAVACERTIFIED
@Override public void learnCoding() 
System.out.println( 
"Let's make coding a habit with ORACLEJAVACERTIFIED"); 
@Override public void learnProgrammingLanguage() 
System.out.println( 
"Let's master all fundamentals of java with the help of ORACLEJAVACERTIFIED"); 
}

Here we have overridden two abstract methods of the interface ORACLEJAVACERTIFIED.

3. Now let’s create a class JAVA 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.

// creating an non-abstract class 
// JAVA which is extending Student 
class JAVA extends Student { 
// overriding the remaining method of the interface,ORACLEJAVACERTIFIED 
@Override public void contribute() 
System.out.println( 
"Now let's help others by contributing in ORACLEJAVACERTIFIED"); 
}

Here we have overridden the remaining method of the interface ORACLEJAVACERTIFIED.

Below is the overall implementation of the problem statement:

// Implemention of Interface using Abstract Class in Java 

// Interface ORACLEJAVACERTIFIED 
interface ORACLEJAVACERTIFIED { 
void learnCoding(); 
void learnProgrammingLanguage(); 
void contribute(); 

// Abstract class Student implementing from ORACLEJAVACERTIFIED interface 
abstract class Student implements ORACLEJAVACERTIFIED { 

// Overriding the methods 
@Override public void learnCoding() 
System.out.println( 
"Let's make coding a habit with ORACLEJAVACERTIFIED"); 
@Override public void learnProgrammingLanguage() 
System.out.println( 
"Let's master all fundamentals of java with the help of ORACLEJAVACERTIFIED"); 

// Extend the JAVA class by Student abstract class 
class JAVA extends Student { 
@Override public void contribute() 
System.out.println( 
"Now let's help others by contributing in ORACLEJAVACERTIFIED"); 

// Driver code 
public class Main { 
public static void main(String[] args) 
// New JAVA object is created 
JAVA oraclejavacertifiedStudent = new JAVA(); 

// Calls to the multiple functions 
oraclejavacertifiedStudent.learnCoding(); 
oraclejavacertifiedStudent.learnProgrammingLanguage(); 
oraclejavacertifiedStudent.contribute(); 
}

Output:


Let's make coding a habit with ORACLEJAVACERTIFIED
Let's master all fundamentals of java with the help of ORACLEJAVACERTIFIED
Now let's help others by contributing in ORACLEJAVACERTIFIED

Source: geeksforgeeks.org

Related Posts

0 comments:

Post a Comment