14
Programming With Java ICS 201 University Of Ha’il 1 Chapter 8 Abstract Class

Chapter 8 Abstract Class

  • Upload
    haamid

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 8 Abstract Class. Abstract Class. Sometimes, it does NOT make sense to create objects from specific classes In such case, these classes should be created as abstract An abstract class can only be used to derive other classes; you cannot create objects from an abstract class - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01

University Of Ha’il 1

Chapter 8

Abstract Class

Page 2: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01Abstract Class

Sometimes, it does NOT make sense to create objects from specific classes

In such case, these classes should be created as abstract

An abstract class can only be used to derive other classes; you cannot create objects from an abstract class

An abstract class must have at least one abstract method

8-2

Page 3: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01Abstract Method

An abstract method has a complete method heading, to which has been added the modifier abstract

It has no method body, and ends with a semicolon in place of its bodypublic abstract long getSerNumber();

An abstract method cannot be private

An abstract class can have any number of abstract and/or fully defined methods

Page 4: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01

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 5: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01

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 6: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01UML Inheritance Diagrams for Abstract Classes

Page 7: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01Creating 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 8: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01The Employee Class Hierarchy

Page 9: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01Abstract 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

Page 10: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01Abstract Class Employee: Outline (Cont’d)

public void setFirstName(String first) { firstName = first; } public String getFirstName() { return firstName; }public void setLastName( String last ) { lastName = last; } public String getLastName() { return lastName; }public void setSocialSecurityNumber( String ssn ) { socialSecurityNumber = ssn; }public String getSocialSecurityNumber() { return socialSecurityNumber; }public abstract double earnings(); // no implementation here} // end abstract class Employee

Page 11: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01Extending 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 12: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01Extending 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 13: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

01Extending 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

Page 14: Chapter 8  Abstract Class

Prog

ram

min

g W

ith Ja

vaIC

S 2

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