lec4 - COMP201 Java Programming Topic 4: Inheritance...

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

View Full Document Right Arrow Icon
COMP201 Java Programming Topic 4: Inheritance Readings: Chapter 5
Background image of page 1

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

View Full DocumentRight Arrow Icon
COMP201 Topic 4 / Slide 2 Outline Introduction Basics Deriving a subclass Using subclasses More advanced issues Inheritance of static methods and fields Abstract classes Final classes The object class and generic programming ( ArrayList class)
Background image of page 2
COMP201 Topic 4 / Slide 3 Introduction to Inheritance Technique for deriving a new class from an existing class. Existing class called superclass, base class, or parent class . New class is called subclass, derived class, or child class.
Background image of page 3

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

View Full DocumentRight Arrow Icon
COMP201 Topic 4 / Slide 4 Subclass and superclass are closely related Subclass share fields and methods of superclass Subclass can have more fields and methods Implementations of a method in superclass and subclass can be different An object of subclass is automatically an object of superclass, but not vice versa The set of subclass objects is a subset of the set of superclass objects. (E.g. The set of Manager s is a subset of the set of Employee s.) This explains the term subclass and superclass. Introduction to Inheritance
Background image of page 4
COMP201 Topic 4 / Slide 5 Introduction to Inheritance Why inherit? Employee class: name, salary, hireDay; getName, raiseSalary(), getHireDay(). Manager is-a Employee , has all above, and Has a bonus getsalary() computed differently Instead of defining Manager class from scratch, one can derive it from the Employee class. Work saved.
Background image of page 5

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

View Full DocumentRight Arrow Icon
COMP201 Topic 4 / Slide 6 Introduction to Inheritance Checking method A method B1 Saving method A method B2 Account method A checking method B1 saving method B2 Why inherit? Inheritance allows one to factor out common functionality by moving it to a superclass, results in better program.
Background image of page 6
COMP201 Topic 4 / Slide 7 Basics/Deriving a Subclass General scheme for deriving a subclass: class subClassName extends superClassName { constructors refined methods additional methods additional fields } Indicate the differences between subclass and superclass
Background image of page 7

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

View Full DocumentRight Arrow Icon
COMP201 Topic 4 / Slide 8 Basics/Deriving a Class Fields for subclass Fields of superclass + additional fields private fields of superclass cannot be directly accessed when defining or refining methods for subclass. ( After all, the subclass is another class!) protected fields can (more on this later). Subclass must have its own constructors Constructors are not inherited (Superclass does not know the additional fields of its subclass and hence it does not know how to create objects of its subclass.)
Background image of page 8
Basics/Deriving a Class Methods for subclass public or protected-access methods of superclass that are not refined.
Background image of page 9

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

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

Page1 / 55

lec4 - COMP201 Java Programming Topic 4: Inheritance...

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

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