1 / 32

MIT AITI 2004 – Lecture 13

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.

edmund
Download Presentation

MIT AITI 2004 – Lecture 13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

  2. 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.

  3. 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

  4. 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

  5. Common Functionality • Student and Employee may have common fields and methods. private String name; getName() • Instead of repeating code, introduce a superclass

  6. Example Hierarchy • Consider the following class structure: Person Superclass Employee Student Subclasses

  7. public class Person { String name; public Person(String n) { name = n; } public String getName() { return name; } } Person Class

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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();

  14. 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

  15. 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

  16. public abstract class Person { String name; public Person(String n) { name = n; } public String getName() { return name; } public abstract String description(); } Abstract Person Class

  17. 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)

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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(); }

  27. 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

  28. 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

  29. 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

  30. 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");

  31. 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()

  32. 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.

More Related