1 / 27

Inheritance and Polymorphism

Inheritance and Polymorphism. Chapter 11. Exam week: May 7 – 11 covers until Wednesday’s lecture (Chap 9, 10, 11 and some of Chap 13) Your own cheat sheet Be sure to submit all the homework ! (by May 14). Revisited: Why OOP? . The definition of a class is reusable.

eldon
Download Presentation

Inheritance and Polymorphism

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. Inheritance and Polymorphism Chapter 11

  2. Exam week: May 7 – 11 • covers until Wednesday’s lecture (Chap 9, 10, 11 and some of Chap 13) • Your own cheat sheet • Be sure to submit all the homework! (by May 14)

  3. Revisited: Why OOP? • The definition of a class is reusable. • It is possible to define subclasses to inherit some or all of the main class characteristics. • Person  Student, Teacher • The concept of data classes allows a programmer to create any new data type that is not already defined in the language itself.

  4. Superclasses and Subclasses • OOP allows you to derive new classes from existing classes – inheritance. • Inheritance enables you to define a general class (superclass, parent class) and later extend it to more specialized classes (subclasses, child classes). • A subclass inherits accessible data fields and methods from its superclass, and may add new data fields and methods.

  5. Something to Note • A subclass is not a subset of its superclass. • Private data fields in a superclass are not accessible outside the class. They cannot be used directly in a subclass. • You shouldn’t define a Square class to extend a Rectangle class. • A Java class may inherit directly from only one superclass – single inheritance. Some programming languages allows multiple inheritance.

  6. The super keyword • The keyword super refers to the superclass of the class. • To call a superclass constructor • To call a superclass method

  7. Calling a superclass constructor • You must use the keyword super to call the superclass constructor. • super(arguments) • Person(arguments) is wrong! • Java requires that the statement that uses the keyword super appear first in the constructor.

  8. Constructor Chaining • If superclass constructor is not invoked, Java automatically puts super() as the first statement. • Constructing an instance of a class invokes all the superclasses’ constructors along the inheritance chain. This is called constructor chaining.

  9. public class Person { Person() { System.out.println("Person's constructor"); } } public class Employee extends Person { Employee() { System.out.println("Employee's constructor"); } } public class Faculty extends Employee { Faculty() { System.out.println("Faculty's constructor"); } public static void main(String[] args) { Faculty f1 = new Faculty(); } }

  10. If a class is designed to be extended, it is better to provide a no-arg constructor to avoid errors! • Exercise: Create the GraduateStudentclass, which inherits the Student class. • Add a new data field advisor (String) • Add a constructor with all customized parameters • Add another constructor with only customized advisor • Get and set method for data field advisor

  11. Calling superclass methods • super.method(parameters); • Examples: • super.setName(name); • super.setEmail(email); • It is not necessary to use super in this case, since setName() and setEmail() are inherited by the subclass.

  12. Overriding Methods • Create a function printInfo() in both Person and Student class. • super is necessary here.

  13. Overriding vs. Overloading • Overloading: multiple methods with the same name but different signatures (number of parameters, type of parameters) • Overriding: new implementation for a method in the subclass (The method is already defined in the superclass.)

  14. Polymorphism • Subtype and supertype • Student is a subtype of Person • Person is a supertype for Student • Every instance of a subclass is an instance of its superclass, but not vice versa. • Every student is a person • but not every person is a student • You can pass an instance of a subclass (student1, student2) to a parameter of its super type (Person).

  15. Polymorphism • In your TestPerson.java: • public static void displayObject(Person p){ • ...... • } • //Object created: Jerry • An object of a subclass can be used wherever its superclass object is used – polymorphism (“many forms”).

  16. Dynamic Binding • Both Person and Student have the printInfo() method. • Person p = new Student(); • p.printInfo(); • Which printInfo() is it calling? Person’s or Student’s? • declared type vs. actual type • The method is determined by the actual type.

  17. The protected Data and Methods • Use the protected modifier to enable the members of the class to be accessed by • the subclasses in any package • or classes in the same package.

  18. Accessibility Summary

  19. The final modifier • The final variable is a constant: • public static final double PI = 3.14159; • The final class cannot be extended: • public final class GraduateStudent{ • ... • } • The final method cannot be overridden by its subclasses: • public final void printInfo(){ • .... • }

  20. The ArrayList Class • Once the array is created, its size is fixed. • Add, insert, remove element in an array? • ArrayList can store an unlimited number of objects.

  21. The ArrayList Class

  22. Examples • Create an ArrayList called cityList • Add some cities in the list: “New York,” “Chicago,” “Miami,” “Los Angeles” • What is the list size? • Is Miami in the list? • The location of Chicago in the list? • Is the list empty? • Display the contents in the list • Insert San Francisco at index 2 • Remove Miami from the list • Remove a city at index 1 • Display the contents in reverse order • Remove all elements

  23. Array vs. ArrayList • Creating: • int[] array = new int[10]; • ArrayList list = new ArrayList(); • Accessing an element: • array[2] list.get(2); • Updating an element: • array[1] = “New York”; list.set(1, “New York”) • Returning size • array.lengthlist.size() • Add, insert, remove elements?

  24. ArrayList of Objects • Back to our Person-Student class, add student1 and student2 to an ArrayList • Print information of the two students in the list

More Related