Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 526
Syam S Avatar asked Jul 24 '14 15:07

Syam S


People also ask

Does every class extend object?

Yes, since Object is the parent of all classes, then every class must extend Object (directly xor indirectly).

Does abstract class extend or implement?

Implementation: Abstract class can provide the implementation of the interface. Interface can't provide the implementation of an abstract class. Inheritance vs Abstraction: A Java interface can be implemented using the keyword “implements” and an abstract class can be extended using the keyword “extends”.

Can abstract class extend ordinary class?

I earlier learned that abstract class can extend concrete class. Though I don't see the reason for it from JAVA designers, but it is ok. I also learned that abstract class that extends concrete class can make overriden methods abstract.

Can an abstract class extend?

In Java, abstract means that the class can still be extended by other classes but that it can never be instantiated (turned into an object).


Video Answer


2 Answers

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.

like image 99
Dave Newton Avatar answered Oct 15 '22 20:10

Dave Newton


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.

like image 45
MicSim Avatar answered Oct 15 '22 21:10

MicSim