❮ Previous Next ❯

Implementing Interfaces in java:


Interfaces are used as super classes whose properties are inherited by a class. Thats why it is necessary to create a class that inherits the given interface. This is achieved as follows

Example

class ClassName implements InterfaceName
{
Body of class
}
//The more general form of implementation is
class ClassName extends superclass implements Interface1,Interface2........
{
body of className
}

Here it represents that a class can extend another class while implementing interfaces
Let us see a program to understand this concept


Example

interface Print
{
void display();
}
class Printing implements Print
{
public void display()
{
System.out.println("JavaRace");
}
}
class InterfaceDemo
{
public static void main(String args[])
{
Printing ob = new Printing();
ob.display();
}
}
Output

JavaRace

Multiple inheritance in Java by interface:

If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
Img Insert Here.
Let's see the following program:

Example

interface Test1
{
void display();
}
interface Test2
{
void show();
}
class Simple implements Test1, Test2
{
public void display();
{
System.out.println("JavaRace");
}
public void show()
{
System.out.println("Silan Software");
}
}
class MultipleDemo
{
public static void main(String args[])
{
Simple s = new Simple();
s.display();
s.show();
}
}
Output

JavaRace
Silan Software

Note:

Multiple inheritance is not supported in case of class. But it is supported in case of interface because there is no ambiguity as implementation is provided by the implementation class.

❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved