Interface vs Abstract Class, Abstract method
Interface
DescriptionUsed when
common functionalityis neededwithout being bound by inheritance relationshipsbetween parent and child.Abstract Methodsare defined,and the
implementingclassesOverrideeach function to implement it in various forms, so it is related topolymorphism
An Interface exists to
enforcethe same methods and behavioron the classes that implement it
AdvantagesIt can compensate for the
limitations of Abstract Classescaused by Java's inability to supportmultiple inheritance
DisadvantagesIf all classes use an Interface, there is the
inconvenience of having to Override and redefinecommonly needed functionalityin all implementing classes
SummaryInterfaces are convenient to use when
specifying common functionalityfor classes that inherit from different Abstract Classes
Abstract method
DescriptionIt refers to a method that
must be overriddenin a child classA method is divided into a
declaration part ()and animplementation part {}, and an abstract method is one where only the declaration is written without the implementationAbstract methods are used because the method content varies depending on which class inherits it
The
implementation part {}must be written in the sub-class that inherits the classIf the implementation is not written, an
erroroccurs!
Commentsshould describe what functionality the method performsabstractis not written onoverridingmethods
Abstract Class
DescriptionA class that has even one
Abstract methodis called anAbstract classThat is, a regular class cannot have abstract methods!
A child class that inherits an Abstract class must
overridetheabstract methodsThe abstract class
forces redefinitionof abstract methods on child classesabstract class<->final classIt is the opposite of a final class which
prohibits inheritanceto restrict overriding!
Abstract classes are intended for
program designOn the other hand, regular classes are intended for creating multiple objects and storing data
Abstract classes
cannot be created as objectsThe child class must inherit the abstract class,
overridethe abstract methods to complete the implementation, and then use the child class to create objects
In a parent-child relationship, when
inheriting (extends)the Abstract Class, amongchild classesthat inherit the same parent Class (here, the Abstract Class):Each
implementscommon functionality,extendsit,and it is related to
inheritanceInheritance is used to utilize and extend the functionality of the SuperClass!
Inheriting Abstract Classes enables
distinctionbetween classes
AdvantagesOnce
designis completed in the abstract class, it is convenient toextend functionalityby inheriting in child classesSince
implementation of abstract methods is enforcedin child classes, thedegree of standardizationis increasedSince
common aspectsof classes can be managed in one place,development and maintenancebecome convenient
DisadvantagesSince Java does not support
multiple inheritance, there are limitations inenforcingAbstract Methodsthat must be implemented using onlyAbstract ClassesWhat if multiple inheritance were possible?
If a drive() method is defined in both Car and Motorcycle, it becomes
ambiguous which one is being inherited and OverriddenThis is the
ambiguity of multiple inheritance, and this is why Java has blocked multiple inheritance
Abstract Class vs Interface
Abstract Class represents
IS - A "is a",while Interface represents
HAS - A "is capable of"
Abstract Class is about
inheritance,while Interface is about
polymorphism
Last updated