Inheritance hierarchy and degree of abstraction

  • In the inheritance hierarchy (tree), classes become more specific and concrete with each new subclass.

  • I.e.:

    • If you move from a subclass back up to a superclass, the classes become more general and less specific --- i.e.: more abstract

  • Example:

     

    • You have concrete cats and dogs, but no concrete "animals"

    • You have real examples of circles, but no concrete "geometric objects"

Intro to abstract methods and abstract classes

  • Important class design principle:

    • The superclass contains common features of all its subclasses

  • Sometimes, a common action (= method) is not well defined

    • You know what the action is, but you cannot be specific about it

    Example of an abstract action/method:

    • The computation of the area of a geometric object
      --- there is no formula to compute the area of an arbitrary geometric object

  • Sometimes, a superclass is so abstract that it cannot be used to create any specific instances

    Example of abstract classes:

    • There is no actual animal thing

    • There is no actual geometric object thing

Abstract classes

  • Consider the GeometricObject class:

  • There are no actual GeometricObject things

    (There are only objects that can be categorized as a GeometricObject)

  • Suppose we want to prevent the user from instantiating GeometricObject objects....

    Solution:

    • Define GeometricObject as an abstract class

Abstract classes

  • Abstract class:

    • An abstract class is a class that cannot be instantiated

      I.e.: you cannot create instances of an abstract class

  • Syntax to define an abstract class:

      public abstract class className
      {
         ...
         ... // Same as an normal class
         ...
      }

  • Note:

    • Any class can be defined as asbtract !!

DEMO: 05-interfaces/10-abstract-class/Demo.java     (show compile error)

Why have abstract classes if you can't instantiate with them ???

  • You can:

    • Define (reference) variables of an abstract class type

  • You can also:

    • Extend (derive a subclass from) an abstract class

  • Therefore:

    • An abstract class can serve as the superclass for polymorphic methods !

  • Example: we can use selectionSort() on the GeometricObject date type

                 abstract GeometricObject
                             /  \
                        Circle   Rectangle
    

    Although we cannot create GeomtricObject, we can pass Circle or Rectangle objects as a GeometricObject type !

DEMO: demo/05-interfaces/12-abstract-class/Demo.java + Sort.java

Abtract methods

  • An abstract method consists of:

    • Only the method declaration without the method body

  • The method declaration consists of:

    • Only the method header (data types) information

    Declarations are used to convey data type information to the Java compiler

  • Syntax to define an abstract method:

       public abstract returnType methodName( params );  

    Example:

       public abstract double getArea( );  

Relationship between abstract classes and abstract methods

  • Abstract method:

    • An abstract method is an incomplete method

      I.e.: it cannot be executed (because it has no body !!)

  • Rule:

    • A class that contains an abstract method must be defined as an abstract class

    This is to prevent users from instantiating objects that contains incomplete methods !

  • Subclasses of abstract classes:

    • A subclass that do not define all the abstract methods must be defined as an abstract class

    • Otherwise, the class can be defined as a "normal" class

Example abstract method

  • We can define the GeometricObject class as an abstract class as follows:

    public abstract class GeometricObject
    {
        private String color;
    
        GeometricObject( String col )   // Constructor 2
        {
            color = col;
        }
    
        ...
    
        public abstract double getArea();  // for polymorphism
    }
    

  • Notice that we do not need to use this dummy method anymore:

        public double getArea()  //Dummy method !! (for polymorphism)
        {
            return 0;  // Some default value
        }

DEMO: demo/05-interfaces/11-abstract-method

Quiz

  • Can you have an abstract final class ?

    • Yes

    • No

Quiz

  • Can you have an abstract final class ?

    • Yes

    • No

    No. An abstract class cannot be instantiated, so it needs to be extended (i.e.: not final) into a "concrete" class to become "instantiable" (= useful)

  • Can you have an abstract final method ?

    • Yes

    • No

Quiz

  • Can you have an abstract final class ?

    • Yes

    • No

    No. An abstract class cannot be instantiated, so it needs to be extended (i.e.: not final) into a "concrete" class to become "instantiable" (= useful)

  • Can you have an abstract final method ?

    • Yes

    • No

    No. An abstract method is incomplete (has no body) and must be overridden (i.e.: not final) so the class can become "concrete" (= useful)

    (Remember that a class that contains an abstract method must be defined as an abstract class)

Summary of abstract classes

  • An abstract method cannot be contained in a non-abstract class.

  • An abstract class cannot be instantiated using the new operator

    But:

    • You can still define its constructors, which are invoked in the constructors of its subclasses.

    • You can define (reference) variables using the abstract class

  • A class that contains abstract methods must be abstract.

    However:

    • It is possible to define an abstract class that does not contain any abstract methods

Inheritance and abstract classes

There are a few interesting points with subclasses and inheritance:

  • A subclass can be abstract even if its superclass is non-abstract

    Example:

     Superclass:      Object          is non-abstract
                        ^
                        |
     Subclass:     GeometricObject    is abstract
    

  • An (abstract) subclass can override a non-abstract method from its superclass with an abstract method !

    • This is very unusual

    Possible usage:

    • When the implementation (code) of the method in the superclass becomes invalid in the subclass

Practicl usages of abstract classes

  • Practical usages of abstract classes:

    1. You are unsure of how a method should be defined/implemented for that class:

      Example:

      • getArea( ) for GeometricObject

      Solution:

      • Define the method as abstract
      • Define the class as abstract

    2. You do not want objects of that type being instantiated (and used) --- even when the class has no abstract methods

      Solution:

      • Define the class as abstract