lecture21-Objects4

lecture21-Objects4 - Lecture 21: Inheritance and...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 21: Inheritance and Polymorphism CS 170, Section 000 2 November 2009 12 November 2009 11/12/2009 CS170, Section 000, Fall 2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Plan ± Logistics ¾ More on Inheritance: ¾ constructor chaining, casting, more examples ¾ Polymorphism CS170, Section 000, Fall 2009 2
Background image of page 2
Logistics Homework 1-5: solutions posted Labs 1-10: solutions posted W6 solution to be posted by tomorrow (Fri) HW6 : solution to be posted by tomorrow (Fri) Midterm 2: Tuesday, Nov 17 th ¾ Review Session: Monday Nov 16, 4:15-6pm, W301 ¾ Practice exam: will try to circulate by tomorrow (Fri) 11/16/2009 CS170, Section 000, Fall 2009 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Inheritance – basic idea GeometricObject color isFilled riangle ircle ectangle Triangle Circle Rectangle sideLength radius width height 4
Background image of page 4
Superclasses and Subclasses GeometricObject1.java Circle4.java ectangle1 ja a Rectangle1.java 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Inheritance A subclass inherits all fields and methods from the superclass A subclass can also: Add new fields Add new methods Override the methods of the superclass Superclass’s constructor are not inherited Unlike fields and methods They are invoked explicitly or implicitly 6
Background image of page 6
Using the Keyword super super refers to the superclass of the class in which super appears This keyword can be used in few ways: To call a superclass constructor To call a superclass method To access a superclass public data field 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Invoking Superclass Constructor Superclasses’ constructors can (only) be invoked from subclasses' constructors explicitly Use the keyword super to call the superclass constructor Java requires that the statement that invokes superclass’ constructor using the keyword super appear first in the constructor 8
Background image of page 8
Superclass’s Constructor Is Always Invoked If no superclass constructor is explicitly invoked, the compiler puts super() as the first statement in the constructor public A() { } is equivalent to public A() { super(); } public A(double d) { // some statements } is equivalent to public A(double d) { super(); // some statements 9 }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Constructor Chaining public class Faculty extends Employee { public static void main(String[] args) { Faculty f = new Faculty(“Brad Pitt”); } public Faculty(String name) { System.out.println(”(3) Faculty’s constructor"); } } class Employee extends Person { public Employee() { System.out.println(”(2) Employee's no-arg constructor"); } } class Person { public Person() { System.out.println("(1) Person's no-arg constructor"); 10 } }
Background image of page 10
Constructor Chaining (cont’d) public class Faculty extends Employee { public static void main(String[] args) { Faculty f = new Faculty(“Brad Pitt”); } public Faculty(String name) { super(); System.out.println(”(3) Faculty’s constructor"); } } class Employee extends Person { public Employee() { super(); System.out.println(”(2) Employee's no-arg constructor"); } } class Person { public Person() { 11 super(); System.out.println("(1) Person's no-arg constructor"); } }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Why bother with super()?
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 37

lecture21-Objects4 - Lecture 21: Inheritance and...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online