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
Abstract classes
-
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 ???
DEMO:
demo/05-interfaces/12-abstract-class/Demo.java
+ Sort.java
Relationship between
abstract classes and
abstract methods
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 ?
|
Quiz
- Can you have an
abstract
final
class ?
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 ?
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 !
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:
- 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
|
- 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
|
|
|
❮
❯