简体   繁体   English

抽象类是否扩展了对象?

[英]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.在这里它工作正常,我不需要覆盖接口方法,因为 Object 是 B 的超类,并且这些方法在其中实现。

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?这是否意味着 Object 不是抽象类的超类? Or am i missing some point?还是我遗漏了一点?

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 .您已经创建了它们必须在A子类中实现的要求。

Class C does not implement those methods, so compilation failure. C类没有实现这些方法,所以编译失败。

Object is a superclass of abstract classes... but it's not a subclass , and subclasses are responsible for implementing abstract functions. Object是抽象类的类……但它不是子类子类负责实现抽象功能。

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.有些用例你可能不会,比如退化/糟糕的设计,或者在探索语言特性时像这样的例子。

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.正如其他人已经提到的, A类通过再次将它们声明为抽象来覆盖Object的这些方法,因此它强制子类实现它们。

As a clarification for your situation try defining A as follows:为了说明您的情况,请尝试如下定义A

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.在这种情况下, AC都从Object继承了这些方法的实现,并且不会发生编译错误。

Object is a super class of all classes, abstract or not. Object 是所有类的超类,无论抽象与否。

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.这与 Object 类无关。 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.这将给出编译错误The type C must implement the inherited abstract method B.someMethod() ,即使 A 已经实现了它。

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.这是一个奇怪的情况,乍一看,您可能会认为像这样声明抽象类 A 是编译器错误。

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.例如,如果您想确保使用 A 类的每个人都自己实现了它们的 equals 和 hashcode,而不是依赖于 Object 版本,那么您可以这样做。

The actual explanation for the behavior is that to extend class A you need to meet every requirement that class A presents to you.对该行为的实际解释是,要扩展 A 类,您需要满足 A 类向您提出的所有要求。

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.在这种特殊情况下,类 A 说子类需要实现这些方法,超类实现它们的事实无关紧要,它本身添加了更具体的要求。

There is nothing special about Object here: Object 没有什么特别之处:

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 .那么就不能说C is not abstract and does not override abstract method test() in B

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.在您针对hashCodeequals情况下,此类调用将导致运行时错误,因为这些方法未实现。 The compiler prevents this from happening by raising a compiler error.编译器通过引发编译器错误来防止这种情况发生。

Yes,Object is super class of abstract class.You can prove using annotation @Override to help you.是的,Object 是抽象类的超类。您可以使用注解@Override 来帮助您证明。

See,there is no error in the following code :看,下面的代码没有错误:

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

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

Every class extends Object.每个类都扩展对象。 Even an abstract class extends Object, but an abstract class is incomplete .即使abstract类扩展了 Object,但抽象类是不完整的 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!试试看!

Every class extends Object .每个类都扩展 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.并且您的抽象类有方法但没有任何主体。

无论何时扩展抽象类,都应确保抽象类中的所有方法都必须在子类中实现,否则会导致编译错误。

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所有类(抽象或非抽象类)都将Object隐式扩展为父类

  • If class extends another parent class explicitly then it inherits the methods of Object superclass via that parent class.如果 class显式扩展另一个父类,则它通过该父类继承Object超类的方法。

  • 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.在您的情况下,最初在Object超类中实现的 equals() 和 hashCode() 被重写抽象类中的抽象方法,隐式扩展了它。

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.没有别的办法。

Yes..Abstract classes extend Java.lang.Object class like any other classes in Java.是的..抽象类像 Java 中的任何其他类一样扩展 Java.lang.Object 类。

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.当一个类没有扩展任何其他类时,编译器在编译期间插入“扩展 java.lang.Object”,这意味着,只有当一个类没有任何其他用户定义的父类时,它才会扩展 Object 类。

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.在这里,C 类已经扩展了 A,因此它不会扩展 Object 类。 As you haven't provided the implementation of equals method , you will see a compilation error.由于您尚未提供 equals 方法的实现,您将看到编译错误。

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.所以对象类可以从抽象类继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM