Java Topics

Interface

  • Interface is a type of class which contains only abstract methods and data members which are by default Static and final.
  • We can't create object for interface.
  • All the methods inside the interface are by default considered as abstract method we don't need to write abstract keyword for method within the interface.
  • We don't use concrete methods in interface.
  • We can create reference variable for the interface to support derived casting and runtime polymorphism.
  • If a class is implementing an interface then the class should override the abstract methods of the interface else the class becomes abstract.
  • One interface cannot implement another interface.

            ex: interface Demo implements Run

  • One interface can extend another interface.

            ex: interface Demo extends Run

  • Interfaces do not contain constructors because we can't create object for interface.
  • Interface do not contain non static members. If it is non static we have to create object, so creation of object is not possible in interface.
  • One class cannot extends interface but one class can extends another class.

            ex: Below is wrong because class should use implements keyword instead of extends keyword

interface Run {

        }

class Demo extends Run{

}

Key points to Remember:

  • If we declare data member as final, then we can't able to reinitialize.
  • If we declare class as final, we cannot able to inherit.
  • If we declare method as final, we can't able to override.
  • A class can extend another class and implement any number of interfaces at the same time.
            ex: class C1 extends C2 implements i1,i2,i3

No comments:

Post a Comment