[oop] Interface vs Abstract Class (general OO)

i will explain Depth Details of interface and Abstract class.if you know overview about interface and abstract class, then first question arrive in your mind when we should use Interface and when we should use Abstract class. So please check below explanation of Interface and Abstract class.

  1. When we should use Interface?

    if you don't know about implementation just we have requirement specification then we go with Interface

  2. When we should use Abstract Class?

    if you know implementation but not completely (partially implementation) then we go with Abstract class.

    Interface

    every method by default public abstract means interface is 100% pure abstract.

    Abstract

    can have Concrete method and Abstract method, what is Concrete method, which have implementation in Abstract class, An abstract class is a class that is declared abstract—it may or may not include abstract methods.

    Interface

    We cannot declared interface as a private, protected

    Q. Why we are not declaring Interface a private and protected?

    Because by default interface method is public abstract so and so that reason that we are not declaring the interface as private and protected.

    Interface method
    also we cannot declared interface as private,protected,final,static,synchronized,native.....

    i will give the reason: why we are not declaring synchronized method because we cannot create object of interface and synchronize are work on object so and son reason that we are not declaring the synchronized method Transient concept are also not applicable because transient work with synchronized.

    Abstract

    we are happily use with public,private final static.... means no restriction are applicable in abstract.

    Interface

    Variables are declared in Interface as a by default public static final so we are also not declared variable as a private, protected.

    Volatile modifier is also not applicable in interface because interface variable is by default public static final and final variable you cannot change the value once it assign the value into variable and once you declared variable into interface you must to assign the variable.

    And volatile variable is keep on changes so it is opp. to final that is reason we are not use volatile variable in interface.

    Abstract

    Abstract variable no need to declared public static final.

i hope this article is useful.

Examples related to oop

How to implement a simple scenario the OO way When to use 'raise NotImplementedError'? PHP: cannot declare class because the name is already in use Python class input argument Call an overridden method from super class in typescript Typescript: How to extend two classes? What's the difference between abstraction and encapsulation? An object reference is required to access a non-static member Java Multiple Inheritance Why not inherit from List<T>?

Examples related to interface

Cast object to interface in TypeScript When to use Interface and Model in TypeScript / Angular Is there a way to create interfaces in ES6 / Node 4? Can a normal Class implement multiple interfaces? When to use: Java 8+ interface default method, vs. abstract method How should I have explained the difference between an Interface and an Abstract class? When do I have to use interfaces instead of abstract classes? How to extend a class in python? Interface type check with Typescript Abstract Class vs Interface in C++

Examples related to abstract-class

invalid new-expression of abstract class type Class is not abstract and does not override abstract method When to use: Java 8+ interface default method, vs. abstract method Spring can you autowire inside an abstract class? Abstract Class:-Real Time Example How should I have explained the difference between an Interface and an Abstract class? When do I have to use interfaces instead of abstract classes? Is it possible to make abstract classes in Python? Abstract Class vs Interface in C++ How do you handle a "cannot instantiate abstract class" error in C++?