2014-03-05 205 views
-4

抽象类由另一个抽象类和非抽象类扩展时是否有区别?例如,父抽象类同时具有抽象方法和非抽象方法。当抽象类被抽象类和非抽象类分别扩展时,在实现方法时是否有区别?由另一个抽象类和非抽象类继承抽象类

+1

'差异'在哪方面? –

+0

这个问题将被关闭,因为不清楚你在问什么。 –

+0

你试过了吗?如果你尝试,那么你会知道其中的差异。 –

回答

0

我试图通过代码来覆盖一些基本点。请参阅上述方法的注释。

package abstractpkg; 

public class Test { 
    public static void main(String[] args) { 
     C c = new C(); // Compile-time error. C is abstract 
     B b = new B(); // OK: B is concrete 
    } 
} 

abstract class A{ 
    abstract void methA(); 
    public void methB(){ 

    } 
} 

/** 
* Non-abstract class extending asbtract class. 
* */ 
class B extends A{ 
    /* 
    * Since it is Non-abstract class, it must provide impl of abstract method because without impl of 
    * method Object of class can't be created. 
    * */ 
    void methA() { 

    }; 

    /* 
    * This overriding is optional, since it's impl is already existing in super class. 
    * If class B has to give spl impl then B should override this method 
    * */ 
    @Override 
    public void methB() { 
     // TODO Auto-generated method stub 
     super.methB(); 
    } 
} 


abstract class C extends A{ 
    /* 
    * Since C is also abstract it may or may not override the method. 
    * */ 

    /* 
    * Overriding and providing impl is optional. 
    * */ 
    @Override 
    void methA() { 

    } 
} 
1

如果抽象类被另一个抽象类扩展,那么它不需要实现所有的父类方法,但扩展抽象子类的第一个具体类必须实现所有的父抽象类方法。

非抽象类必须实现抽象父类的方法