32
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Embed Size (px)

Citation preview

Page 1: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

MIT AITI 2004 – Lecture 13

Abstract Classes and Interfaces

Page 2: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

What is an Abstract Class?

An abstract class is a class that cannot be instantiated—we cannot create instances of an abstract class.

One or more methods may be declared, but not defined. (The programmer has not yet written code for a few methods).

The declared methods and classes have the keyword abstract in their signature.

Page 3: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public class Employee { private String name; private double salary;

public Employee(String n, double s) { name = n; salary = s; }

public String getName() { return name; }

public double getSalary() { return salary; }

public String description() { return "employee with a salary of $ " + salary; }}

Employee Class

Page 4: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public class Student { private String name; private String course;

public Student(String n, String c) { name = n; course = c; }

public String getName() { return name; }

public String getCourse() { return course; }

public String description() { return "a student majoring in " + course; }}

Student Class

Page 5: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Common Functionality

Student and Employee may have common fields and methods. private String name;

getName()

Instead of repeating code, introduce a superclass

Page 6: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Example Hierarchy

Consider the following class structure:

Person

Employee Student

Superclass

Subclasses

Page 7: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public class Person {String name;

public Person(String n) {name = n;

}

public String getName() {return name;

}}

Person Class

Page 8: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public class Employee extends Person { // private String name; private double salary;

public Employee(String n, double s) { super(n); salary = s; }

// public String getName() { return name; }

public double getSalary() { return salary; }

public String description() { return "an employee with a salary of $" + salary; }}

Employee Subclass of Person

Page 9: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public class Student extends Person{// private String name;private String course;

public Student(String n, String c) {super(n);course = c; }

// public String getName() { return name; }

public String getCourse() {return course; }

public String description() {return "a student majoring in " + course; }

}

Revised Student

Page 10: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

description() Method

Let’s create Person, Employee, and Student objectPerson kwame = new Student("Kwame", "CS");

Employee kojo = new Employee("Kojo", 200000);

Student yaa = new Student("Yaa", "Math");

Description of an Employee and a Student returns:employee with a salary of ¢200000

student majoring in Math

Can we say: kwame.description()

NO! the variable kwame is of type Person, which does not have a description() method defined

Page 11: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public class Person { String name;

public Person(String n) { name = n; }

public String getName() { return name; }

// add a method to return the // description of a Person}

Let's Revise Person

Page 12: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public class Person {String name;

public Person(String n) {name = n;

}

public String getName() {return name;

}

public String description() {return "person named " +

name;}

}

Revised Person

Page 13: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

description() Revisited

Now we can call the description() method on Objects that are of type Person (instances of a Student, Employee, or Person)

Person kwame = new Person("Kwame"); Person kojo = new Employee("Kojo", 200000); Person yaa = new Student("Yaa", "Math");

kwame.description(); kojo.description(); yaa.description();

Page 14: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

description Method Revisited

Now we can call the description() method on Objects that are of type Person (instances of a Student, Employee, or Person)Person kwame = new Person("Kwame");Person kojo = new Employee("Kojo", 20000);Person yaa = new Student("Yaa", "Math");

kwame.description(); // method in Personkojo.description(); // method in Employeeyaa.description(); // method in Student

PROBLEM: We don’t want to create instances of Person, just Students and Employee

Page 15: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Abstract Methods

• Solution: Use the keyword abstract

• A method labeled abstract is declared but not implemented

• Rule: An abstract class can have zero or more abstract methods

• Make description() an abstract method in the class Person

Page 16: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public abstract class Person { String name;

public Person(String n) { name = n; }

public String getName() { return name; }

public abstract String description();}

Abstract Person Class

Page 17: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Abstract Classes

Cannot instantiate or create an object of an abstract class

Person jacob = new Person("Jacob") // ERROR!!

An abstract class can have both abstract and non-abstract methods

Abstract methods need to be defined in concrete subclasses (classes that can be instantiated)

Page 18: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Using Abstract Classes

• Variables can be objects of abstract typesPerson p = new Student("Greg", "CS");

• Here p has the type Person, but references an instance of a non-abstract class, Student

Page 19: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Calls to Abstract Methods

Person[] people = new Person[2];

people[0] = new Employee("Evita", 2000000.0);

people[1] = new Student("Greg", "CS");

for (int i = 0; i < people.length; i++) {

Person p = people[i];

System.out.println(p.getName() + ", " +

p.description());

}

What is the output?Evita, an employee with a salary of $200000

Greg, a student majoring in CS

Page 20: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public abstract class Person { String name;

public Person(String n) { name = n; }

public String getName() { return name; }

// must declare in order to call // method on variable of type Person public abstract String description();}

Abstract Person Class

Page 21: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Advantages

• Classes can now be very general in a class/type hierarchy. – This allows more abstraction in object

oriented programming.

• Have more control over inheritance in a class/type hierarchy.– Make a class abstract even if there are no

abstract methods

Page 22: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Summary of Abstract Classes

• Partial implementation of a class

• Cannot be instantiated

• Use the abstract keyword in their signature.

• Abstract methods are defined in subclasses

Page 23: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Problem Situation

• Consider creating an Object that represents an Intern.

• An Intern behaves like both an Employee and a Student.

• Problem: a class can only extend ONE other class

Page 24: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Interfaces

• Solution: Use an interface, which is a set of requirements for a class

• A class can implement more than one interface

• Methods in an interface are automatically public and abstract

• Make Employee an interface

Page 25: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Interface Details

• An interface is a contract for a class.

• An interface specifies a set of methods a class must implement.

• An interface, similar to an abstract class, cannot be instantiated

• An interface has no constructors, only constants and method declarations.

• Classes implement interfaces using the keyword implements

Page 26: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Employee Interface

public interface Employee {

// fields are public static final constants double STARTING_SALARY = 200000.0;

// methods are automatically public and // abstract; must be overridden in // classes that implement the interface String description();

double getSalary();}

Page 27: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

public class Student { private String name; private String course;

public Student(String n, String c) { name = n; course = c; }

public String getName() { return name; }

public String getCourse() { return course; }

public String description() { return "a student majoring in " + course; }}

Student Class Revisted

Page 28: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

class Intern extends Student implements Employee { private double income;

public Intern(String n, String c) { super(n, c); income = STARTING_SALARY; }

public double getSalary() { return income; }

public String description() { return "intern majoring in "+ super.getCourse() + "with an income of $" + income; }}

Intern Class

Page 29: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Using Intern Class

public static void main(String[] args) {

Intern irish = new Intern("Conor", "Math");

System.out.println(irish.getName() + " ," + irish.description());}

Output: Conor, intern majoring in Math

with an income of $200000.0

Page 30: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Variable Types

• A variable may have the type of an abstract class, an interface, or a concrete class (a non-abstract class).

• Because only a concrete class can be instantiated, an object may only have the type of a concrete class.

• All of these are valid variable declarations: Intern a = new Intern("Conor", "Math"); Student b = new Intern("Conor", "Math"); Employee c = new Intern("Conor", "Math");

Page 31: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Variable vs Object Types (Again)

Intern a = new Intern("Conor", "Math"); Student b = new Intern("Conor", "Math"); Employee c = new Intern("Conor", "Math");

• These expressions will not compile: b.getSalary() // Student does not have getSalary

c.getCourse() // Employee does not have getCourse

• But all of these will: ((Employee)b).getSalary()

((Intern)b).getSalary()

((Student)c).getCourse()

((Intern)c).getCourse()

Page 32: MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

Interface Rules

• Interfaces may specify but do not implement methods.

• A class that implements the interface must implement all its methods.

• Interfaces cannot be instantiated.

• Interfaces may contain constants.