330 likes | 515 Views
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.
E N D
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 abstractin their signature.
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
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
Common Functionality • Student and Employee may have common fields and methods. private String name; getName() • Instead of repeating code, introduce a superclass
Example Hierarchy • Consider the following class structure: Person Superclass Employee Student Subclasses
public class Person { String name; public Person(String n) { name = n; } public String getName() { return name; } } Person Class
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
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
description() Method • Let’s create Person, Employee, and Student object Person 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
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
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
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();
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 Person kojo.description(); // method in Employee yaa.description(); // method in Student • PROBLEM: We don’t want to create instances of Person, just Students and Employee
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
public abstract class Person { String name; public Person(String n) { name = n; } public String getName() { return name; } public abstract String description(); } Abstract Person Class
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)
Using Abstract Classes • Variables can be objects of abstract types Person p = new Student("Greg", "CS"); • Here p has the type Person, but references an instance of a non-abstract class, Student
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
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
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
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
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
Interfaces • Solution: Use an interface, which is aset 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
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
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(); }
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
class Intern extendsStudent implementsEmployee { 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
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
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");
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()
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.