Syam S
Syam S

Reputation: 8499

Does abstract class extend Object?

I have read about the difference between interfaces and abstract classes, but this one is confusing. Consider this interface and class.

interface I {
    public int hashCode();
    public boolean equals(Object obj);
}

class B implements I {
    // Works Fine
}

Here it works fine and i need not override interface methods because Object is a super class of B and those methods are implemented in it.

Now consider these

abstract class A {
    public abstract int hashCode();
    public abstract boolean equals(Object obj);
}

class C extends A {
    // Compile error because methods are not overridden
}

Why would this result in compile error? Does this mean Object is not a super class for abstract class? Or am i missing some point?

Upvotes: 26

Views: 5443

Answers (12)

Gowthami
Gowthami

Reputation: 1

Yes..Abstract classes extend Java.lang.Object class like any other classes in Java.

Compiler inserts "extends java.lang.Object" during compilation when a class is not extending any other class which means, a class extends Object class only when it doesn't have any other user defined Parent class.

abstract class A {
    public abstract int hashCode();
    public abstract boolean equals(Object obj);
}

class C extends A {
    // Compile error because methods are not overridden
}

Here, Class C is already extending A so it won't extend Object class. As you haven't provided the implementation of equals method , you will see a compilation error.

Upvotes: 0

Pat
Pat

Reputation: 1

Key points to keep in mind before working with interfaces/abstract classes =>

  • Interfaces by definition cannot extend a class.

  • All classes (abstract or not) implicitly extend Object as a parent

  • If class extends another parent class explicitly then it inherits the methods of Object superclass via that parent class.

  • Class extending abstract class must implement all inherited abstract methods OR be declared as an abstract class itself

In your case, equals( ) and hashCode( ) originally implemented in the Object superclass are overridden as abstract methods in the abstract class implicitly extending it.

So to avoid compilation error, you must either implement the inherited abstract methods in the child class or declare the child class itself as abstract. There isn't another way.

Upvotes: 0

Sarvesh Kaushik
Sarvesh Kaushik

Reputation: 1

Yes abstract class does extends the object class. And it inherits properties of object class. We should not be confused here that abstract class can't be instantiate. That will happen in sub class of abstract class but in abstract class we can instantiate the object class and can do override the basic implementation. So object class can be inherited from an abstract class.

Upvotes: -2

Dave Newton
Dave Newton

Reputation: 160191

It results in a compile error because by definition abstract functions must be implemented downstream in the inheritance chain. You've created the requirement they must be implemented in a subclass of A.

Class C does not implement those methods, so compilation failure.

Object is a superclass of abstract classes... but it's not a subclass, and subclasses are responsible for implementing abstract functions.

In contrast, if a class implements an interface, the implementation can live anywhere in that class's inheritance hierarchy. It's less common to have those implementations lie in a superclass, because you'd generally declare the interface in the superclass.

There are use cases where you might not, like degenerate/poor design, or examples like this while poking around language features.

Upvotes: 34

Ungapps
Ungapps

Reputation: 98

Yes,Object is super class of abstract class.You can prove using annotation @Override to help you.

See,there is no error in the following code :

abstract class A 
{   
    @Override
    public abstract int hashCode();

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

Upvotes: 1

Georgie
Georgie

Reputation: 61

Whenever you extend an abstract class you should make sure that ALL the methods in the abstract class must be implemented in the sub class, or else it will lead to compilation error..

Upvotes: 0

Tim B
Tim B

Reputation: 41188

This is an odd case, really at first glance you might expect it to be a compiler error to declare the abstract class A like that.

In fact there are some (uncommon) reasons you might want to. For example if you wanted to make sure that everyone using class A had implemented their equals and hashcode themselves instead of relying on the Object version then you could do this.

The actual explanation for the behavior is that to extend class A you need to meet every requirement that class A presents to you.

In this particular case class A is saying that sub classes need to implement these methods, the fact that a super class has implemented them is irrelevant, it is adding a more specific requirement itself.

There is nothing special about Object here:

abstract class A {
    public abstract int hashCode();
    public abstract boolean equals(Object obj);
    public void test() {

    }
 }

 abstract class B extends A {
     public abstract void test();
 }  

Now if you try to define:

class C extends B {
    public int hashCode() { return 1; }
    public boolean equals(Object ob) { return false; }
}

Then that will fail saying that C is not abstract and does not override abstract method test() in B.

Upvotes: 3

MicSim
MicSim

Reputation: 26796

As already mentioned by others, class A overrides those methods in Object by declaring them again as abstract, so it forces subclasses to implement them.

As a clarification for your situation try defining A as follows:

abstract class A {
    //public abstract int hashCode();
    //public abstract boolean equals(Object obj);
}

class C extends A {
    // no compile error because no abstract methods have to be overridden
}

In this case both A and C inherit the implementation of those methods from Object and no compilation error occurs.

Upvotes: 6

Deepanshu J bedi
Deepanshu J bedi

Reputation: 1530

Every class extends Object.(father of all classes) it is a compilation error because you have to provide the body of an abstract class. and your abstract class has methods but without any body.

Upvotes: 0

Fabian Lauer
Fabian Lauer

Reputation: 9897

Your abstract class has virtual methods (methods without implementation) only. This means that they exist in the class's interface, thus someone might actually call them. Such a call, in your case against hashCode or equals, would result in a runtime error as these methods are not implemented. The compiler prevents this from happening by raising a compiler error.

Upvotes: 1

Solomon Slow
Solomon Slow

Reputation: 27115

Every class extends Object. Even an abstract class extends Object, but an abstract class is incomplete. You are not allowed to instantiate an abstract class, and any class that extends an abstract class must either supply all of the missing methods, or it must also be declared abstract.

Would this result in a compile error?

Try it and see!

Upvotes: 0

Eran
Eran

Reputation: 393831

Object is a super class of all classes, abstract or not.

I believe that when you declare a class as abstract and declare in it abstract methods, you force any sub-class to implement them, regardless of whether a super-class of the abstract class already implements them.

This has nothing to do with the Object class. You'll get the same behavior if you create all the classes yourself :

public class A {

   public int someMethod () {
       return 1;
   }
}

public abstract class B extends A {
   public abstract int someMethod ();
}

public class C extends B {

}

This will give the compilation error The type C must implement the inherited abstract method B.someMethod(), even though A already implements it.

Upvotes: 3

Related Questions