Download presentation
Presentation is loading. Please wait.
1
Abstract Classes
2
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
3
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
4
Abstract Classes (Cont’d) By declaring one or more methods to be abstract and by omitting the method body, only objects of derived classes which override the method(s) can be instantiated. Example: public abstract void drawHere(); 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!
5
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.
6
Attempting to instantiate an object of an abstract class is a compilation error. Example: Abstract Classes (Cont’d) abstract class Shape { … } Shape sh = new Shape();
7
Failure to implement a superclass’s abstract methods in a subclass is a compilation error unless the subclass is also declared abstract. Example: Abstract Classes (Cont’d) abstract class Shape { public abstract void drawHere(); } class Rectangle extends Shape { // The method drawHere() is NOT implemented! }
8
Abstract Classes (Cont’d) Example: public abstract class AbstractClassExample { protected int x; public void abstract print(); public void setX(int a) { x = a; } public AbstractClassExample() { x = 0; }
9
UML Inheritance Diagrams for Abstract Classes
10
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
11
The Employee Class Hierarchy
12
Abstract Class Employee: Outline // Employee abstract superclass. public abstract class Employee { private String firstName; private String lastName; private String socialSecurityNumber; // three-argument constructor public Employee( String first, String last, String ssn ) { firstName = first; lastName = last; socialSecurityNumber = ssn; } // end three-argument Employee constructor Declare abstract class Employee Attributes common to all employees
13
Abstract Class Employee: Outline (Cont’d) // set first name public void setFirstName( String first ) { firstName = first; } // end method setFirstName // return first name public String getFirstName() { return firstName; } // end method getFirstName // set last name public void setLastName( String last ) { lastName = last; } // end method setLastName // return last name public String getLastName() { return lastName; } // end method getLastName
14
Abstract Class Employee: Outline (Cont’d) // set social security number public void setSocialSecurityNumber( String ssn ) { socialSecurityNumber = ssn; // should validate } // end method setSocialSecurityNumber // return social security number public String getSocialSecurityNumber() { return socialSecurityNumber; } // end method getSocialSecurityNumber // return String representation of Employee object public String toString() { return String.format( "%s %s\nsocial security number: %s", getFirstName(), getLastName(), getSocialSecurityNumber() ); } // end method toString // abstract method overridden by subclasses public abstract double earnings(); // no implementation here } // end abstract class Employee abstract method earnings() has no implementation
15
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 toString() { return "I am a " + kind + " and I go " + speak(); } public abstract String speak(); // Abstract method } Implement speak() differently in each subclass. public class Cat extends Animal { public Cat() { kind = "cat"; } public String speak() { return "meow"; } public class Cow extends Animal { public Cow() { kind = "cow"; } public String speak() { return "moo"; }
16
Extending Concrete Classes public class SalariedEmployee extends Employee { private double weeklySalary; … // calculate earnings; override abstract method earnings in Employee public double earnings() { return getWeeklySalary(); } // end method earnings … }
17
Extending Concrete Classes (Cont’d) public class CommissionEmployee extends Employee { private double grossSales; // gross weekly sales private double commissionRate; // commission percentage… // calculate earnings; override abstract method earnings in Employee public double earnings() { return getCommissionRate() * getGrossSales(); } // end method earnings … }
18
Extending Concrete Classes (Cont’d) public class HourlyEmployee 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 ( getHours() <= 40 ) // no overtime return getWage() * getHours(); else return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5; } // end method earnings
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.