简体   繁体   English

在java中实现抽象方法/类

[英]implementing abstract methods/classes in java

Can I implement abstract methods in an abstract base class A in java? 我可以在java中的抽象基类A中实现抽象方法吗?

If the answer is yes and there is an implemented abstract method in a base class A and there is a derived class B from A (B is not abstract). 如果答案是肯定的,并且在基类A中存在实现的抽象方法,并且存在来自A的派生类B(B不是抽象的)。 Does B still has to implement that base abstract method? B仍然必须实现该基本抽象方法吗?

If I understand your question correctly, Yes. 如果我理解你的问题,是的。

public abstract class TopClass {
  public abstract void methodA();
  public abstract void methodB();
}

public abstract class ClassA extends TopClass {
  @Override
  public void methodA() {
    // Implementation
  }
}

public class ClassB extends ClassA {
  @Override
  public void methodB() {
    // Implementation
  }
}

In this example, ClassB will compile. 在这个例子中,ClassB将编译。 It will use it's own implementation of methodB(), and ClassA's implementation of methodA(). 它将使用它自己的methodB()实现,以及ClassA的methodA()实现。 You could also override methodA() in ClassB if desired. 如果需要,您还可以覆盖ClassB中的methodA()。

You could have two abstract classes, X and Y, where Y extends X. In that case it could make sense for Y to implement an abstract method of X, while still being abstract. 你可以有两个抽象类,X和Y,其中Y扩展X.在这种情况下,Y实现X的抽象方法,同时仍然是抽象的,这是有意义的。 Another non-abstract class Z could extend Y. However, in your example, for A to implement its own abstract methods is a contradiction, the point of making them abstract is so it doesn't provide implementations, it just specifies what the method signatures should look like. 另一个非抽象类Z可以扩展Y.但是,在你的例子中,对于A来实现它自己的抽象方法是一个矛盾,使它们成为抽象的点是因为它不提供实现,它只是指定方法签名是什么应该是这样的。

Abstract classes can have regular methods. 抽象类可以有常规方法。 If you want to implement some of the methods of class A and leave rest of the methods abstract, you can do this. 如果要实现A类的某些方法并将其余方法保留为抽象,则可以执行此操作。 However, abstract methods cannot have a body, therefore if you mark a method as abstract, then it has to be implemented by a subclass, you can't implement them in the abstract class. 但是,抽象方法不能有一个体,因此如果将方法标记为抽象,则必须由子类实现,不能在抽象类中实现它们。 However, you can have an abstract class without abstract methods, then subclass only needs to extend it. 但是,您可以拥有一个没有抽象方法的抽象类,然后子类只需要扩展它。

Yes, you can implement abstract methods in a class which is declared as abstract. 是的,您可以在声明为abstract的类中实现抽象方法。 If a class is declared abstract that does not mean all its method must be abstract. 如果一个类被声明为abstract,并不意味着它的所有方法都必须是抽象的。

For a concrete sub class, it is not mandatory to implement the abstract methods that are already implemented by one of their super class. 对于具体的子类,不必强制实现已由其超类之一实现的抽象方法。

No . Abstract methods are meant to be defined by the subclass(es). 抽象方法意味着由子类定义。 For more information, see Abstract Methods and Classes . 有关更多信息,请参阅抽象方法和类 However, you can define a method in the base class and have the subclass(es) override it, if required. 但是,您可以在基类中定义方法,并在需要时让子类覆盖它。

Yes, but it can't be abstract any more. 是的,但它不再是抽象的。 Abstract means there is no implementation. 摘要意味着没有实现。

What you can do is: 你能做的是:

interface I {
    void meth();
}

//and

abstract class A implements I {
    public void meth() {
        //implementation
    }
}

Or: 要么:

abstract class A {

    public abstract void meth();
}

//and

abstract class B extends A {
    public void meth() {
    }
}

If A already has an implementation, you can override it in B (if B is concrete), because B inherits that default implementation from A. 如果A已经有一个实现,你可以在B中覆盖它(如果B是具体的),因为B从A继承了该默认实现。

如果你实现一个抽象方法,它不再是抽象的,所以没有。

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

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