Understanding the purpose of Abstract Classes in Java

前端 未结 8 1649
伪装坚强ぢ
伪装坚强ぢ 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.
    0 讨论(0)
  • 2020-12-01 06:40

    @Override

    @Override was introduced in Java 5 (and extended a little bit in Java 6). It's only informative. It says "I'm suppose to override something that already exist in parent class or interface.

    IDE's like Eclipse can warn you in case there's no such parent method (by example if you mispell the name). In that case your method will not be invoked (because of the mispelling).

    But don't worry too much about it.

    Abstract class vs interface

    An abstract class allows you define a basic functionality leaving undefined parts. An interface doesn't allow you to implement anything. You can program everything except the part that really changes in each case. So when you need it, you inherit and implement the missing part.

    Override two methods

    Yes. In Java you can override all methods not explicity declared as final in parent class. It's ok. If you want to make it unmodifiable you can declare it final. By example, if you want to declare an ordering you could:

    public abstract class Ordering<X>
    {
    abstract boolean isLower(X a, X b);
    abstract boolean isEquals(X a, X b);
       final boolean isGreater(X a, X b) {
          return !isLower(a, b) && !isEquals(a, b);
       }
    }
    

    Of course it may have sense to override isGreater to implement it another more efficient way (imagine it's costly to compare). But there are scenarios when you want to provide basic already implemented functionality (and then's when abstract classes are better than interfaces) or when you want to force some implementation (then's when final keyword show useful).

    0 讨论(0)
提交回复
热议问题