1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been...

13
1 Chapter 9a Abstract Classes & Dynamic Binding

Transcript of 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been...

Page 1: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

1

Chapter 9a

Abstract Classes & Dynamic Binding

Page 2: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

2

Abstract Classes

• All classes so far have been concrete classes– Classes that can be used to create object

• We can also create abstract classes– Classes that can not be used to create objects– Abstract classes can be inherited as the basis for

other classes

• The purpose of an abstract class is to be inherited by another class

Page 3: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

3

Abstract Classes

• An abstract class is a class that contains at least one abstract method

• An abstract method– Defined using the keyword abstract– A method that has no method statements

• The class that inherits the abstract class must override the abstract methods

Page 4: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

4

Abstract Class Definitionpublic abstract class Animal

{

private String name;

public String getName( )

{

return name;

}

public abstract void speak( );

}

Page 5: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

5

Inheriting Abstract Classes

public class Dog extends Animal

{

*

*

public void speak( )

{

screen.println(“Woof”);

}

}

Page 6: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

6

Declaring Objects

• Declaring an object of an abstract class is illegal

Animal someAnimal = new Animal( );

• Declaring an object of a class that inherits an abstract class

Dog myLab = new Dog(“Belle”);

myLab.speak( );

Page 7: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

7

Abstract Classes

• Why?– Abstract classes allow for the definition of class

methods without having to define the method’s code

• Each class that inherits the abstract class defines its version of the code for each abstract method

Page 8: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

8

Binding

• Binding– refers to the point in time when all the information

needed to call a function is known

• Two types of binding– Early (Compile time)– Late (Run time)

• Dynamic binding is another name for late binding

Page 9: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

9

Dynamic Binding

• A subclass object is also considered to be an object of the superclass– Every Dog object is also an Animal object

• While we can not create an object of an abstract class we can create a reference of the abstract class

• As a result a reference of an abstract class type can “point” to any object of a class that inherited the abstract class

Page 10: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

10

Abstract Class References

public class Cow extends Animal

public class Dog extends Animal

Animal ref; // reference

Cow aCow = new Cow(“Bossie”);

Dog aDog = new Dog(“Sophie”);

ref = aCow;

ref.speak( );

ref = aDog;

ref.speak( );

Page 11: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

11

Dynamic Binding

• Two different methods named speak( ) are executed

• Which version is executed is determined at run-time based on the reference address

ref = aCow;

ref.speak( );

ref = aDog;

ref.speak( );

Page 12: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

12

Significance

• A abstract class reference allows you to act upon a diverse group of similar class objects

• No objects of type Animal but a reference of type Animal allows you to access object of type Cow and Dog

Page 13: 1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

13

Arrays of Subclass Objects

public class Cow inherits Animal

public class Dog inherits Animal

Animal[ ] ref = new Animal[2]; // reference array

Cow aCow = new Cow(“Bossie”);

Dog aDog = new Dog(“Sophie”);

ref [0] = aCow;

ref [1] = aDog;

for ( i = 0; i < 2; i++)

ref [i].speak( );