270 likes | 374 Views
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.
E N D
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. • 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.
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.
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.
The super keyword • The keyword super refers to the superclass of the class. • To call a superclass constructor • To call a superclass method
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.
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.
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(); } }
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
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.
Overriding Methods • Create a function printInfo() in both Person and Student class. • super is necessary here.
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.)
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).
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”).
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.
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.
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(){ • .... • }
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.
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
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?
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