Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit...

18
Abstract Classes
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    2

Transcript of Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit...

Page 1: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

Abstract Classes

Page 2: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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

Page 3: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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

Page 4: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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!

Page 5: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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.

Page 6: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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

Page 7: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

• 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! }

Page 8: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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; }}

Page 9: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

UML Inheritance Diagrams for Abstract Classes

Page 10: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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

Page 11: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

The Employee Class Hierarchy

Page 12: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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

Page 13: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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

Page 14: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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

Page 15: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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"; }}

Page 16: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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…}

Page 17: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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…}

Page 18: Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.

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