Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes."— Presentation transcript:

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

2 Unit 022 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.

3 Unit 023 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.

4 Unit 024 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.

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

6 Unit 026 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.

7 Unit 027 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.}

8 Unit 028 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

9 Unit 029 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.

10 Unit 0210 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);

11 Unit 0211 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.

12 Unit 0212 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:

13 Unit 0213 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.}

14 Unit 0214 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.}

15 Unit 0215 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.


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

Similar presentations


Ads by Google