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

Find Us On Facebook

Contact Us

Name

Email *

Message *

C Programming

More

C++ Tutorial

More

Java Tutorial

More

software engineering

More

MS Office

More

Database Management

More

Advertisement

Top