Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Classes. Review PA – 3 Design Considerations https://users.cs.jmu.edu/bernstdh /Web/CS239/programs/pa3/draft.php ?harrisnlCS239 https://users.cs.jmu.edu/bernstdh.

Similar presentations


Presentation on theme: "Abstract Classes. Review PA – 3 Design Considerations https://users.cs.jmu.edu/bernstdh /Web/CS239/programs/pa3/draft.php ?harrisnlCS239 https://users.cs.jmu.edu/bernstdh."— Presentation transcript:

1 Abstract Classes

2 Review PA – 3 Design Considerations https://users.cs.jmu.edu/bernstdh /Web/CS239/programs/pa3/draft.php ?harrisnlCS239 https://users.cs.jmu.edu/bernstdh /Web/CS239/programs/pa3/draft.php ?harrisnlCS239

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

4 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 (unless it itself is abstract).

5 Abstract Classes generally contain at least one abstract method are any classes containing at least one abstract method can contain non-abstract methods If there is an abstract method, then the class must be declared as abstract, but… If a class is declared as abstract it may or may not have non-abstract or abstract methods.

6 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 concrete children are generally methods whose bodies will change from one subclass to another

7 Standard UML Notation +public -private #protected {abstract} (name is italicized) top compartment – class name middle compartment – class attributes  name : type bottom compartment – class methods  name (parameterList types) : return type

8 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

9 Let’s look again at the StaffMember class What will happen if:  We remove the word abstract in the class header?  We change the abstract method to a non-abstract method?  We try to instantiate a StaffMember object?  We decide not to “pay” Volunteers?  We create a new class from the StaffMember object which is an abstract class also?

10 Another example Shapes - DesigningClasses.pdfDesigningClasses.pdf What should the hierarchy look like? Which classes should be abstract? Which concrete? What services should be required (made abstract)?

11 Finished product Shape.java TwoDimensionalShape.java ThreeDimensionalShape.java Sphere.java Rectangle.java Rectangle.java


Download ppt "Abstract Classes. Review PA – 3 Design Considerations https://users.cs.jmu.edu/bernstdh /Web/CS239/programs/pa3/draft.php ?harrisnlCS239 https://users.cs.jmu.edu/bernstdh."

Similar presentations


Ads by Google