Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering...

15
Unit 02 1 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    234
  • download

    2

Transcript of Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering...

Unit 02 1

Abstract Classes

• What is an Abstract Class?

• Properties of an Abstract Class

• Discovering Abstract Classes

Unit 02 2

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

HourlyEmployee classes.

Unit 02 3

What is an Abstract Class? – Cont’d

1. class Employee{2. private String name;3. private double payRate;4.5. public Employee(String name, double payRate){6. this.name = name;7. this.payRate = payRate;8. }9. public double pay(){10. return payRate;11. }12. public void setPayRate(double newRate){13. payRate = newRate;14. }15. public String toString(){16. return "Name: "+name+"Pay Rate: "+payRate;17. }18. }

• The payRate field represents the monthly pay for the employee. Notice that the pay method simply returns the value of this field.

Unit 02 4

What is an Abstract Class? – Cont’d

1. class HourlyEmployee extends Employee{2. private int hours;3.4. public HourlyEmployee(String name, double rate){5. super(name, rate);6. hours = 0;7. }8. public void assHours(int moreHours){9. hours += moreHours;10. }11. public String toString(){12. return super.toString()+"Hours: "+hours;13. }14. public double pay(){15. return super.pay() * hours;16. }17. }

The payRate 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 5

What is an Abstract Class? – Cont’d

• Clearly, if the pay method is not overridden, by mistake, the pay of the HourlyEmployee 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 payRate;4.5. public Employee(String name, double payRate){6. this.name = name;7. this.payRate = payRate;8. }

9. public abstract double pay();

10. public void setPayRate(double newRate){11. payRate = newRate;12. }13. //...14. }

Unit 02 6

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 7

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 MonthlyEmployee extends Employee{2. public MonthlyEmployee(String empNamem double empRate){3. super(empName, empRate);4. }5. public double pay(){6. return payRate;7. }8. }

Unit 02 8

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 9

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 10

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

public abstract void doIt(int count);

Unit 02 11

Discovering Abstract Classes

• Suppose we wish to write a program that manipulates two-dimensional 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 12

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 13

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 getClass().getName();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. }19. }

Unit 02 14

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. }5. }

Unit 02 15

Exercises

1. Write a class, TestEmpolyee, that has the following methods:a. printPay(Employee emp) : that prints the name and salary of emp.b. a main method that creates an instance of MonthlyEmployee and that of HourlyEmployee, and prints them using printPay method.

2. Write a class, Manager, that extends MonthlyEmployee, and has another instance variables, bonus and an instance method, setBonus. Override the pay method and update TestEmployee accordinly.

3. Write a class, TestShapes, that creates an instance of each of our shape classes and prints it.

4. Suppose you wish to write an application that stores information about reading materials in a BookShop. 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.