Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14 - Abstract Classes

Similar presentations


Presentation on theme: "Lecture 14 - Abstract Classes"— Presentation transcript:

1 Lecture 14 - Abstract Classes
Professor Adams

2 Abstract classes “When you extend an existing class, you have the choice of whether or not to redefine the method(s) of the super class…” When you want to force subclasses to redefine the method(s), make them abstract methods in the superclass. Lewis & Loftus p. 449

3 Abstract classes “A class that defines an abstract method, or that inherits an abstract method without overriding it, must be declared as abstract. You can also declare classes with no abstract methods as abstract. This prevents programmers from creating instances of that class but allows them to create their own subclasses.”

4 Abstract classes You can not construct an object of an abstract class but you can have an object reference whose type is an abstract class Example from text: BankAccount an Account; anAccount = new BankAccount(); class BankAccount in text on page 448 is not abstract but an abstract method was added to it on page 449 making it abstract anAccount = new SavingsAccount(); class SavingsAccount extended BankAccount anAccount = null;

5 Abstract classes Can have instance fields
Can have some concrete methods

6 Rules for Abstract Classes
An abstract class can not be instantiated An abstract class can’t have instances A class that can have have instances is said to be concrete An abstract class provides a prototype for other classes to follow

7 Subclasses of an Abstract Class
will inherit the variables and methods of the abstract class will have the same basic characteristics are free to redefine variables and methods and add new ones must override any abstract methods of its parent.

8 Abstract Classes generally contain at least one abstract method
are not required by the compiler to be declared abstract – but we require it are any classes containing at least one abstract method can contain non-abstract methods

9 Abstract Methods have the word abstract in their declaration
do not have a body end their declarations with a semi-colon must be overridden in children of containing class are generally methods whose bodies will change from one subclass to another

10 Standard UML Notation + public - private # protected {abstract}
top compartment – class name middle compartment – class attributes name : type bottom compartment – class methods returnType name (parameterList types)

11 Some Lewis & Loftus slides
Abstract Classes Some Lewis & Loftus slides

12 Abstract Classes An abstract class is a placeholder in a class hierarchy that represents a generic concept An abstract class cannot be instantiated We use the modifier abstract on the class header to declare a class as abstract: public abstract class Whatever { // contents }

13 Abstract Classes An abstract class often contains abstract methods with no definitions the abstract modifier must be applied to each abstract method An abstract class typically contains non-abstract methods (with bodies) A class declared as abstract does not need to contain abstract methods

14 Abstract Classes The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract An abstract method cannot be defined as final (because it must be overridden) or static (because it has no definition yet) The use of abstract classes is a design decision – it helps us establish common elements in a class that is too general to instantiate

15 Indirect Use of Members
An inherited member can be referenced directly by name in the child class, as if it were declared in the child class But even if a method or variable is not inherited by a child, it can still be accessed indirectly through parent methods See FoodAnalysis.java (page 403) See FoodItem.java (page 404) See Pizza.java (page 405)


Download ppt "Lecture 14 - Abstract Classes"

Similar presentations


Ads by Google