Abstract Classes What is an Abstract Class Properties
Abstract Classes • What is an Abstract Class? • Properties of an Abstract Class • Discovering Abstract Classes Unit 02 1
What is an Abstract Class? • A subclass (programmer) normally has the option of whether to override the methods of a superclass or not. • However, there are situations where the superclass (programmer) would like to force the subclass (programmer) to override a certain method. • This is usually the case if there is no suitable default implementation. • For example, consider following examples of Employee and Hourly. Employee classes. Unit 02 2
What is an Abstract Class? – Cont’d 1. class Employee{ 2. private String name; 3. private double pay. Rate; 4. 5. public Employee(String name, double pay. Rate){ 6. this. name = name; 7. this. pay. Rate = pay. Rate; 8. } 9. public double pay(){ 10. return pay. Rate; 11. } 12. public void set. Pay. Rate(double new. Rate){ 13. pay. Rate = new. Rate; 14. } 15. public String to. String(){ 16. return "Name: "+name+"Pay Rate: "+pay. Rate; 17. } 18. } • The pay. Rate field represents the monthly pay for the employee. Notice that the pay method simply returns the value of this field. Unit 02 3
What is an Abstract Class? – Cont’d 1. class Hourly. Employee extends Employee{ 2. private int hours; 3. 4. public Hourly. Employee(String name, double rate){ 5. super(name, rate); 6. hours = 0; 7. } 8. public void ass. Hours(int more. Hours){ 9. hours += more. Hours; 10. } 11. public String to. String(){ 12. return super. to. String()+"Hours: "+hours; 13. } 14. public double pay(){ 15. return super. pay() * hours; 16. } 17. } The pay. Rate field represents the hourly pay for the employee. Here the pay method multiplies this value with the hours worked and returns the result. Unit 02 4
What is an Abstract Class? – Cont’d • • • Clearly, if the pay method is not overridden, by mistake, the pay of the Hourly. Employee will be computed wrongly. To force the subclass programmer to override this method, the method should be declared as abstract. An abstract method consists of only the method heading. 1. abstract class Employee{ 2. private String name; 3. private double pay. Rate; 4. 5. public Employee(String name, double pay. Rate){ 6. this. name = name; 7. this. pay. Rate = pay. Rate; 8. } 9. public abstract double pay(); 10. 11. 12. 13. 14. } public void set. Pay. Rate(double new. Rate){ pay. Rate = new. Rate; } //. . . Unit 02 5
What is an Abstract Class? – Cont’d • Now, once a class has at least one abstract method, then the class is said to be abstract and it must be declared as such in its heading. • Thus, an abstract class essentially means a class that is "not completely" defined. • The term, concrete class, is sometimes used to describe a class that is not abstract. • Note: it is not necessary for an abstract class to have abstract methods. • Even if all the methods in a class are implemented, the class will still be abstract if it is declared as such in its heading. Unit 02 6
Properties of an Abstract Class • An abstract class cannot be instantiated - that is, it cannot be used to create objects. • For example, we cannot create an object of the Employee class. • Thus, to represent an employee whose pay is per month, we need to sub-class the Employee abstract class as follows: 1. class Monthly. Employee extends Employee{ 2. public Monthly. Employee(String emp. Namem double emp. Rate){ 3. super(emp. Name, emp. Rate); 4. } 5. public double pay(){ 6. return pay. Rate; 7. } 8. } Unit 02 7
Properties of an Abstract Class – Cont’d • The relationship between the three classes is shown by the following: • Although an abstract class cannot be used to create objects, it can be used to define reference variables. • Similar to what we saw in inheritance, this variable can be assigned an object created using a concrete sub-class of Employee. • An abstract class may contain a constructor but it is invoked only via the super keyword in the constructor of a concrete subclass constructor Unit 02 8
Properties of an Abstract Class – Cont’d • In summary, an abstract class is just like a normal class except that it cannot be instantiated and is mainly used to factor out instance variables and methods that all classes in a class-hierarchy should have. Unit 02 9
Properties of Abstract Methods • An abstract method is like a placeholder for a method that will be fully defined in a descendent class • It has a complete method heading, to which has been added the modifier abstract • It cannot be private • It has no method body, and ends with a semicolon in place of its body public abstract double get. Pay(); public abstract void do. It(int count); Unit 02 10
Discovering Abstract Classes • Suppose we wish to write a program that manipulates twodimensional geometrical shapes, namely: – Squares – Rectangles and – Circles • Suppose further that we want each such shape to tell its name, its area and its perimeter. • Since each of these shapes is a type of shape, it makes sense to have a superclass, Shape, from which the others can be derived. • However, since the computation of area and perimeter for each of these shapes is different, the subclasses should be forced to override these methods. Thus the Shape class must be abstract. Unit 02 11
Discovering Abstract Classes – Cont’d • Moreover, since square is a special type of rectangle, we can derive it from the Rectangle class. • Thus, we have a two level hierarchy tree shown as follows: Unit 02 12
Discovering Abstract Classes – Cont’d • The code for these classes is shown as follows: 1. public abstract class Shape{ 2. public String name(){ 3. return get. Class(). get. Name(); 4. } 5. public abstract double area(); 6. public abstract double perimeter(); 7. } 8. public class Circle extends Shape{ 9. public double radius; 10. public Circle(double r){ 11. radius = r; 12. } 13. public double area(){ 14. return Math. PI * (radius * radius); 15. } 16. public double perimeter(){ 17. return (2. 0 * Math. PI )*radius; 18. } Unit 02 19. } 13
Discovering Abstract Classes – Cont’d 1. public class Rectangle extends Shape{ 2. private double length; 3. private double width; 4. 5. public Rectangle(double length, double width}( 6. this. length = length; 7. this. width = width; 8. { 9. public double area}() 10. return length * width; 11. { 12. public double perimeter(){ 13. return 2 * (length + width ); 14. } 15. } 1. public class Square extends Rectangle{ 2. public Square(double length}( 3. super(length, length; ( 4. { Unit 02 5. { 14
Exercises 1. 2. 3. 4. Write a class, Test. Empolyee, that has the following methods: a. print. Pay(Employee emp) : that prints the name and salary of emp. b. a main method that creates an instance of Monthly. Employee and that of Hourly. Employee, and prints them using print. Pay method. Write a class, Manager, that extends Monthly. Employee, and has another instance variables, bonus and an instance method, set. Bonus. Override the pay method and update Test. Employee accordinly. Write a class, Test. Shapes, that creates an instance of each of our shape classes and prints it. Suppose you wish to write an application that stores information about reading materials in a Book. Shop. Reading materials could be books, journals, magazines and newspapers. Identify the classes (including abstract classes, if any) that are needed for this application. Draw a class diagram for the classes and implement them. Unit 02 15
- Slides: 15