Abstract Classes - PowerPoint PPT Presentation

Loading...

PPT – Abstract Classes PowerPoint presentation | free to view - id: 63330-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Abstract Classes

Description:

The Cat and Duck classes need to be modified in a similar way. ... new Cat('Max', 'Abyssinian'), new Duck('Daffy','Aylesbury'), new Spaniel('Fido' ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 38
Provided by: LindaM104
Category:
Tags: abstract | classes

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Abstract Classes


1
Abstract Classes Interfaces
  • CSC 313 Course Notes Ch. 6 continued
  • By L. M. Hicks

2
Abstract 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.

3
Abstract 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.

4
Abstract 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.

5
The 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

6
Abstract 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.

7
Abstract 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)
13
TryAbstract Sample Output
14
Abstract Animal Inheritance
15
(No Transcript)
16
(No Transcript)
17
Abstract 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.

18
Abstract 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.

19
Abstract 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.

20
Abstract 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.

21
Interfaces
  • Support Polymorphism

22
Interfaces
  • 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.

23
Interface constants methods
24
An 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.

25
public 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
26
Abstract 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
27
Animal 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")
  •   

28
Code 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

29
An Animal Interface
30
Using 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.

31
Using 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.

32
Using 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.

33
Heterogeneous Arrays
  • Interfaces make it possible to build data
    structures where members come from non-related
    classes.

34
Interface 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.

35
Interface 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.

36
Interface 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.

37
Thats All For Today Folks!
About PowerShow.com