Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Chapter 9a Abstract Classes & Dynamic Binding

2 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

3 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

4 4 Abstract Class Definition public abstract class Animal { private String name; public String getName( ) { return name; } public abstract void speak( ); }

5 5 Inheriting Abstract Classes public class Dog extends Animal { * public void speak( ) { screen.println(“Woof”); }

6 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( );

7 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

8 8 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

9 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

10 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( );

11 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( );

12 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

13 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( );


Download ppt "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."

Similar presentations


Ads by Google