Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.

Similar presentations


Presentation on theme: "Java Programming Dr. Randy Kaplan. Abstract Classes and Methods."— Presentation transcript:

1 Java Programming Dr. Randy Kaplan

2 Abstract Classes and Methods

3 3 When a class is defined we usually do so with the idea that we will eventually instantiate the class Another kind of class is not meant to be instantiated This kind of class is called an abstract class When a class is defined we usually do so with the idea that we will eventually instantiate the class Another kind of class is not meant to be instantiated This kind of class is called an abstract class

4 4 Abstract Classes and Methods Purpose Provide an appropriate superclass from which other classes can inherit These classes share a common design - for example common attributes Purpose Provide an appropriate superclass from which other classes can inherit These classes share a common design - for example common attributes

5 5 Abstract Classes and Methods Classes that can be used to instantiate objects are called concrete classes Abstract classes are too general to create real objects Abstract classes only specify what is common among subclasses We need to be more specific before we can create objects Classes that can be used to instantiate objects are called concrete classes Abstract classes are too general to create real objects Abstract classes only specify what is common among subclasses We need to be more specific before we can create objects

6 6 Abstract Classes and Methods Abstract classes sometimes constitute several levels of the hierarchy In this hierarchy Shape is an abstract class Abstract classes sometimes constitute several levels of the hierarchy In this hierarchy Shape is an abstract class ShapeShape 2DShape2DShape 3DShape3DShape CircleCircleSquareSquareTriangleTriangleSphereSphereCubeCubeTetrahedronTetrahedron

7 7 Abstract Classes and Methods The classes 2DShape and 3DShape are also abstract classes ShapeShape 2DShape2DShape 3DShape3DShape CircleCircleSquareSquareTriangleTriangleSphereSphereCubeCubeTetrahedronTetrahedron

8 8 Abstract Classes and Methods An abstract class is made abstract by declaring it with the abstract keyword An abstract class will contain one or more abstract methods Example public abstract void draw(); An abstract class is made abstract by declaring it with the abstract keyword An abstract class will contain one or more abstract methods Example public abstract void draw();

9 9 Abstract Classes and Methods An abstract method does not provide an implementation A class that contains any abstract methods must be declared as an abstract class even if that class contains some concrete methods Constructors and static methods cannot be declared abstract An abstract method does not provide an implementation A class that contains any abstract methods must be declared as an abstract class even if that class contains some concrete methods Constructors and static methods cannot be declared abstract

10 10 Abstract Classes and Methods Constructors are not inherited therefore an abstract constructor could never be implemented Static methods are inherited but, they are not associated with particular objects Constructors are not inherited therefore an abstract constructor could never be implemented Static methods are inherited but, they are not associated with particular objects

11 11 Abstract Classes and Methods Abstract methods are meant to be overidden so they can process objects based on their types It would not make sense to declare a static method as abstract Abstract methods are meant to be overidden so they can process objects based on their types It would not make sense to declare a static method as abstract

12 12 Abstract Classes and Methods Objects cannot be instantiated from abstract superclasses Abstract superclasses CAN be used to declare variables that can hold references to objects of any concrete class derived from those abstract superclasses Programs use these variables to manipulate subclass objects polymorphically Objects cannot be instantiated from abstract superclasses Abstract superclasses CAN be used to declare variables that can hold references to objects of any concrete class derived from those abstract superclasses Programs use these variables to manipulate subclass objects polymorphically

13 13 Abstract Classes and Methods Static methods defined in abstract superclasses can also be used as any other static method can be used

14 14 Application of Polymorphism Drawing program needs to display shapes including - circles trianglesd rectangles and those that the programmer adds to the system Drawing program needs to display shapes including - circles trianglesd rectangles and those that the programmer adds to the system

15 15 Application of Polymorphism The drawing program uses Shape variables to manage the objects that are displayed To draw an object - drawing program uses superclass shape variable superclass shape variable contains a reference to subclass object to invoke the objects draw method The drawing program uses Shape variables to manage the objects that are displayed To draw an object - drawing program uses superclass shape variable superclass shape variable contains a reference to subclass object to invoke the objects draw method

16 16 Application of Polymorphism This method - the one that is called on to draw the shape - is declared abstract in the superclass Each concrete subclass MUST implement a method named draw to render the specific shape that is represented by that subclass Each object in the shape inheritance knows how to draw itself This method - the one that is called on to draw the shape - is declared abstract in the superclass Each concrete subclass MUST implement a method named draw to render the specific shape that is represented by that subclass Each object in the shape inheritance knows how to draw itself

17 17 Using Abstract Classes In this example we will define a class inheritance hierarchy for a payroll system Consider the following brief requirements specification In this example we will define a class inheritance hierarchy for a payroll system Consider the following brief requirements specification

18 18 Requirements A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly workers are paid by the hour and receive overtime pay for hours in excess of 40 hours, commission employyes are paid a perrcentage of their sales, and salaried commissioned employees are given a base salary plus a percentage of

19 19 Requirements (cont.) their sales. For the current pay period the company has decided to reward salaried - commission employees by adding 10% to their base salaries. The company wants to implement a Java-based payroll application that implements calculations polymorphically.

20 20 Implementation Abstract class Employee The abstract class employee is used to represent the general concept of an employee Abstract class Employee The abstract class employee is used to represent the general concept of an employee

21 21 Implementation The classes that extend Employee are - SalariedEmployee CommissionEmployee HourlyEmployee BasePlusCommissionEmployee will extend CommissionEmployee The classes that extend Employee are - SalariedEmployee CommissionEmployee HourlyEmployee BasePlusCommissionEmployee will extend CommissionEmployee

22 22 Implementation The Employee class declcares the interface to the hierarchy The interface to the hierarchy consists of the set of methods that a program can invoke on all Employee objects Here the term interface is used in a general sense The Employee class declcares the interface to the hierarchy The interface to the hierarchy consists of the set of methods that a program can invoke on all Employee objects Here the term interface is used in a general sense

23 23 Implementation Elements of the abstract class Employee first name last name social security number Elements of the abstract class Employee first name last name social security number

24 24 Implementation Creating the Abstract Superclass Methods supplied by Class Employee so far include - earnings (abstract because this depends on the pay class of the employee) Each subclass overrides earnings with the appropriate implementation r toString get/set Creating the Abstract Superclass Methods supplied by Class Employee so far include - earnings (abstract because this depends on the pay class of the employee) Each subclass overrides earnings with the appropriate implementation r toString get/set

25 25 Implementation

26 26 Implementation

27 27 Implementation

28 28 Implementation

29 29 Implementation

30 30 Implementation

31 31 Implementation

32 32 Implementation

33 33 Implementation

34 34 Implementation

35 35 Implementation

36 36 Implementation

37 37 Implementation


Download ppt "Java Programming Dr. Randy Kaplan. Abstract Classes and Methods."

Similar presentations


Ads by Google