Why can an abstract class force a concrete method to be overridden?

I use a library where an abstract class overrides a concrete method inherited from Object with an abstract method:

public abstract class A { @Override public abstract boolean equals(Object obj); }

To extend this class, I have to implement the equals method:

public class B extends A { @Override public boolean equals(Object obj) { return obj != null && obj.getClass() == B.class; } }


Why can an abstract method (A::equals) override a concrete method (Object::equals)? I don't see the goal of this.

--------------Solutions-------------

In this specific example it makes perfect sense. If sub-classes of A are meant to be used in Collections, where equals is widely used to locate objects, making A's equals method abstract forces you to give non-default implementation of equals in any sub-classes of A (instead of using the default implementation of the Object class which only compares instance references).

Of course, your suggested implementation of equals in B makes little sense. You should be comparing the properties of the 2 B instances to determine if they are equal.

This is a more suitable implementation :

public class B extends A {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof B))
return false;
B other = (B) obj;
return this.someProperty.equals(other.someProperty) && this.secondProperty.equals(other.secondProperty);
}
}

In addition, remember to override hashCode whenever you override equals (since the contract of equals and hashCode requires that if a.equals(b) == true then a.hashCode() == b.hashCode()).

Because in this case, you would want your objects to define their own equals, which supposedly will behave differently from the default implementation.

You should not look at this as removing functionality, but rather as enforcing that inheriting objects implement their own.

This would allow you to force a subclass to reimplement a method. Whether this is a good idea or not is another matter. You would only do this if you wanted to enforce a stronger contract than the original method provided. You should then carefully document the new contract.

It means you must implement your own equals() method

Because all classes in Java inherently extend the Object class. Class A will inherit the Object#equals method. Suppose you wanted to force a compile error when the equals method is not explicitly implemented like in this example. Making the equals method abstract without an implementation block would enable you to do this.

Category:java Time:2018-12-17 Views:3

Related post

  • Can an abstract class have a final method? 2009-08-19

    Can an abstract class have a final method in Java? --------------Solutions------------- Sure. Take a look at the Template method pattern for an example. abstract class Game { protected int playersCount; abstract void initializeGame(); abstract void m

  • Why can't a class declare a static method and an instance method with the same signature 2011-11-08

    Possible Duplicate: Static and Instance methods with the same name? This question came up after a discussion we had on the team today. I am not 100% familiar with how the CLR actually loads a class's code from an assembly, however i thought that stat

  • Can an abstract class force the inheriting class to implement a method as static? 2011-12-08

    Python 3.2 in case that matters... The following code shows that the "concrete class" can either implement some_method as a static method or an instance method: import abc class SomeAbstractClass(metaclass=abc.ABCMeta): @abc.abstractmethod def some_m

  • Abstract class with all concrete methods 2008-12-12

    Are there some practical programming situations for someone to declare a class abstract when all the methods in it are concrete? --------------Solutions------------- Well you could be using a template method pattern where there are multiple override

  • Why can't a class extend traits with method of the same signature? 2009-12-02

    Why is the error below? How to workaround it? EDIT: I assumed that since A and B compile to (interface,class) pairs, it's a matter of choosing the right static method call to implement when compiling C. I would expect the priority to be according to

  • How to tell Pex not to stub an abstract class that has concrete implementations 2011-09-21

    I'm trying to use Pex to test some code. I have an abstract class with four concrete implementations. I have created factory methods for each of the four concrete types. I had also created one for the abstract type, except as this nice thread explain

  • How is abstract class different from concrete class? 2012-04-18

    I understand WHY we need Abstract Class in Java - to create sub-classes. But the same can be achieved by concrete class. e.g. Class Child extends Parent. Here Parent can very well be abstract & concrete. So why do we have ABSTRACT?? -------------

  • Can an abstract class have a constructor? 2008-11-04

    Can an abstract class have a constructor? If so, how it can be used and for what purposes? --------------Solutions------------- Yes, an abstract class can have a constructor. Consider this: abstract class Product { int multiplyBy; public Product( int

  • Can an abstract class be used as a reference type? 2010-11-19

    I'm not so sure what that would imply and my textbook is dancing around the answer. --------------Solutions------------- Yes you can: abstract class AbstractClass { } class DeclaredClass extends AbstractClass { } AbstractClass c = new DeclaredClass()

  • Why can't inner classes declare static members? 2011-12-18

    The Java Tutorial says that since an inner class is associated with an instance of the enclosing class, it (the inner class) cannot define any static members itself. It's interesting for me why can't inner classes declare static members for instance,

  • Why can't a class inherit from the result of a decltype? 2012-02-20

    Why can't a class have a decltype in the inheritance list? For instance, I would expect the following code to make A<B> inherit from RType, but with G++ 4.6.1 (using -std=c++0x) it does not compile: #include <type_traits> template<type

  • How to prevent SBT from trying to execute abstract classes with JUnit test methods? 2011-02-14

    I'm migrating a mixed Java/Scala project from Maven to SBT. When I run "test" I get lots of errors on classes that are abstract classes with JUnit test methods in them. They all have a name of the form Abstract*TestCase. Maven safely ignored these cl

  • Why can't we use assertion for public methods? 2011-03-23

    Why can't we use assertion for public methods? I have read somewhere "An assert is inappropriate in public methods because the method guarantees that it will always enforce the argument checks. A public method must check its arguments whether or not

  • Why we need abstract class when we can inherit normal class? 2012-03-11

    Possible Duplicate: Interface vs Abstract Class (general OO) Following are the confusions which I have between interface, abstract and normal class- I know an abstract class can have a method with no implementation, but what is the harm in declaring

  • Can an abstract class be member of other concrete class as composition relationship ? c++ 2012-03-20

    P is an abstract class, I want to make it member of class A which is a normal concrete class. Is it possible if yes how. Relationship is composition Thanks for help --------------Solutions------------- Since P is abstract, you can never create an obj

  • Why should an abstract class implement an abstract method of an abstract base class? 2010-07-17

    In the following example, the class Derived implements the abstract method method from class Main. But I can't think of a reason to fill in the method body in the abstract Derived class' implementation. Surely I should only implement abstract methods

  • Abstract class with pure virtual method - why is it possible to do "Abstract * abs3;"? 2011-09-01

    Consider the following : class Abstract { public: virtual void func() = 0; }; int main() { Abstract abs1; // doesn't compile Abstract * abs2 = new Abstract(); // doesn't compile Abstract * abs3; // compiles return 0; } Please notice that I didn't imp

  • When and Why to use abstract classes/methods? 2010-07-27

    I have some basic questions about abstract classes/methods.I know basic use of abstract classes is to create templates for future classes. But are there any more uses of them ? When should you prefer them over interfaces and when not ? Also when are

  • Can one abstract class extend another abstract class and increase functionality 2011-07-19

    I have an abstract class. I want to extend the abstract class by another abstract class and then implement the extended abstract class. Is it possible .If yes, whether it's a good approach in point of view regarding OOPS? --------------Solutions-----

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.469 (s). 13 q(s)