Thursday, 23 April 2020

Implementing and Extending an Interface in Java

Like classes, interfaces can also be extended. That is, an interface can be sub-interfaced from other interface. This is achieved using the keyword extends as shown below:

syntax:
interface i2 extends i1
{
  body of interface2
}
Example program
interface i1
{
  void display();
}

interface i2 extends
{
  void show();
}

class MyClass implements i2
{

    public void display()
    {
      System.out.println(“ interface one method”);
    }

    public void show()
    {
      System.out.println(“ interface two method”);
    }
}

class ExtendDemo
{
public static void main(String args[])
{
    MyClass m=new MyClass();
     m.display();
     m.show();
}
}

Implementing interfaces

Interfaces used as super classes whose properties inherited by classes. It is therefore necessary to create a class that inherits the given interface. This is done as follows

Syntax:

class classname implements interfaceName 
{

body of the class 
}




Example:
interface i1
{
  void display();
}

interface i2 extends
{
  void show();
}

class MyClass implements i2
{

    public void display()
    {
      System.out.println(“ interface one method”);
    }

    public void show()
    {
      System.out.println(“ interface two method”);
    }
}

class ImplementDemo
{
public static void main(String args[])
{
    MyClass m=new MyClass();
     m.display();
     m.show();
}
}


0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top