Review of inheritance, polymorphism, and interfaces

  1. In Java inheritance, the subclass is derived from only one existing class, called superclass.
  2. The private members of a superclass are private to subclass. The subclass cannot directly access them.
  3. A subclass can override the methods of a superclass. However, this overriding applies only to the objects of the subclass.
  4. No final or static methods can be overridden.
  5. If a class is declared as final, it cannot be extended by a subclass.
  6. If the subclass overrides a public method of a superclass, one can call the corresponding superclass method by using the reserved word super followed by the dot operator followed by the method name with an appropriate parameter list.
  7. While designing a superclass constructor, a call to the superclass constructor is specified by using the reserved word super followed by the parameters list. Moreover, the call of the superclass constructor must be the first statement.
  8. For a superclass to give a member access to its subclass and prevent its direct access outside the class, you must give the protected access to the member.
  9. If you define a class and do not use the reserved word extends to derive it from an existing class, then your class is automatically considered to be derived from class Object. The class Object directly or indirectly becomes the superclass of every class in Java.
  10. Object variables are polymorphic. Java allows to treat an object of a subclass as an object of a superclass. That is, a reference variable of a superclass type can point to an object of subclass type.
  11. You cannot automatically make a reference variable of a subclass type point to an object of superclass type. Use casting (with caution!).
  12. An abstract method is a method that has only the heading but no body. The heading of an abstract method is terminated with a semicolon.
  13. An abstract class is a class declared with the reserved word abstract
  14. An interface is a class that contains only abstract methods and/or named constants.
  15. You do not instantiate interfaces, one implements them instead. To implement an interface, you must provide implementations to all its methods.
  16. Java allows a class to implement several interfaces.
  17. Use the Java interfaces to share common behavior. Use the inheritance to share common code.