20 IOO Lec02 Constructors PDF

Title 20 IOO Lec02 Constructors
Author Anand Tiwari
Course Intermediate Object-Oriented Programming
Institution La Trobe University
Pages 7
File Size 142.7 KB
File Type PDF
Total Downloads 603
Total Views 782

Summary

Intermediate Object-Oriented ProgrammingLecture 2 HandoutCovers How to define constructors for derived classes How to call overridden methods Reading Savitch, Absolute Java, 71. and 7. 1 Defining Constructors for Derived ClassesIn the previous lecture, we learned how to define derived classes. Howev...


Description

Intermediate Object-Oriented Programming Lecture 2 Handout Covers • How to define constructors for derived classes • How to call overridden methods

Reading • Savitch, Absolute Java, 71. and 7.2

1

Defining Constructors for Derived Classes

In the previous lecture, we learned how to define derived classes. However, we more or less skipped over the issue of defining their constructors. Here are the rules regarding the derived class constructors: 1. A derived class constructor can use a super statement to call a constructor in the base class. The super statement, if present, must be the first statement of the constructor. (We have seen this rule in an example in Lecture 1) 2. A constructor can use a this statement to call another constructor in the same class. The this statement, if present, must be the first statement of the constructor. (This rule actually applies to all classes: base classes and derived classes) 3. If a derived class constructor have a super statement with no arguments, this statement can be omitted. (Thus, if a derived class constructor does not start explicitly with a super or this statement, it means that it starts with a super statement with no arguments.) 4. If the derived class has no constructor explicitly defined, the no-argument default constructor will be automatically provided by the Java runtime system, and this default constructor will call the no-argument constructor of the base class. Thus effectively we have four cases: Case 1. The derived class constructor has a super statement, which calls the base class constructor with matching parameters. Case 2. The derived class constructor has a this statement, which calls the constructor in the same class with matching parameter. Case 3. The the derived class constructor does not explicitly have a super or this statement as the first statement. In this case the derived class constructor call the no-argument constructor of the base class. Case 4. The derived class has no constructors explicitly defined. In this case, the Java runtime system provides a no-argument default constructor, which calls the

1.1

Example: Case 1 - Derived class constructor has a super statement

public class Person { private String id; private String name; public Person(String id, String name) { this.id = id; this.name = name; } public String getId(){ return id; } public String getName(){ return name;} public String toString() { return "Person[id: " + id + ", name: " + name + "]"; } } public class Student extends Person { private String course; // This constructor calls a constructor in the superclass public Student(String id, String name, String course) { super(id, name); this.course = course; } public String toString() { return "Student[id: " + getId() + ", name: " + getName() + ", course: " + course + "]"; } public static void main(String[] args) { Student s = new Student("P01", "Smith", "IT"); System.out.println(s); } }

The subclass constructor calls the constructor in the superclass to initilize id and name. It then initializes course. public class Tester { public static void main(String[] args) { Student s = new Student("P01", "Smith", "IT"); System.out.println(s); } }

Output: Student[id: P01, name: Smith, course: IT]

1.2

Example: Case 2 - Derived class constructor has a this statement

public class Person { private String id; private String name; public Person(String id, String name) { this.id = id; this.name = name; } // get methods // toString method } public class Student extends Person { private String course; public Student(String id, String name, String course) { super(id, name); this.course = course; } public Student(String id, String name) { this(id, name, "Unknown"); // calls the other constructor in the same class // and initializes course to "Unknown" } // toString method }

The second constructor of class Student calls the first constructor (in the same class), which, in turn, calls the constructor in the base class Person. public class Tester { public static void main(String[] args) { Student s1 = new Student("P01", "Smith", "IT"); System.out.println(s1); Student s2 = new Student("P02", "Adams"); System.out.println(s2); } }

Output: Student[id: P01, name: Smith, course: IT] Student[id: P02, name: Adams, course: Unknown]

1.3

Example: Case 3 - Derived class constructor has no super or this statement

public class Person { private String id; private String name; public Person(String id, String name) { this.id = id; this.name = name; } public Person() // no-argument constructor { this("ID unknown", "Name unknown"); // calls the constructor in the same class, which initializes // id and name to "ID unknown" and "Name unknown" respectively } // get methods // toString method } public class Student extends Person { private String course; public Student(String id, String name, String course) { super(id, name); this.course = course; } // This constructor calls the no-argument constructor in the base class public Student() { course = "Course unknown"; } // toString method }

The second constructor of class Student automatically calls the no-argument constructor of the base class. public class Tester { public static void main(String[] args) { Student s1 = new Student("P01", "Smith", "IT"); System.out.println(s1); Student s2 = new Student(); System.out.println(s2); } }

Output: Student[id: P01, name: Smith, course: IT] Student[id: ID unknown, name: Name unknown, course: Course unknown]

1.4

Example: Case 4 - Derived class has no constructor explicitly defined

public class Person { private String id; private String name; public Person(String id, String name) { this.id = id; this.name = name; } public Person() // no-argument constructor { this("ID unknown", "Name unknown"); // calls the constructor in the same class, which initializes // id and name to "ID unknown" and "Name unknown" respectively } // get methods // toString method } public class Student extends Person { private String course; // no constructor explicitly defined public String toString() { return "Student[id: " + getId() + ", name: " + getName() + ", course: " + course + "]"; } }

No constructor is defined in class Student. When an instance of Student is created, the default no-argument constructor is called, which in turn calls the no-argument constructor in the base class Person. public class Tester { public static void main(String[] args) { Student s = new Student(); System.out.println(s); } }

Output: Student[id: ID unknown, name: Name unknown, course: null]

Note that course is initialized to the default value null.

2

How to Access Overridden Methods in Superclasses – Another Use of the super Keyword

Apart from using the super keyword to access a constructor in the superclass, we can also use this keyword to access a method in the superclass which has been overridden by the subclass.

A subclass can invoke an overridden method defined in the superclass by using the super keyword with the following syntax: super.methodName(arguments)

The example below will illustrate why we need this feature and how we would use it.

Example Suppose we have the Customer class defined below. Note that we have the display method to display the name and gender of the customer on the screen.

public class Customer { private String name; private char gender;

// ’M’ or ’F’

public Customer(String name, char gender) { this.name = name; this.gender = gender; } // methods to get name and gender public void display() { System.out.println("Name: " + name); System.out.println("Gender: " + gender); } }

Next, we define the subclass FrequentFlyer with its own display method. Suppose we want this method to display • the name and gender (defined in the superclass) • as well as the id and the points (defined in the subclass). And suppose we also want to call the method display defined in the superclass to display the name and gender (that is we want to reuse the logic). Then we can use the super keyword as shown in the class below.

public class FrequentFlyer extends Customer { private String id; private int points; public FrequentFlyer(String id, String name, char gender, int points) { super(name, gender); this.id = id; this.points = points; } public String toString() { return "FrequentFlyer[id: " + id + ", name: " + getName() + ", gender: " + getGender() + ", points: " + points + "]"; } public void display() { System.out.println("Id: " + id); super.display(); System.out.println("Points: " + points); } }

Observe that: • The display method in the subclass calls the display method in the superclass, with statement super.display() • The superclass display method, in turn, displays name and gender on the screen. • Consequently, the subclass display method displays the id, name, gender and points, in that order. Thus, the following test program public class Tester { public static void main(String [] args) { FrequentFlyer ff = new FrequentFlyer("FF123", "Bob", ’M’, 1000); ff.display(); } }

will produce the output Id: FF123 Name: Bob Gender: M Points: 1000

When we use the super keyword to access an overridden method in the superclass, we often refer to it as a super reference....


Similar Free PDFs