Understanding the purpose of Abstract Classes in Java

前端 未结 8 1711
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 06:15

Suppose I have two Classes, A and B. The A class is defined as abstract, while B extends this abstract class, and finally i test the result and both classes are part of same

8条回答
  •  不思量自难忘°
    2020-12-01 06:39

    This article has some good concepts (and examples) regarding the title of your question.

    Abstract Classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract Classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.

    Suppose that you want to define a class with certain methods, but for whatever design purpose of your program/project, you want to make sure that an specific method (Abstract Method) must be implemented when the Abstract Class is extended.

    Basically, the purpose of Abstract Classes is to define a Class with methods that:

    • must be declared (abstract) on the subclass
    • are optionally used (non-abstract) on the subclass


    Additional Comment

    • Few years ago, I developed an application for Android (Java, not Kotlin), and during the usage of many libraries, I experienced the behaviour of extending my classes with Abstract Classes and due to the behaviour of this type of class, Android Studio automatically added to my code, all Abstract Methods that were part of the Abstract Class which I was extending.

提交回复
热议问题