Interface in JAVA

Interface & Its Advantage:-

1. If all the methods in the class is abstract then the class is nothing but the interface.

2. We cannot create an object of interface.

3. To implement the interface by child class keyword “implements” is used.

4. Interface is defined by using keyword interface.

5. If a class implements interface but does not implements methods, then the class is abstract class.

6. We can assign the object of class which implements the interface of type of                                                     that interface.

interface Duck
{
    Void DuckType();
    Void DuckProperty();
}

class BlackDuck implements Duck
{
 Public void DuckType()
 {
     System.out.println("Black Duck");
 }

 Public void DuckProperty()
 {
   System.out.println("Swim, Quack  and Fly");
 }
 }

class TestDuck
{
 Public static void main(String args[])
 {
     BlackDuck obj=new BlackDuck();
     Obj.DuckType();
     Obj.DuckProperty();
 }
}

Advantages of Interface:-

  • Runtime Polymorphism
  • Contract can be used throughout the application

To know more about inheritance and abstract classes, refer this article.

Posted

in

by

Tags:


Related Posts

Comments

2 responses to “Interface in JAVA”

  1. […] To read more on interface, refer this article. […]

  2. Inigo Avatar
    Inigo

    My problem with descriptions of interfaces is that you can create the child classes and define the methods without using an interface. Since the child class is being instantiated anyway, what is the point of creating an interface? If you need to change the base interface, you’d need to change the children anyway, so I still don’t see the advantage in the examples presented.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading