Description- Used 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
- Used when
Advantages- It can compensate for the
limitations of Abstract Classescaused by Java's inability to supportmultiple inheritance
- It can compensate for the
Disadvantages- If all classes use an Interface, there is the
inconvenience of having to Override and redefinecommonly needed functionalityin all implementing classes
- If all classes use an Interface, there is the
Summary- Interfaces are convenient to use when
specifying common functionalityfor classes that inherit from different Abstract Classes
- Interfaces are convenient to use when
Description- It refers to a method that
must be overriddenin a child class - A method is divided into a
declaration part ()and animplementation part {}, and an abstract method is one where only the declaration is written without the implementation - Abstract 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 class- If the implementation is not written, an
erroroccurs!
- If the implementation is not written, an
Commentsshould describe what functionality the method performsabstractis not written onoverridingmethods
- It refers to a method that
Description- A class that has even one
Abstract methodis called anAbstract class- That is, a regular class cannot have abstract methods!
- A child class that inherits an Abstract class must
overridetheabstract methods- The abstract class
forces redefinitionof abstract methods on child classes abstract class<->final class- It is the opposite of a final class which
prohibits inheritanceto restrict overriding!
- It is the opposite of a final class which
- The abstract class
- Abstract classes are intended for
program design- On the other hand, regular classes are intended for creating multiple objects and storing data
- Abstract classes
cannot be created as objects- The child class must inherit the abstract class,
overridethe abstract methods to complete the implementation, and then use the child class to create objects
- The child class must inherit the abstract class,
- 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
inheritance- Inheritance is used to utilize and extend the functionality of the SuperClass!
- Each
- Inheriting Abstract Classes enables
distinctionbetween classes
- A class that has even one
Advantages- Once
designis completed in the abstract class, it is convenient toextend functionalityby inheriting in child classes - Since
implementation of abstract methods is enforcedin child classes, thedegree of standardizationis increased - Since
common aspectsof classes can be managed in one place,development and maintenancebecome convenient
- Once
Disadvantages-
Since Java does not support
multiple inheritance, there are limitations inenforcingAbstract Methodsthat must be implemented using onlyAbstract Classes -
What if multiple inheritance were possible?
class Vehicle extends Car, Motorcycle { @Override public void run(){ super.drive(); } }
- If a drive() method is defined in both Car and Motorcycle, it becomes
ambiguous which one is being inherited and Overridden - This is the
ambiguity of multiple inheritance, and this is why Java has blocked multiple inheritance
- If a drive() method is defined in both Car and Motorcycle, it becomes
-
- Abstract Class represents
IS - A "is a",- while Interface represents
HAS - A "is capable of"
- while Interface represents
- Abstract Class is about
inheritance,- while Interface is about
polymorphism
- while Interface is about