Title: Abstract Classes
1Abstract Classes Interfaces
- CSC 313 Course Notes Ch. 6 continued
- By L. M. Hicks
2Abstract Classes
- In the class Animal, we had to introduce a
version of the method sound() that did nothing,
just because we wanted to call the sound() method
in the sub-class objects dynamically. - The method sound() has no meaning in the context
of the generic class Animal, so implementing it
does not make much sense.
3Abstract Classes
- To handle this situation java provides abstract
classes. - An abstract class is a class in which one or more
methods are declared, but not defined. - The bodies of these methods are omitted, because,
as in the case of the method sound() in our class
Animal, implementing the methods does not make
sense.
4Abstract Classes
- To define an abstract class you use the keyword
abstract in front of the class name. - The declaration for an abstract method ends with
a semi-colon, and you specify the method with the
keyword abstract to identify it as such.
5The class Animal as an abstract class
- public abstract class Animal
- private String type
- public Animal(String aType)
- type new String(aType)
-
- public void show()
- System.out.println("This is a "type)
-
- public abstract void sound()
- // end class Animal
6Abstract Classes
- It doesn't matter whether you prefix the class
name with public abstract or abstract public,
they are equivalent. - The same goes for the declaration of an abstract
method, but both public and abstract must precede
the return type specification.
7Abstract Classes
- An abstract method cannot be private since a
private method cannot be inherited, and therefore
cannot be redefined in a sub-class. - You cannot instantiate an object of an abstract
class, but you can declare a variable for it.
8(No Transcript)
9(No Transcript)
10(No Transcript)
11(No Transcript)
12(No Transcript)
13TryAbstract Sample Output
14Abstract Animal Inheritance
15(No Transcript)
16(No Transcript)
17Abstract Class Method Summary
- An abstract class in Java is essentially an
incomplete class. - You can specify 0 or more method interfaces that
do not include code. - An abstract class is a class that contains one or
more abstract methods. It must be defined with
the attribute abstract.
18Abstract Class Method Summary
- A sub-class of an abstract class must also be
declared as abstract if it does not provide
definitions for all of the abstract methods
inherited from its super-class. - An abstract class cannot be instantiated but can
(and must) be extended.
19Abstract Class Method Summary
- If the extension implements the codeless methods
of the abstract class, then it can be
instantiated. - An example of an abstract class is the Animal
class --- you don't instantiate an Animal in
general, but you do instantiate something
concrete that derives from the Animal class, such
as a Dog.
20Abstract Class Method Summary
- Another more natural example is the taxonomy of
the animal kingdom --- there are no instances of
the class mammalia but people are instances of
the human species, which is derived from the
mammals class. - Although abstract classes cannot be instantiated,
they can be referred to in type. For example, you
can specify a parameter of type Animal in a
method call.
21Interfaces
22Interfaces
- An interface is essentially a collection of
constants and abstract methods that you can
implement in a class. - The methods in an interface are always public and
abstract, so you do not need to specify them as
such. - Constants in an interface are always public,
static and final, so you do not need to specify
the attributes for these either. - An interface can contain just constants, just
abstract methods, or both.
23Interface constants methods
24An Animal Interface PetOutput
- public interface PetOutput
-
- void sound()
- void show()
-
- Now all we need to do is make sure each of the
classes implement this interface.
25public class Dog implements PetOutput private St
ring name // Name of a dog private String bree
d // Dog breed public Dog(String aName)
name aName // Supplied name
breed "Unknown" // Default breed value //
Make a Duck a Dog public Dog (Duck aDuck)
name aDuck.getName() breed aDuck.getBreed()
public Dog (String aName, String aBreed) nam
e aName // Supplied name breed aBre
ed // Supplied breed
26Abstract Dog class continued // Show a dog's det
ails public void show() System.out.println("It
's "name" the "breed) // A barking method pu
blic void sound() System.out.println("Woof
Woof") public String getName()
return name
27Animal Interface Example cont.
- The Cat and Duck classes need to be modified in a
similar way. - The definition of the class, Spaniel, will
remain - class Spaniel extends Dog
-
- public Spaniel(String aName)
-
- super(aName, "Spaniel")
-
-
28Code To Test The Classes
- public class TestInterface
- final int aSize 4
- public static void main(String args)
- PetOutput thePets new Dog("Rover", "Poodle"
), - new Cat("Max", "Abyssinia
n"), - new Duck("Daffy","Aylesbu
ry"), - new Spaniel("Fido")
- PetOutput petChoice
- for(int i 0 i lt aSize i)
- petChoice theAnimalsi
- System.out.println("\nYou have chosen")
- petChoice.show()
- petChoice.sound()
-
- // end main
- // end class
29An Animal Interface
30Using Multiple Interfaces
- A derived class can have only a single base class
in Java. Java does not support multiple
inheritance as does C. -
- Java does not restricted in the number of
interfaces a class can implement.
31Using Multiple Interfaces
- A simple use for implementing multiple interfaces
in a class is to use one interface to hold
application constants, - ..and another to declare methods that you want to
use polymorphically.
32Using Multiple Interfaces
- You can put all the constant values that you need
in a program into a single interface definition,
and then implement the interface in any class
that needs access to the constants. - This only involves adding implements and the
interface name to the first line of the class
definition.
33Heterogeneous Arrays
- Interfaces make it possible to build data
structures where members come from non-related
classes.
34Interface Summary
- An interface can only contain abstract methods.
- All variables in an Interface are static and
final, i.e. constants. - You can use Interface names like types.
- Define variables to hold object references to any
instance implementing the Interface methods.
35Interface Summary
- A class that does not define all the methods for
an interface it implements, must be declared as
abstract. - If several classes implement a common interface,
methods declared as members of the interface can
be executed polymorphically.
36Interface Summary
- A class can implement one or more interfaces by
declaring them in the class definition, and
including the code to implement each of the
interface methods.
37Thats All For Today Folks!