开发者

Error in inheritance

I have an interface BaseModel

public interface BaseModel
{
    String toString();
}

and an abstract class BaseData

public abstract class BaseData
{
    public abstract String toString();
}

I want my model classes to override the toString() method. For this I tried the following ways, one at a time, but not together:

  1. Make all my model classes implement the interface BaseModel

  2. Make all my model classes implement the abstract class BaseData

In the first case I do not get a compilation error if I do not override the toString() method in my model class. I guess it it considering the Object.toString() method as a valid implementation.

But in my second case, if I do not override the toString() method, I get a compilation error:

The type Title must implement the inherited abstract method BaseData.toString()

where Title is one of my model class that extends Bas开发者_C百科eData but does not override toString().

Why is the discrepancy?

Thanks in advance.


When you call the toString from your interface, JVM will search for a toString method in your Titleclass and all its extended classess. eg. Keep searching till it reaches the end of the tree = Object class.

                       Object.class - found toString
                           |
Interface.class   ---  Title.class - can't find toString

In java you can only extend 1 class. So your abstract class extends Object.class and overrides the toString method. Sincs Title extends your abstract class, it cannot extend the Object.class by itself.

Object.class
     |
Abstract.class - overrides toString
     |
Title.class - has to override toString as it is abstract


If you specify a method in an interface, you will need to implement it in any class that inherits from the interface. (However I am unsure about why you do not get a compile error here - this may be as you said because it is using Object.toString() but don't quote me on it)

For abstract classes, anything marked abstract must be implemented on classes that inherit from it.

The exception to this rule is if you have abstract class A which has a subclass B (which is also abstract), and B has a subclass C. B doesn't need to implement the toString method as long as C implements it.


Because your inherited class is concrete. In this case it must implement all abstrct methods declared in interface that it implements and abstract classes that it extends.

so, just implement this method in your class or mark it as abstract if you mean that it should be abstract.


In case 1 you define a new interface (with toString()) and then implement it. As you deduced because your objects extend Object and Object defines toString() also, any Object subclass will conform to your interface.

In case 2 you define an abstract class with an abstract toString() method that hides the superclass (Object) method. In this case, your subclasses of BaseData need to define an implementation of the new abstract toString() method.


Every object has toString() method so if you implement BaseModel, it's already fulfill the interface contract. But the BaseData you specifically make the method abstract so every subclass must reimplement it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜