What is the difference between an interface and abstract class?

前端 未结 30 2309
情歌与酒
情歌与酒 2020-11-21 11:51

What exactly is the difference between an interface and abstract class?

30条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 12:29

    I am constructing a building of 300 floors

    The building's blueprint interface

    • For example, Servlet(I)

    Building constructed up to 200 floors - partially completed---abstract

    • Partial implementation, for example, generic and HTTP servlet

    Building construction completed-concrete

    • Full implementation, for example, own servlet

    Interface

    • We don't know anything about implementation, just requirements. We can go for an interface.
    • Every method is public and abstract by default
    • It is a 100% pure abstract class
    • If we declare public we cannot declare private and protected
    • If we declare abstract we cannot declare final, static, synchronized, strictfp and native
    • Every interface has public, static and final
    • Serialization and transient is not applicable, because we can't create an instance for in interface
    • Non-volatile because it is final
    • Every variable is static
    • When we declare a variable inside an interface we need to initialize variables while declaring
    • Instance and static block not allowed

    Abstract

    • Partial implementation
    • It has an abstract method. An addition, it uses concrete
    • No restriction for abstract class method modifiers
    • No restriction for abstract class variable modifiers
    • We cannot declare other modifiers except abstract
    • No restriction to initialize variables

    Taken from DurgaJobs Website

提交回复
热议问题