150 likes | 155 Views
Learn what abstract classes are, their key properties, and how they are used in programming. Explore examples like Employee and HourlyEmployee classes and understand the importance of abstract methods in Java OOP. An abstract class cannot be instantiated directly and serves as a blueprint for other classes to inherit from.
E N D
Abstract Classes • What is an Abstract Class? • Properties of an Abstract Class • Discovering Abstract Classes Unit 02
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
What is an Abstract Class? – Cont’d • class Employee{ • private String name; • private double payRate; • public Employee(String name, double payRate){ • this.name = name; • this.payRate = payRate; • } • public double pay(){ • return payRate; • } • public void setPayRate(double newRate){ • payRate = newRate; • } • public String toString(){ • return "Name: "+name+"Pay Rate: "+payRate; • } • } • The payRatefield represents the monthly pay for the employee. Notice that the paymethod simply returns the value of this field. Unit 02
What is an Abstract Class? – Cont’d • class HourlyEmployee extends Employee{ • private int hours; • public HourlyEmployee(String name, double rate){ • super(name, rate); • hours = 0; • } • public void assHours(int moreHours){ • hours += moreHours; • } • public String toString(){ • return super.toString()+"Hours: "+hours; • } • public double pay(){ • return super.pay() * hours; • } • } The payRatefield represents the hourly pay for the employee. Here the pay method multiplies this value with the hours worked and returns the result. Unit 02
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. • abstract class Employee{ • private String name; • private double payRate; • public Employee(String name, double payRate){ • this.name = name; • this.payRate = payRate; • } • public abstract double pay(); • public void setPayRate(double newRate){ • payRate = newRate; • } • //... • } Unit 02
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
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: • class MonthlyEmployee extends Employee{ • public MonthlyEmployee(String empNamem double empRate){ • super(empName, empRate); • } • public double pay(){ • return payRate; • } • } Unit 02
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
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
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
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
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
Discovering Abstract Classes – Cont’d • The code for these classes is shown as follows: • public abstract class Shape{ • public String name(){ • return getClass().getName(); • } • public abstract double area(); • public abstract double perimeter(); • } • public class Circleextends Shape{ • public double radius; • public Circle(double r){ • radius = r; • } • public double area(){ • return Math.PI * (radius * radius); • } • public double perimeter(){ • return (2.0 * Math.PI )*radius; • } • } Unit 02
Discovering Abstract Classes – Cont’d • public class Rectangle extends Shape{ • private double length; • private double width; • public Rectangle(double length, double width){ • this.length = length; • this.width = width; • } • public double area(){ • return length * width; • } • public double perimeter(){ • return 2 * (length + width ); • } • } • public class SquareextendsRectangle{ • public Square(double length){ • super(length, length); • } • } Unit 02
Exercises • 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. • Write a class, TestShapes, 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 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. Unit 02