lecture11

lecture11 - PIC 20A Lecture 11: More Inheritance, final...

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

View Full Document Right Arrow Icon
PIC 20A Lecture 11: More Inheritance, final Methods and Classes, abstract Methods and Classes
Background image of page 1

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

View Full DocumentRight Arrow Icon
References and Inheritance Subclass references can be implicitly converted to superclass ones eg SubClass subref = new SubClass(…); SuperClass supref = subref; Person p = new Student("Joe", 21, 4.0); // A Student "is a" Person
Background image of page 2
References and Inheritance You can pass or return any subclass reference where a superclass one is expected eg suppose Person had a method public void talkTo( Person person ){ } Then the following is legal: Person p = new Person("Josephine", 22); Student s = new Student("Joe",21,4.0); p.talkTo( s );
Background image of page 3

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

View Full DocumentRight Arrow Icon
Type Casting and Inheritance You can explicitly convert a superclass reference to a subclass one only if it refers to an object of the correct subclass . (Otherwise, a ClassCastException is thrown) eg SubClass subref = (SubClass) supref; Student s = (Student) p; // only if Person p actually refers to a Student
Background image of page 4
Warning: Assigning an object of a superclass to a subclass reference (without a cast) is a syntax error! eg
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/12/2010 for the course PIC PIC 20A taught by Professor Keithouellette during the Spring '09 term at UCLA.

Page1 / 14

lecture11 - PIC 20A Lecture 11: More Inheritance, final...

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

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