1

I am having an interface called I. This interface is implemented by an abstract class AClazz.

AClazz implements I

Now this abstract class is extended by several concrete classes.

ConcreteClazz extends AClazz
AnotherConcreteClazz extends AClazz

Now when I add a new method contract to the interface I why does eclipse and hence Java language complain about a missing implementation inside the concrete classes (ConcreteClazz and AnotherConcreteClazz) and not inside the abstract class AClazz?

If AClazz was a concrete class (& not abstract) and the concrete classes extended from the non abstract AClazz will Java complain about missing implementation inside concrete classes again?

3 Answers 3

1

So say your interface I has a method m:

public interface I {
     public void m (int someParameter);
}

Now AClazz implements I, and ConcreteClazz extends AClazz. That means that ConcreteClazz also implements I, whether or not you say so explicitly in the class declaration of ConcreteClazz. That means you can use a ConcreteClazz object anywhere an I is expected. So say somewhere, in some other class, you have a method

void takeSomeAction(Something x, I y) {
    ....
    y.m (x.getSomeProperty());  // call the interface method
    ....
}

You can call it with a ConcreteClazz object:

ConcreteClazz z = new ConcreteClazz(...);
Something st;

foo.takeSomeAction(st, z);

You're giving takeSomeAction a ConcreteClazz to use as its I parameter. Then takeSomeAction will call the m method. How can this work unless you've provided an implementation for m, somewhere?

That's why you need to implement m, either in AClazz or ConcreteClazz. You could implement it in AClazz, if that's the right thing to do, and then you don't need to provide another implementation in ConcreteClazz. But if you don't implement it in AClazz, it won't cause any errors, since, at runtime, you can't actually have an object of type AClazz. But since you can have objects of type ConcreteClazz, that means that somewhere, for ConcreteClazz, there has to be an implementation of m.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep thanks I got that part. Now I am curious to know where the error would be thrown had AClazz been a concrete class instead of an abstact class. Will Java complain of missing implementation inside AClazz or inside ConcreteClazz?
@AnupSaumithri In AClazz. If AClazz is were a concrete class, it would have to implement all the methods in I, or else the compiler will dipslay an error. If ConcreteClazz then extends AClazz, I'm not sure if the compiler could even start compiling ConcreteClazz if AClazz didn't compile.
1

An Abstract class means, this class contains abstract methods (i.e. no implementation, only definition of method), the classes that are extending it must implement the unimplemented methods.

Since an Interface contains 100% pure public abstract methods, which must be implemented by the classes that are implementing them unless it's marked as abstract. Again follow the above rule.

2 Comments

Great thank you. Its a pretty novice question, looks like Im getting out of form.
Backticks are for inline code blocks; they shouldn't be used for emphasis. It would be better to use the asterisk (italics), or double asterisks (bold) instead.
0

An abstract class remains abstract no matter how many new abstract methods you add to it. So compiler does not complain when you add a new method in your I interface which is implemented by your abstract class.

A concrete class cannot have any abstract methods, as your subclasses are extending your abstract class and that class does not implement the newly added method to the interface, hence the compiler complains. Because newly added abstract method is inherited in all your child classes and a concrete class cannot have an abstract method.

4 Comments

So what if that abstract class were to be a concrete class. Is the implementation required inside the last subclasses or the immediate implementer?
@AnupSaumithri If you add the implementation for new interface method in your parent abstract class then nothing needed to be done in your child classes.
I want to know what if that parent abstract class were to be a concrete class. Where would the java complain about the missing implementation? Inside the parent concrete class or the children concrete class? Thanks'
Parent class is abstract so no complain, concrete subclasses will not be compiled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.