Abstract Classes Lecture Objectives To learn about abstract
Abstract Classes
Lecture Objectives • To learn about abstract classes • To understand how to inherit abstract classes • To understand how to override abstract methods inherited from an abstract superclass
Abstract Classes • An abstract class is not intended to be used to create objects. • Abstract classes: § Classes that are too general to create real objects § Used only as abstract superclasses for concrete subclasses and to declare reference variables § Many inheritance hierarchies have abstract superclasses occupying the top few levels § Keyword abstract • Use to declare a class abstract • Also use to declare a method abstract – Abstract classes normally contain one or more abstract methods – All concrete subclasses must override all inherited abstract methods
Abstract Classes (Cont’d) • By declaring one or more methods to be abstract and by omitting the method body, only objects of concrete derived classes, which override the method(s), can be instantiated. • Example: public abstract void draw. Here(); • A class that has at least one abstract method must be declared abstract. • However, a class could be always declared abstract without having any abstract method!
Abstract Classes (Cont’d) • An abstract class declares common attributes and behaviors of the various classes in a class hierarchy. An abstract class typically contains one or more abstract methods that subclasses must override if the subclasses are to be concrete. The instance variables and concrete methods of an abstract class are subject to the normal rules of inheritance.
Abstract Classes (Cont’d) • Attempting to instantiate an object of an abstract class is a compilation error. • Example: abstract class Shape { … } Shape sh = new Shape();
Abstract Classes (Cont’d) • Failure to implement a superclass’s abstract methods in a subclass is a compilation error unless the subclass is also declared abstract. • Example: abstract class Shape { public abstract void draw. Here(); } class Rectangle extends Shape { // The method draw. Here() is NOT implemented! }
Abstract Classes (Cont’d) Example: public abstract class Abstract. Class. Example { protected int x; public void abstract print(); public void set. X(int a) x = a; } { public Abstract. Class. Example() x = 0; } } {
UML Inheritance Diagrams for Abstract Classes
Creating Abstract Superclass: Employee class • abstract superclass Employee § earnings is declared abstract • No implementation can be given for earnings in the Employee abstract class § An array of Employee variables will store references to subclass objects • earnings method calls from these variables will call the appropriate version of the earnings method
The Employee Class Hierarchy
Abstract Class Employee: Outline // Employee abstract superclass. public abstract class Employee { private String first. Name; private String last. Name; private String social. Security. Number; Declare abstract class Employee Attributes common to all employees // three-argument constructor public Employee( String first, String last, String ssn ) { first. Name = first; last. Name = last; social. Security. Number = ssn; } // end three-argument Employee constructor
Abstract Class Employee: Outline (Cont’d) // set first name public void set. First. Name( String first ) { first. Name = first; } // end method set. First. Name // return first name public String get. First. Name() { return first. Name; } // end method get. First. Name // set last name public void set. Last. Name( String last ) { last. Name = last; } // end method set. Last. Name // return last name public String get. Last. Name() { return last. Name; } // end method get. Last. Name
Abstract Class Employee: Outline (Cont’d) abstract method earnings() has no implementation // set social security number public void set. Social. Security. Number( String ssn ) { social. Security. Number = ssn; // should validate } // end method set. Social. Security. Number // return social security number public String get. Social. Security. Number() { return social. Security. Number; } // end method get. Social. Security. Number // return String representation of Employee object public String to. String() { return String. format( "%s %snsocial security number: %s", get. First. Name(), get. Last. Name(), get. Social. Security. Number() ); } // end method to. String // abstract method overridden by subclasses public abstract double earnings(); // no implementation here } // end abstract class Employee
Implementing an Abstract Method • Define the speak() method as abstract in the superclass Animal. public abstract class Animal { protected String kind; // Cow, pig, cat, etc. public Animal() { } public String to. String() { return "I am a " + kind + " and I go " + speak(); } public abstract String speak(); // Abstract method } • Implement speak() differently in public class Cat extends Animal { each subclass. public Cat() { kind = "cat"; public class Cow extends Animal { } public Cow() { public String speak() { kind = "cow"; return "meow"; } } public String speak() { } return "moo"; }
Extending Concrete Classes public class Salaried. Employee extends Employee { private double weekly. Salary; … // calculate earnings; override abstract method earnings in Employee public double earnings() { return get. Weekly. Salary(); } // end method earnings … }
Extending Concrete Classes (Cont’d) public class Commission. Employee extends Employee { private double gross. Sales; // gross weekly sales private double commission. Rate; // commission percentage… // calculate earnings; override abstract method earnings in Employee public double earnings() { return get. Commission. Rate() * get. Gross. Sales(); } // end method earnings … }
Extending Concrete Classes (Cont’d) public class Hourly. Employee extends Employee { private double wage; // wage per hour private double hours; // hours worked for week // calculate earnings; override abstract method earnings in Employee public double earnings() { if ( get. Hours() <= 40 ) // no overtime return get. Wage() * get. Hours(); else return 40 * get. Wage() + ( get. Hours() - 40 ) * get. Wage() * 1. 5; } // end method earnings
- Slides: 18