Why is a default abstract method allowed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajayrnair
    New Member
    • May 2008
    • 1

    Why is a default abstract method allowed?

    Hi All,
    Consider the following code snippet

    package p1;

    public abstract class test {
    abstract void chuckde();
    public abstract void onceMore();

    protected abstract void tettt();

    }

    2nd class:

    package p2;

    import p1.test;

    public class Test2 extends test{
    @Override
    public void onceMore() {
    // TODO Auto-generated method stub

    }
    }


    the above code will not compile for a very obvious reason: Test2 MUST implement the abstract method chuckde from test, but it cant since the method has a default access and is not visible to Test2 which is in a different package.
    I am forced to make the class abtract. If that is the case, i would never have a concrete class in a different package if it inherits test. Why is this allowed? Why doesnt the compiler crib like it does when i create a private abstract method?

    Thanks,
    --Ajay
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    In your particular case the compiler can only protest for the extending class
    because it must be either abstract or implement a method that isn't visible
    in the second class.

    A method can't be defined abstract and private. Nor can a class be defined
    abstract and final. Because that won't make any sense; in your example one
    can define a concrete extending class in the same package as the abstract
    base class.

    kind regards,

    Jos

    Comment

    Working...