DEV Community

Ranjith Ranjith
Ranjith Ranjith

Posted on

Day 6 (interface )

1] what is Interfaces:

 1)Like abstract classes, interfaces cannot be used to create objects
Enter fullscreen mode Exit fullscreen mode

(in examabove, it is not possible to create an "Animal" object in the MyMainClass)

 2) Interface methods do not have a body - the body is provided by the "implement" class

  3)On implementation of an interface, you must override all of its methods
Enter fullscreen mode Exit fullscreen mode

Interface methods are by default abstract and public

  4)Interface attributes are by default public, static and final
Enter fullscreen mode Exit fullscreen mode

An interface cannot contain a constructor (as it cannot be used to create objects)

2] Why use interface :

  1)interfaces are used to define contracts for classes, enabling abstraction, polymorphism, and multiple inheritance. 
  2) They specify a set of methods that a class must implement, ensuring that all implementing classes share a common behavior.
   3)This allows for loose coupling and flexible code design. 


3] Where do we use an interface : 

  interface keyword in Java is used to declare a special type of class that only contains abstract methods, default methods, static methods, and final variables
Enter fullscreen mode Exit fullscreen mode

Top comments (0)