简体   繁体   中英

Abstract method in abstract Derived class overrides/implements abstract or concrete method of abstract Base class. How and why?

Came across interesting inheritance / overriding issue (taken from here ).

When both Base and Derived classes are abstract, and

  1. both have exactly same abstract method, or
  2. abstract Base has concrete method, and Derived declares SAME method abstract.

I don't understand why Eclipse IDE calls it in Derived

  1. implements (it gives NO CODE / BODY in Derived!)
  2. overrides (also gives NO CODE / BODY in Derived!)

Snippet 1:

public abstract class Girl {
     abstract String getDescription();
}


abstract class GirlDecorator extends Girl {
       abstract String getDescription();  // implements Girl.getDescription() - says Eclipse IDE
}

Snippet 2:

public abstract class Girl {
    String description = "no particular";

      String getDescription() {
          return description;
      }
}

abstract class GirlDecorator extends Girl {
       abstract String getDescription();  // overrides Girl.getDescription() - says Eclipse IDE
}

In Java there are two types of methods (non-static)

  1. Virtual - Normal method, must be implemented in the base class but can be overridden by inheritance.

  2. Abstract - Method that is not implemented in the base class but in the derived class.

So that wraps it, virtual methods are not implemented in the derived class, they are implemented in the base class and can be overridden by the derived class. Abstract methods are not implemented in the base class, they are implemented in the derived class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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